You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

2.5 KiB

TimeTracker

Instructions

Write a ProjectTime class to track the duration of a project using start and end times. You will be asked to add getter and setter methods for the startTime and endTime attributes. When you set one of them, you must update the hoursLogged attribute. The hoursLogged getter should output the hours in the following format:

  • Less than 120 minutes: hoursLogged minutes
  • Less than 120 hours: hoursLogged hours
  • Less than 120 days: hoursLogged days
  • More than 120 days: hoursLogged months

If there is any error, the number of hoursLogged should be handled accordingly.

💡 The hoursLogged shouldn't be a negative number except -1 in case of errors.

Expected Functions

public class ProjectTime {
    private String startTime;
    private String endTime;
    private float hoursLogged;

    public ProjectTime(String start, String end);

    public void setStartTime();
    public void setEndTime();

    public String getStartTime();
    public String getEndTime();

    public String getHoursLogged();
}

Usage

Here is a possible ExerciseRunner.java to test your function :

public class ExerciseRunner {
    public static void main(String[] args) {
        // Example of a very short project
        ProjectTime shortProject = new ProjectTime("2023-05-14 09:00", "2023-05-14 09:30");
        System.out.println("Short Project Total Logged Time: " + shortProject.getHoursLogged());  // Should be "30 m"

        // Example of an overnight project
        ProjectTime overnightProject = new ProjectTime("2023-05-14 20:00", "2023-05-15 08:00");
        System.out.println("Overnight Project Total Logged Time: " + overnightProject.getHoursLogged());  // Should be "12 h"

        // Example of a full day project
        ProjectTime fullDayProject = new ProjectTime("2023-05-14 00:00", "2023-05-15 00:00");
        System.out.println("Full Day Project Total Logged Time: " + fullDayProject.getHoursLogged());  // Should be "24 h"

        // Example with incorrect format
        ProjectTime errorProject = new ProjectTime("2023-05-14", "2023-05-15 08:00");
        System.out.println("Error Project Total Logged Time: " + errorProject.getHoursLogged());  // Should handle error or indicate incorrect format
    }
}

and its output :

$ javac *.java -d build
$ java -cp build ExerciseRunner
Short Project Total Logged Time: 30 m
Overnight Project Total Logged Time: 12 h
Full Day Project Total Logged Time: 24 h
Error Project Total Logged Time: -1
$