Before I had asked a question about subtracting time and I thought I had it. However as I was debugging, several bugs appeared. Basically what I am asking is to create a punch clock, but I can't use the Java Calendar API.
Users will input start times and the time they punched out and it will calculate the difference between the times.
Mine is not working very well and I don't know why. For example, If 4:00am is the start and 12:00am is the end, 8 hrs 00 min is returned. That is incorrect. I thought I created an if statement for the possibilities.
Here is my code:
public static void elapsedTime()
{
Scanner reader = new Scanner (System.in);
System.out.print("Enter the beginning hour: ");
int startingHour = reader.nextInt();
System.out.print("Enter the beginning minute(s): ");
int startingMin = reader.nextInt();
System.out.print("Enter AM/PM: ");
reader.nextLine();
String startingTOD = reader.nextLine();
System.out.print("Enter the ending hour: ");
int endingHour = reader.nextInt();
System.out.print("Enter the ending minute(s): ");
int endingMin = reader.nextInt();
System.out.print("Enter AM/PM: ");
reader.nextLine();
String endingTOD = reader.nextLine();
System.out.println();
int hours;
int minutes = endingMin - startingMin;
String Am = "am" ;
String Pm = "pm" ;
if (endingTOD.equalsIgnoreCase(startingTOD) && minutes < 0 )
{
hours = (endingHour - startingHour) -1 ;
minutes = minutes + 60 ;
System.out.println(hours + " " + minutes);
}
else if (endingHour > startingHour&& endingTOD.equalsIgnoreCase(startingTOD) & minutes > 0 )
{
hours = endingHour - startingHour;
System.out.println(hours + " " + minutes);
}
else if (endingHour > startingHour && endingTOD.equalsIgnoreCase(startingTOD) && minutes == 0 )
{
hours = (endingHour-startingHour);
minutes = 0;
System.out.println(hours + " " + minutes);
}
else if (endingHour < startingHour && endingTOD.equalsIgnoreCase(startingTOD) && minutes == 0 )
{
hours = (endingHour-startingHour) + 12;
minutes = 0;
System.out.println(hours + " " + minutes);
}
else if ( endingHour==startingHour && minutes == 0)
{
hours = 12;
minutes = 0;
System.out.println(hours + " " + minutes);
}
else if (( endingTOD.equalsIgnoreCase(Pm) && startingTOD.equalsIgnoreCase(Am)) && minutes > 0)
{
hours = (endingHour - startingHour) + 12;
System.out.println(hours + " " + minutes);
}
else if (( endingTOD.equalsIgnoreCase(Pm) && startingTOD.equalsIgnoreCase(Am)) && minutes < 0)
{
hours = (endingHour - startingHour) -1 ;
minutes = minutes + 60 ;
System.out.println(hours + " " + minutes);
}
else if (endingHour > startingHour && ( endingTOD.equalsIgnoreCase(Pm) && startingTOD.equalsIgnoreCase(Am)) && minutes == 0)
{
hours = (endingHour-startingHour) + 12;
minutes = 0;
System.out.println(hours + " " + minutes);
}
else if (endingHour < startingHour &&( endingTOD.equalsIgnoreCase(Pm) && startingTOD.equalsIgnoreCase(Am)) && minutes == 0)
{
hours = (endingHour - startingHour) +24;
minutes = 0;
System.out.println(hours + " " + minutes);
}
else if (endingHour < startingHour &&( endingTOD.equalsIgnoreCase(Pm) && startingTOD.equalsIgnoreCase(Am)) && minutes > 0)
{
hours = (endingHour - startingHour) + 2;
System.out.println(hours + " " + minutes);
}
}