Advertisement
Guest User

CIS 131 Lab 1

a guest
Feb 19th, 2020
161
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 12.91 KB | None | 0 0
  1. import java.awt.*;
  2. import java.util.*;
  3.  
  4. /**
  5. * @author - Kurtis Renth
  6. * @version - CIS 131 Lab 1
  7. */
  8.  
  9. public class CIS131Lab1 {
  10.  
  11.     //constants
  12.     final static double RATE_MON_FRI = 1.25;
  13.     final static double RATE_SAT_SUN = 0.50;
  14.     final static double MIN_MON_FRI = 3.00;
  15.     final static double MIN_SAT_SUN = 2.00;
  16.     final static double MAX_MON_FRI = 15.00;
  17.     final static double MAX_SAT_SUN = 10.00;
  18.     final static int MAX_TIME = 2359;
  19.     final static int MIN_TIME = 0;
  20.     final static double INTERVAL_TIME = 15.0;
  21.     final static String SENTINEL = "quit";
  22.     final static int MAX_TIME_IN_MIN = 1440;                                                                                                                                                                                                       
  23.  
  24. /**
  25. * Entry point for this program
  26. * @param args - not used for this program
  27. */
  28.     public static void main (String [] args) {
  29.  
  30.             double allTotalOwed = 0;
  31.             double totalOwed = 0;
  32.             displayLabInformation();
  33.             displayWelcome();
  34.             String dayOfWeek = getDayOfWeek();
  35.  
  36.             while (dayOfWeek != SENTINEL) {
  37.  
  38.                 int arrivalTime = getUserTime("Please enter the vehicle's arrival time    (HHMM) : ");
  39.                 int departTime = getUserTime("Please enter the vehicle's departure time  (HHMM) : ");
  40.                 int totalArriveMin = getTotalMinutes(arrivalTime);
  41.                 int totalDepartMin = getTotalMinutes(departTime);
  42.                 int totalParkingTime = getTotalParkingTime(arrivalTime, departTime);
  43.                 double getNumIntervals = getNumIntervals(arrivalTime, departTime);
  44.                 double rate = getRate(dayOfWeek);
  45.                 totalOwed = getTotalOwed(totalParkingTime, rate, getNumIntervals, dayOfWeek);
  46.                 displayReceipt(dayOfWeek, arrivalTime, departTime, totalParkingTime, rate, totalOwed);
  47.                 dayOfWeek = getDayOfWeek();
  48.                 allTotalOwed = allTotalOwed + totalOwed;
  49.             }
  50.  
  51.             displayFinalTotal(allTotalOwed);
  52.             displayGoodbye();
  53.                    
  54.         }
  55.    
  56.     //Displays the welcome message
  57.     public static void displayWelcome () {
  58.  
  59.         System.out.println();
  60.         System.out.println("******************************");
  61.         System.out.println();
  62.         System.out.println("     Renth Parking Gargage    ");
  63.         System.out.println();
  64.         System.out.println("******************************");
  65.         System.out.println();
  66.  
  67.     }
  68.  
  69.     /**
  70.     * Gets the day of the week
  71.     * @return - returns the day of the week (String value)
  72.     */
  73.     public static String getDayOfWeek () {
  74.        
  75.         String day1 = "Monday";
  76.         String day2 = "Tuesday";
  77.         String day3 = "Wednesday";
  78.         String day4 = "Thursday";
  79.         String day5 = "Friday";
  80.         String day6 = "Saturday";
  81.         String day7 = "Sunday";
  82.         System.out.println();
  83.         String dayOfWeek = IR4.getString("Please enter the day of the week (mon, tue, wed, thu, fri, sat, sun) or quit: ");
  84.         dayOfWeek = dayOfWeek.toLowerCase();
  85.  
  86.         while (!(dayOfWeek.startsWith("m") || dayOfWeek.startsWith("tu") || dayOfWeek.startsWith("w") ||        dayOfWeek.startsWith("th") || dayOfWeek.startsWith("f") || dayOfWeek.startsWith("sa") || dayOfWeek.startsWith("su") || dayOfWeek.startsWith("q"))) {
  87.             System.out.println();
  88.             System.out.println("Error: Invalid day of the week. Please try again!");
  89.             dayOfWeek = IR4.getString("Please enter the day of the week (mon, tue, wed, thu, fri, sat, sun) or quit: ");
  90.             System.out.println();
  91.         }
  92.  
  93.         if (dayOfWeek.startsWith("m")) {
  94.             dayOfWeek = day1;
  95.         } else if (dayOfWeek.startsWith("tu")) {
  96.             dayOfWeek = day2;
  97.         } else if (dayOfWeek.startsWith("w")) {
  98.             dayOfWeek = day3;
  99.         } else if (dayOfWeek.startsWith("th")) {
  100.             dayOfWeek = day4;
  101.         } else if (dayOfWeek.startsWith("f")) {
  102.             dayOfWeek = day5;
  103.         } else if (dayOfWeek.startsWith("sa")) {
  104.             dayOfWeek = day6;
  105.         } else if (dayOfWeek.startsWith("su")) {
  106.             dayOfWeek = day7;
  107.         } else if (dayOfWeek.startsWith("q")) {
  108.             dayOfWeek = SENTINEL;
  109.         }
  110.  
  111.         return dayOfWeek;
  112.     }
  113.  
  114.     /**
  115.     * Gets the time the user entered for arrival/departure
  116.     * @param message - Sends a message to userTime within getUserTime to display a prompt message
  117.     * @return returns an int value that is either the arrival or departure time
  118.     */
  119.  
  120.     public static int getUserTime (String message) {
  121.  
  122.         int userTime = IR4.getInteger(message);
  123.        
  124.         while (isTimeValid(userTime)) {
  125.             userTime = IR4.getInteger(message);
  126.             System.out.println();
  127.         }
  128.  
  129.         return userTime;
  130.     }
  131.  
  132.     /**
  133.     * Does the math needed to convert the user entered time into total minutes
  134.     * @param time - passes in the time the user entered from getUserTime
  135.     * @return returns an int value that is the time the user entered converted to total minutes
  136.     */
  137.  
  138.     public static int getTotalMinutes (int time) {
  139.        
  140.         int totalMin = 0;
  141.         int minuteTime = time % 100;
  142.         int hourTime = (time - minuteTime) / 100;
  143.         totalMin = (hourTime * 60) + minuteTime;
  144.         return totalMin;
  145.     }
  146.  
  147.     /**
  148.     * Does the math needed to calculate the total amount of minutes spent parking
  149.     * @param arrivalTime,departTime - passes the total time in minutes from the values the user entered for depart and arrival minutes
  150.     * @return returns an int value that is the total amount of time in minutes the car spent parked
  151.     */
  152.  
  153.     public static int getTotalParkingTime (int arrivalTime, int departTime) {
  154.        
  155.         int totalParkingTime = 0;
  156.         int totalArriveMin = getTotalMinutes(arrivalTime);
  157.         int totalDepartMin = getTotalMinutes(departTime);
  158.  
  159.         if (totalArriveMin > totalDepartMin) {
  160.             totalParkingTime = (MAX_TIME_IN_MIN - totalArriveMin) + totalDepartMin;
  161.         } else {
  162.             totalParkingTime = totalDepartMin - totalArriveMin;
  163.         }
  164.  
  165.         return totalParkingTime;
  166.     }
  167.  
  168.     /**
  169.     * Gets the number of intervals the car spent parked
  170.     * @param arrivalTime,departTime - passes in the total time in minutes for the arrival and departure time
  171.     * @return returns a double value that is the total amount of intervals the car stayed parked
  172.     */
  173.  
  174.     public static double getNumIntervals (int arrivalTime, int departTime) {
  175.  
  176.         double numIntervals = 0;
  177.         int totalParkingTime = getTotalParkingTime(arrivalTime, departTime);
  178.         numIntervals = totalParkingTime / INTERVAL_TIME;
  179.         return numIntervals;
  180.     }
  181.  
  182.     /**
  183.     * Calculates the rate based on what day of the week it is
  184.     * @param getDayOfWeek - passes in the day of the week the user entered
  185.     * @return returns a double value that is the rate the user is charged for based on a certain day
  186.     */
  187.  
  188.     public static double getRate (String getDayOfWeek) {
  189.  
  190.         String day1 = "Monday";
  191.         String day2 = "Tuesday";
  192.         String day3 = "Wednesday";
  193.         String day4 = "Thursday";
  194.         String day5 = "Friday";
  195.         String day6 = "Saturday";
  196.         String day7 = "Sunday";
  197.         double rate = 0;
  198.         String dayOfWeek = getDayOfWeek;
  199.  
  200.         if (dayOfWeek.equalsIgnoreCase(day1)) {
  201.             rate = RATE_MON_FRI;
  202.         } else if (dayOfWeek.equalsIgnoreCase(day2)) {
  203.             rate = RATE_MON_FRI;
  204.         } else if (dayOfWeek.equalsIgnoreCase(day3)) {
  205.             rate = RATE_MON_FRI;
  206.         } else if (dayOfWeek.equalsIgnoreCase(day4)) {
  207.             rate = RATE_MON_FRI;
  208.         } else if (dayOfWeek.equalsIgnoreCase(day5)) {
  209.             rate = RATE_MON_FRI;
  210.         } else if (dayOfWeek.equalsIgnoreCase(day6)) {
  211.             rate = RATE_SAT_SUN;
  212.         } else if (dayOfWeek.equalsIgnoreCase(day7)) {
  213.             rate = RATE_SAT_SUN;
  214.         }
  215.  
  216.         return rate;
  217.     }
  218.  
  219.     /**
  220.     * Calculates the total the user owes
  221.     * @param totalParkTime - the total amount of time in minutes the user spent parked
  222.     * @param rate - the rate used for the period their car was parked
  223.     * @param numIntervals - the total number of intervals of 15 minutes over the span of the total parking time
  224.     * @param dayOfWeek - the day of the week the car was parked
  225.     * @return - returns a double value that is the total amount of money owed
  226.     */
  227.  
  228.     public static double getTotalOwed (int totalParkTime, double rate, double numIntervals, String dayOfWeek) {
  229.  
  230.         String day1 = "Monday";
  231.         String day2 = "Tuesday";
  232.         String day3 = "Wednesday";
  233.         String day4 = "Thursday";
  234.         String day5 = "Friday";
  235.         String day6 = "Saturday";
  236.         String day7 = "Sunday";
  237.         double remainder = 0;
  238.         double totalOwed = 0;
  239.  
  240.         double floorInterval = 0;
  241.  
  242.         if (totalParkTime < INTERVAL_TIME) {
  243.             totalOwed = 0;
  244.         } else if (numIntervals % 100 > 0) {
  245.             numIntervals = Math.ceil(numIntervals);
  246.         }
  247.  
  248.         totalOwed = rate * numIntervals;
  249.  
  250.         if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day1)) {
  251.             totalOwed = MAX_MON_FRI;
  252.         } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day2)) {
  253.             totalOwed = MAX_MON_FRI;
  254.         } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day3)) {
  255.             totalOwed = MAX_MON_FRI;
  256.         } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day4)) {
  257.             totalOwed = MAX_MON_FRI;
  258.         } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day5)) {
  259.             totalOwed = MAX_MON_FRI;
  260.         } else if (totalOwed > MAX_SAT_SUN && dayOfWeek.equals(day6)) {
  261.             totalOwed = MAX_SAT_SUN;
  262.         } else if (totalOwed > MAX_SAT_SUN && dayOfWeek.equals(day7)) {
  263.             totalOwed = MAX_SAT_SUN;
  264.         } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day1) && totalParkTime >= INTERVAL_TIME) {
  265.             totalOwed = MIN_MON_FRI;
  266.         } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day2) && totalParkTime >= INTERVAL_TIME) {
  267.             totalOwed = MIN_MON_FRI;
  268.         } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day3) && totalParkTime >= INTERVAL_TIME) {
  269.             totalOwed = MIN_MON_FRI;
  270.         } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day4) && totalParkTime >= INTERVAL_TIME) {
  271.             totalOwed = MIN_MON_FRI;
  272.         } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day5) && totalParkTime >= INTERVAL_TIME) {
  273.             totalOwed = MIN_MON_FRI;
  274.         } else if (totalOwed < MIN_SAT_SUN && dayOfWeek.equals(day6) && totalParkTime >= INTERVAL_TIME) {
  275.             totalOwed = MIN_SAT_SUN;
  276.         } else if (totalOwed < MIN_SAT_SUN && dayOfWeek.equals(day7) && totalParkTime >= INTERVAL_TIME) {
  277.             totalOwed = MIN_SAT_SUN;
  278.         } else if (totalParkTime < INTERVAL_TIME) {
  279.             totalOwed = 0.0;
  280.         }
  281.  
  282.         return totalOwed;
  283.     }
  284.  
  285.     /**
  286.     * Validates whether the user entered time is valid or invalid
  287.     * @param time - the variable passed in for the user entered arrival/departure time
  288.     * @return - returns a boolean is the time is valid or invalid
  289.     */
  290.    
  291.     public static boolean isTimeValid (int time) {
  292.  
  293.         if (time > MAX_TIME) {
  294.             System.out.println("Error: Invalid time entry. Entered time is too high. Please try again!");
  295.             System.out.println();
  296.             return true;
  297.         }
  298.  
  299.         if (time % 100 > 59) {
  300.             System.out.println("Error: Please enter the arrival time as standard 24 hour time input!");
  301.             System.out.println();
  302.             return true;
  303.         }
  304.  
  305.         if (time < MIN_TIME) {
  306.             System.out.println("Error: Invalid time entry, Entered time is too low. Please try again!");
  307.             System.out.println();
  308.             return true;
  309.         }
  310.  
  311.         return false;
  312.     }
  313.  
  314.     //Displays the Lab information
  315.     public static void displayLabInformation () {
  316.        
  317.         System.out.println();
  318.         System.out.println("Welcome! This Program allows the user to enter");
  319.         System.out.println("a day of the week and then an arrival");
  320.         System.out.println("time and finally a departure time. The");
  321.         System.out.println("program then calculates the final cost");
  322.         System.out.println("for parking there during the duration");
  323.         System.out.println("the user entered and shows a receipt!");
  324.     }
  325.  
  326.     /**
  327.     * Displays the receipt for the user entered info for the parking garage
  328.     * @param dayOfWeek - User entered day of the week
  329.     * @param arrivalTime - User entered time the car arrived
  330.     * @param departTime - User entered time the car departed
  331.     * @param totalParkingTime - Total time spent parked in minutes
  332.     * @param rate - Rate used for a certain time of the week for the parking gargae
  333.     * @param amountCharged - Total amount charged for the user's parking duration
  334.     */
  335.  
  336.     public static void displayReceipt (String dayOfWeek, int arrivalTime, int departTime, int totalParkingTime, double rate, double amountCharged) {
  337.  
  338.         System.out.println();
  339.         System.out.println("**************************************");
  340.         System.out.println();
  341.         System.out.println("        Renth Parking Gargage    ");
  342.         System.out.println("            Sales Receipt        ");
  343.         System.out.println();
  344.         System.out.println("**************************************");
  345.         System.out.println("Day of Week: " + dayOfWeek);
  346.         System.out.println("Rate: $" + rate + " per 15 minute interval");
  347.         System.out.println();
  348.         System.out.printf("%-19s%5d%n","Arrival Time:", arrivalTime);
  349.         System.out.printf("%-19s%5d%n","Departure Time:", departTime);
  350.         System.out.printf("%-19s%5d%s%n%n","Parking Duration:", totalParkingTime, " minutes");
  351.         System.out.printf("%-18s%s%.2f%n", "Amount Charged: ","$", amountCharged);
  352.         System.out.println("***************************************");
  353.     }
  354.  
  355.     /**
  356.     * Calculates the running total owed of parking charges
  357.     * @param allTotalOwed - variable that holds the total charge for every purchase made.
  358.     */
  359.    
  360.     public static void displayFinalTotal (double allTotalOwed) {
  361.        
  362.         System.out.println();
  363.         System.out.printf("%s%.2f" , "Total Charged Today: $" , allTotalOwed);
  364.         System.out.println();
  365.     }
  366.  
  367.     //Displays the goodbye message
  368.     public static void displayGoodbye() {
  369.  
  370.         System.out.println();
  371.         System.out.println("----Program Terminating----");
  372.         System.out.println("          Goodbye!");
  373.         System.out.println();
  374.     }
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement