/* * Intro to Programming CSE 1310 * University of Texas at Arlington */ package code14nov17lab3to4; import java.io.File; import java.io.FileNotFoundException; import java.util.InputMismatchException; import java.util.Scanner; /** * * @author jcmtiernan */ public class Code14Nov17Lab3to4 { /** * @param args the command line arguments */ public static void main(String[] args) { /* Define a variable called exactStartTime and give it a value of 0754 . Define a variable called exactBreakStartTime and give it a value of 1100 . Define a variable called exactBreakEndTime and give it a value of 1204 . Define a variable called exactEndTime and give it a value of 1558 . Create four new double variables called recStartTime, recBreakStartTime, recBreakEndTime, and recEndTime where rec stands for “recorded”. */ // Cannot put 0 on front of number in code // or it is interpreted as OCTAL not DECIMAL /* original values int exactStartTime =754; int exactBreakStartTime = 1136; int exactBreakEndTime = 1214; int exactEndTime = 1558; //second set of values int exactStartTime = 919; int exactBreakStartTime = 1058; int exactBreakEndTime = 1214; int exactEndTime = 1832; */ /* third set of values int exactStartTime = 953; int exactBreakStartTime = 1127; int exactBreakEndTime = 1244; int exactEndTime = 2024; */ File timeData = new File("timeData"); Scanner inData = new Scanner(System.in); boolean fileFound = true; try { inData = new Scanner(timeData); } catch (FileNotFoundException fnf) { System.out.println("The input file was not found."); fileFound = false; } Scanner inString; int makeMilTime = 0; int exactStartTime = 0; int exactBreakStartTime = 0; int exactBreakEndTime = 0; int exactEndTime = 0; int recStartTime = 0; int recBreakStartTime = 0; int recBreakEndTime = 0; int recEndTime = 0; double rate = 0; String inputStartTime = ""; String inputBreakStartTime = ""; String inputBreakEndTime = ""; String inputEndTime = ""; String timeIn = ""; String timeCheck = ""; String hr = ""; String min = ""; String ap = ""; int hrInt = 0; int minInt = 0; int lineCnt = 1; while ((fileFound) && (inData.hasNext())) { // get normal time values from an input file //System.out.println("line count is "+lineCnt); for (int val = 0; val < 4; val++) { //System.out.println("\n\n"); timeIn = inData.next(); //System.out.println("Read in *"+timeIn+"* length "+timeIn.length()+" from file line "+(val+1)); inString = new Scanner(timeIn); inString.useDelimiter(":"); hr = inString.next(); //System.out.println("Hour is "+hr); int l = hr.length(); //System.out.println("l is length of hr = "+l); timeCheck = timeIn.substring(l+1); min = timeCheck.substring(0,2); ap = timeCheck.substring(2); inString.close(); //System.out.println("Hour is "+hr+", minute is "+min+", and it is "+ap); hrInt = Integer.parseInt(hr); minInt = Integer.parseInt(min); // if data is being hardcoded as military time or if user is giving military time input // in a variable, e.g. called inputMilTime, then hrInt and minInt are: // hrInt = inputMilTime / 100; // minInt = inputMilTime % 100; //System.out.println("Military time of value "+(val+1)+" on day "+lineCnt+" of "+timeIn+" is "+makeMilTime); /* i. If the hour value is negative, then set the hour to 0, take the absolute value of the minutes, and tell the user what the error was and how it was handled. Note: since a negative value is handled in the hour, then the exact minute values cannot be negative. */ if (hrInt < 0) { hrInt = 0; minInt = Math.abs(minInt); System.out.println("ERROR CORRECTION: The time value was negative so hour is set to 0\n" + " and minInt is set to its absolute value to keep the minutes."); } /* ii. If minutes value is greater than 59, then take the minute value divide by 60 and add that number to the hours. Also take the minutes value and modulo by 60 and save that as the new minute value. Give the user a message telling them the number of hours added to the hours and the new value for minutes. */ if (minInt > 59) { System.out.println("ERROR CORRECTION: The minute value "+minInt+" was greater than 59"); int tempHr = minInt/60; hrInt = hrInt + (tempHr); minInt = minInt % 60; System.out.println(" An hour is added to hours to get "+hrInt +" and \n the minute value " +"is set to modulo 60 which gives "+minInt); } /* iii. If the hour value is equal to 24, then set the hour to 0 and tell the user what the error was and how it was handled. */ if ((hrInt) == 24) { hrInt = 0; System.out.println("ERROR CORRECTION: Midnight hour set to 00."); } /* iv. If the hour value is greater than 24, then modulo the hour value by 24, set the hour to this remainder, then tell the user what the error was and how it was handled. */ if ((hrInt) > 24) { System.out.println("ERROR CORRECTION: Hour value "+hrInt+" was greater than 24."); hrInt = hrInt % 12; System.out.println(" Hour value is set to modulo 12 which gives "+hrInt); } // create military time from input if (ap.equalsIgnoreCase("am")) { if (hrInt == 12) { makeMilTime = minInt; } else { makeMilTime = hrInt * 100 + minInt; } } else { if (hrInt == 12) { makeMilTime = 1200 + minInt; } else { makeMilTime = (hrInt + 12) * 100 + minInt; } } switch (val) { case 0: exactStartTime = makeMilTime; inputStartTime = timeIn; break; case 1: exactBreakStartTime = makeMilTime; inputBreakStartTime = timeIn; break; case 2: exactBreakEndTime = makeMilTime; inputBreakEndTime = timeIn; break; case 3: exactEndTime = makeMilTime; inputEndTime = timeIn; break; } } boolean goodData = true; if (inData.hasNext()) { try { rate = inData.nextDouble(); //System.out.println("Pay rate from file is "+rate); } catch (InputMismatchException ime) { rate =8.25; goodData = false; System.out.println("ERROR: invalid rate data in the file"+ime); } } System.out.println(""); /* Second, your program must insure that the four exact error-checked values are in order from earliest to latest time. The smallest valid time value is 0000 and the largest valid time value is 2399 within one day. Check to make sure that the four values are in order. If the four values are not in order from smallest to largest, then ignore the four time values for that day and give the user a message telling them the data was not valid because the values were not in time order. */ // After four values are read in, check order // If the times are in order, then do the remaining processing, // otherwise just increment to next row. if (((exactStartTime < exactBreakStartTime) && (exactBreakStartTime < exactBreakEndTime)) && (exactBreakEndTime < exactEndTime ) && goodData) { /* System.out.println("\n"); // values now used for calculations System.out.println("Exact Start Time is "+exactStartTime); System.out.println("Exact Break Start Time is "+exactBreakStartTime); System.out.println("Exact Break End Time is "+exactBreakEndTime); System.out.println("Exact End Time is "+exactEndTime+"\n"); */ // no error checking is done on input recStartTime = rndToIncrements(exactStartTime); recBreakStartTime = rndToIncrements(exactBreakStartTime); recBreakEndTime = rndToIncrements(exactBreakEndTime); recEndTime = rndToIncrements(exactEndTime); /* System.out.println("Recorded Start Time is "+recStartTime); System.out.println("Recorded Break Start Time is "+recBreakStartTime); System.out.println("Recorded Break End Time is "+recBreakEndTime); System.out.println("Recorded End Time is "+recEndTime+"\n"); */ // need to deal with minutes seperately out of 60 not out of 100 int rBSTm = recBreakStartTime % 100; int rBSTh = recBreakStartTime - rBSTm; int rSTm = recStartTime % 100; int rSTh = recStartTime - rSTm; int firstHalf = (rBSTh - rSTh) + (rBSTm - rSTm); if ((rBSTm - rSTm) < 0) { // 1600 - 1215 gives 385 but should give 345 so // 1600 - 1200 will give 400 and // 00 - 15 needs to give -55 (for 45) so - 40 from result (diff of 100 and 60) // ex. 00 - 30 needs to give -70 (for 30) so -40 works // ex. 00 - 45 needs to give -85 (for 15) so -40 works // ex. 15 - 30 needs to give -55 (for 45) firstHalf = firstHalf - 40; } int rBETm = recBreakEndTime % 100; int rBETh = recBreakEndTime - rBETm; int rETm = recEndTime % 100; int rETh = recEndTime - rETm; int secondHalf = (rETh - rBETh)+(rETm - rBETm); if ((rETm - rBETm) < 0) { secondHalf = secondHalf - 40; } //System.out.print("1st half is "+firstHalf+" and "); //System.out.println("2nd half is "+secondHalf+".\n"); // need to check for min value over 45 and handle int day = firstHalf + secondHalf; //System.out.println("Sum of 1st and 2nd half is "+day+"\n"); if ((day % 100) > 45) { day = (day - 60) + 100; // minus a 60 minute hour and add 100 to record the hour } //System.out.println("Total hours recorded in the form of hhmm is "+day+"\n"); //System.out.println("Total recorded is "+(day/100)+" hours and "+(day%100)+" minutes.\n"); // Convert HHMM total to HH.hh double hoursPay = ((day / 100) + (((day % 100)/15) * .25)); //System.out.println("Total hours to pay is "+hoursPay+"\n"); double hoursRate = rate; double totalPay = hoursPay * hoursRate; /* System.out.print("Total pay for day "+lineCnt+" is $"); System.out.printf("%.2f",totalPay); System.out.println(" at $"+hoursRate+" per hour.\n\n"); System.out.print("Exact Start Time is "+exactStartTime); System.out.println(" and Recorded Start Time is "+recStartTime); System.out.print("Exact Break Start Time is "+exactBreakStartTime); System.out.println(" and Recorded Break Start Time is "+recBreakStartTime); System.out.print("Exact Break End Time is "+exactBreakEndTime); System.out.println(" and Recorded Break End Time is "+recBreakEndTime); System.out.print("Exact End Time is "+exactEndTime); System.out.println(" and Recorded End Time is "+recEndTime+"\n\n"); */ System.out.printf("%6s%3d%9s%9s%9s%9s%9s\n","Day ",lineCnt,"","Start","End","","Hourly"); System.out.printf("%9s%9s%9s%9s%9s%9s\n","","Start","Break","Break","End","Rate"); System.out.printf("%9s%9s%9s%9s%9s%4s%5.2f\n","Input",inputStartTime,inputBreakStartTime,inputBreakEndTime,inputEndTime,"$",hoursRate); System.out.printf("%9s%9d%9d%9d%9d\n","Exact",exactStartTime,exactBreakStartTime,exactBreakEndTime,exactEndTime); System.out.printf("%9s%9d%9d%9d%9d\n\n","Recorded",recStartTime,recBreakStartTime,recBreakEndTime,recEndTime); System.out.printf("%9s%9s%9s%9s%9s%9s\n","Values:","hhmm","Hours","& Mins","Total","Rate/hr"); System.out.printf("%9s%9d%9d%9d%9.2f%9.2f\n","",day,(day/100),(day%100),hoursPay,hoursRate); //System.out.printf("%9s%9s\n%9s%9d\n","","Total","hhmm",day); //System.out.printf("%9s%9d\n%9s%9d\n\n","Hours &",(day/100),"Minutes",(day%100)); //System.out.printf("%9s%9.2f\n%9s%9.2f%9s\n","As hours",hoursPay,"At rate",hoursRate," per hour"); System.out.printf("%8s%1s%9.2f\n\n","Pays ","$",totalPay); } else { System.out.printf("%6s%3d%9s%9s%9s%9s%9s\n","Day ",lineCnt,"","Start","End","","Hourly"); System.out.printf("%9s%9s%9s%9s%9s%9s\n","","Start","Break","Break","End","Rate"); System.out.printf("%9s%9s%9s%9s%9s%4s%5.2f\n","Input",inputStartTime,inputBreakStartTime,inputBreakEndTime,inputEndTime,"$",rate); System.out.printf("%9s%9d%9d%9d%9d\n","Exact",exactStartTime,exactBreakStartTime,exactBreakEndTime,exactEndTime); System.out.println("\nERROR: Input times are out of order for day "+lineCnt); inData.nextLine(); } System.out.println("----------------------------------------------------------"); lineCnt++; } } public static int rndToIncrements(int milTime) { /* Get hours and minutes seperately */ int min = milTime % 100; int retTime = milTime - min; //System.out.println("in rnd: milTime = "+milTime+" min = "+min+" retTime = "+retTime); if ((min >= 0) && (min <= 7)) { min = 0; } else if ((min >= 8) && (min <= 22)) { min = 15; } else if ((min >= 23) && (min <= 37)) { min = 30; } else if ((min >= 38) && (min <= 52)) { min = 45; } else if ((min >= 53) && (min <= 59)) { min = 100; } else { min = 0; // minute value was greater than 59 } retTime = retTime + min; if (retTime > 2359) { retTime = 0; } return retTime; } }