//*************************************** // Program Name: calcparking.java * // Author: Pat Moss * // Date: Oct 10, 2005 Rev. 02 * // * // Demonstrate class "calcparking", etc.* //*************************************** // calculate and display the parking fee // for the current vehicle, as follows: // Code: Desc.: Parking Fees: // C Cars: First 2 hours = Free // Next 3 hours = 0.50/hour // Next 11 hours = 0.25/hour // T Trucks: First 1 hours = Free // Next 2 hours = 1.00/hour // Next 13 hours = 0.75/hour // S Senior Citizens: No charge // H Display User Help screen import java.util.Scanner; import java.text.NumberFormat; public class calcparking { String inputstr; char glinputCode; String glinputCodestr; boolean inputcodeok = false; boolean bgntimeok = false; boolean endtimeok = false; boolean totalhrsok = false; double bgnhrs, endhrs, totalhrs; double glcheckHrs; double gldblHrs; double gltotalHrs; double glparkingFee; Scanner scan = new Scanner(System.in); // constructor calcparking() { initall(); } // initialize all variables void initall() { inputstr = ""; glinputCode = ' '; glinputCodestr = ""; inputcodeok = false; bgntimeok = false; endtimeok = false; totalhrsok = false; bgnhrs = 0.0; endhrs = 0.0; totalhrs = 0.0; glcheckHrs = 0.0; gldblHrs = 0.0; gltotalHrs = 0.0; glparkingFee = 0.0; } // process the current vehicle information double getInputData() { initall(); do { System.out.print ("Car(C), Truck(T), Senior(S), or Help(H)?"); inputstr = scan.nextLine().toUpperCase(); inputcodeok = checkinputcode(inputstr); } while (!inputcodeok); // System.out.println("** glinputCode: " + glinputCode); do { // "S" = Senior citizen = "No charge" if (glinputCode == 'S') { totalhrs = 0.0; gltotalHrs = 0.0; break; } do { System.out.print ("Begin time (HH:MM AM/PM)?"); inputstr = scan.nextLine().toUpperCase(); bgntimeok = checkinputtime(inputstr); } while (!bgntimeok); bgnhrs = glcheckHrs; do { System.out.print ("Exit time (HH:MM AM/PM)?"); inputstr = scan.nextLine().toUpperCase(); endtimeok = checkinputtime(inputstr); } while (!endtimeok); endhrs = glcheckHrs; totalhrs = endhrs - bgnhrs; gltotalHrs = Math.ceil (totalhrs); // System.out.print ("**bgn/end/totalhrs/gltotalHrs: "); // System.out.print (bgnhrs + " " + endhrs + " " + totalhrs); // System.out.println (" " + gltotalHrs); if((totalhrs < 0.0) || (totalhrs > 16.0)) { // error -- do nothing here } else { totalhrsok = true; } } while (!totalhrsok); // calculate the Parking Fee glparkingFee = getParkingFee(glinputCode, gltotalHrs); // display the parking Code/Total hours/Parking Fee printReport (glinputCodestr, gltotalHrs, glparkingFee); // return the parking fee to the main driver program return glparkingFee; } // check for a valid input code // must be one of "C", "T", "S", or "H" boolean checkinputcode (String inpstr) { glinputCode = ' '; glinputCodestr = ""; if (inpstr.length() != 1) return false; char inpcode = inpstr.charAt(0); switch (inpcode) { case 'C': glinputCode = inpcode; glinputCodestr = "Car"; break; case 'T': glinputCode = inpcode; glinputCodestr = "Truck"; break; case 'S': glinputCode = inpcode; break; case 'H': // "H" = Help = Display a Help screen System.out.println ("User Help:"); System.out.println (""); System.out.println ("C Cars: First 2 hours = Free"); System.out.println (" Next 3 hours = 0.50/hour"); System.out.println (" Next 11 hours = 0.25/hour"); System.out.println (""); System.out.println ("T Trucks: First 1 hours = Free"); System.out.println (" Next 2 hours = 1.00/hour"); System.out.println (" Next 13 hours = 0.75/hour"); System.out.println (""); System.out.println ("Hours are adjusted upward to next whole hour."); System.out.println (""); System.out.println ("S Senior Citizens: No charge."); System.out.println (""); System.out.println ("H This User Help screen."); System.out.println (""); return false; default: return false; } return true; } // check for a valid input time string value // valid input format is HH:MM XM (where X = "A" or "P") // and convert a valid input time to decimal hours since midnight // for example, 2:30 AM is 2.50 hours, and 2:30 PM is 14.50 hours boolean checkinputtime (String inpstr) { glcheckHrs = 0.0; if (inpstr.length() != 8) return false; if (!checkformat (inpstr, "99:99 XM")) return false; // fetch hours, minutes, and AM/PM from the input string String strHH = inpstr.substring(0,2); String strMM = inpstr.substring(3,5); String strXM = inpstr.substring(6,8); // System.out.println("inpstr: " + inpstr); // System.out.println("HH/MM/XM: " + strHH + " " + strMM + " " + strXM); // System.out.print ("Lens: " + strHH.length() + " ") // System.out.println(strMM.length() + " " + strXM.length()); // convert hours and minutes from string to integer format int intHH = Integer.parseInt(strHH); int intMM = Integer.parseInt(strMM); // compute total hours in decimal format double numhrs = (double) intHH + intMM / 60.0; // fix per Dave Hammer: // a. if hours >= 12, then subtract 12 from hours // this applies for 12:xx AM and for 12:xx PM // b. if suffix = "PM", then add 12 back to hours // then we have decimal "military time, // 00.00 hours to approx 23.99 hours if (numhrs >= 12.0) numhrs -= 12.0; if (strXM.equals("PM")) numhrs += 12.0; glcheckHrs = numhrs; return true; } // check for a valid string format // valid format characters include: // 9 = must be 0 to 9 // X = must be A or P (for AM or PM) // Literal characters: space or : or M // // Example format string for an input time: // 99:99 XM // boolean checkformat (String inpstr, String inpfmt) { int inplen = inpstr.length(); for (int i=0; i '9')) return false; break; default: return false; } } return true; } // calculate the parking fee // based on: // 1. The input code ("C", "T", "S", or "H") // 2. The total hours (0.0 to 16.0) // for the current vehicle, as follows: // Code: Desc.: Parking Fees: // C Cars: First 2 hours = Free // Next 3 hours = 0.50/hour // Next 11 hours = 0.25/hour // T Trucks: First 1 hours = Free // Next 2 hours = 1.00/hour // Next 13 hours = 0.75/hour // S Senior Citizens: No charge // H Help: No charge double getParkingFee (char inpcode, double tothrs) { // adjust total hours upward to the next whole hour value double dblhrs = Math.ceil (tothrs); double totfee = 0.0; // System.out.println ("tothrs,dblhrs: " + tothrs + " " + dblhrs); switch (inpcode) { case 'C': if (dblhrs <= 2.0) { totfee = 0.0; } else if ((dblhrs > 2.0) && (dblhrs <= 5.0)) { totfee = (dblhrs - 2.0) * 0.50; } else { totfee = (3) * 0.50; totfee += (dblhrs - 5.0) * 0.25; } break; case 'T': if (dblhrs <= 1.0) { totfee = 0.0; } else if ((dblhrs > 1.0) && (dblhrs <= 3.0)) { totfee = (dblhrs - 1.0) * 1.00; } else { totfee = (2.0) * 1.00; totfee += (dblhrs - 3.0) * 0.75; } break; case 'S': totfee = 0.0; break; case 'H': totfee = 0.0; break; default: totfee = 0.0; break; } return totfee; } // display the parking code, parking hours, and parking fee void printReport (String inpcodestr, double dblhrs, double parkfee) { // adjust total hours upward to the next whole hour value int inthrs = (int) Math.ceil(dblhrs); // Round the output to two decimal places NumberFormat fmt = NumberFormat.getCurrencyInstance(); System.out.println (""); System.out.print ("The " + inpcodestr); System.out.print (" parking fee due for "); System.out.print (inthrs + " hours is "); System.out.println (fmt.format(parkfee)); } }