Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.util.*;
- /**
- * @author - Kurtis Renth
- * @version - CIS 131 Lab 1
- */
- public class CIS131Lab1 {
- //constants
- final static double RATE_MON_FRI = 1.25;
- final static double RATE_SAT_SUN = 0.50;
- final static double MIN_MON_FRI = 3.00;
- final static double MIN_SAT_SUN = 2.00;
- final static double MAX_MON_FRI = 15.00;
- final static double MAX_SAT_SUN = 10.00;
- final static int MAX_TIME = 2359;
- final static int MIN_TIME = 0;
- final static double INTERVAL_TIME = 15.0;
- final static String SENTINEL = "quit";
- final static int MAX_TIME_IN_MIN = 1440;
- /**
- * Entry point for this program
- * @param args - not used for this program
- */
- public static void main (String [] args) {
- double allTotalOwed = 0;
- double totalOwed = 0;
- displayLabInformation();
- displayWelcome();
- String dayOfWeek = getDayOfWeek();
- while (dayOfWeek != SENTINEL) {
- int arrivalTime = getUserTime("Please enter the vehicle's arrival time (HHMM) : ");
- int departTime = getUserTime("Please enter the vehicle's departure time (HHMM) : ");
- int totalArriveMin = getTotalMinutes(arrivalTime);
- int totalDepartMin = getTotalMinutes(departTime);
- int totalParkingTime = getTotalParkingTime(arrivalTime, departTime);
- double getNumIntervals = getNumIntervals(arrivalTime, departTime);
- double rate = getRate(dayOfWeek);
- totalOwed = getTotalOwed(totalParkingTime, rate, getNumIntervals, dayOfWeek);
- displayReceipt(dayOfWeek, arrivalTime, departTime, totalParkingTime, rate, totalOwed);
- dayOfWeek = getDayOfWeek();
- allTotalOwed = allTotalOwed + totalOwed;
- }
- displayFinalTotal(allTotalOwed);
- displayGoodbye();
- }
- //Displays the welcome message
- public static void displayWelcome () {
- System.out.println();
- System.out.println("******************************");
- System.out.println();
- System.out.println(" Renth Parking Gargage ");
- System.out.println();
- System.out.println("******************************");
- System.out.println();
- }
- /**
- * Gets the day of the week
- * @return - returns the day of the week (String value)
- */
- public static String getDayOfWeek () {
- String day1 = "Monday";
- String day2 = "Tuesday";
- String day3 = "Wednesday";
- String day4 = "Thursday";
- String day5 = "Friday";
- String day6 = "Saturday";
- String day7 = "Sunday";
- System.out.println();
- String dayOfWeek = IR4.getString("Please enter the day of the week (mon, tue, wed, thu, fri, sat, sun) or quit: ");
- dayOfWeek = dayOfWeek.toLowerCase();
- 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"))) {
- System.out.println();
- System.out.println("Error: Invalid day of the week. Please try again!");
- dayOfWeek = IR4.getString("Please enter the day of the week (mon, tue, wed, thu, fri, sat, sun) or quit: ");
- System.out.println();
- }
- if (dayOfWeek.startsWith("m")) {
- dayOfWeek = day1;
- } else if (dayOfWeek.startsWith("tu")) {
- dayOfWeek = day2;
- } else if (dayOfWeek.startsWith("w")) {
- dayOfWeek = day3;
- } else if (dayOfWeek.startsWith("th")) {
- dayOfWeek = day4;
- } else if (dayOfWeek.startsWith("f")) {
- dayOfWeek = day5;
- } else if (dayOfWeek.startsWith("sa")) {
- dayOfWeek = day6;
- } else if (dayOfWeek.startsWith("su")) {
- dayOfWeek = day7;
- } else if (dayOfWeek.startsWith("q")) {
- dayOfWeek = SENTINEL;
- }
- return dayOfWeek;
- }
- /**
- * Gets the time the user entered for arrival/departure
- * @param message - Sends a message to userTime within getUserTime to display a prompt message
- * @return returns an int value that is either the arrival or departure time
- */
- public static int getUserTime (String message) {
- int userTime = IR4.getInteger(message);
- while (isTimeValid(userTime)) {
- userTime = IR4.getInteger(message);
- System.out.println();
- }
- return userTime;
- }
- /**
- * Does the math needed to convert the user entered time into total minutes
- * @param time - passes in the time the user entered from getUserTime
- * @return returns an int value that is the time the user entered converted to total minutes
- */
- public static int getTotalMinutes (int time) {
- int totalMin = 0;
- int minuteTime = time % 100;
- int hourTime = (time - minuteTime) / 100;
- totalMin = (hourTime * 60) + minuteTime;
- return totalMin;
- }
- /**
- * Does the math needed to calculate the total amount of minutes spent parking
- * @param arrivalTime,departTime - passes the total time in minutes from the values the user entered for depart and arrival minutes
- * @return returns an int value that is the total amount of time in minutes the car spent parked
- */
- public static int getTotalParkingTime (int arrivalTime, int departTime) {
- int totalParkingTime = 0;
- int totalArriveMin = getTotalMinutes(arrivalTime);
- int totalDepartMin = getTotalMinutes(departTime);
- if (totalArriveMin > totalDepartMin) {
- totalParkingTime = (MAX_TIME_IN_MIN - totalArriveMin) + totalDepartMin;
- } else {
- totalParkingTime = totalDepartMin - totalArriveMin;
- }
- return totalParkingTime;
- }
- /**
- * Gets the number of intervals the car spent parked
- * @param arrivalTime,departTime - passes in the total time in minutes for the arrival and departure time
- * @return returns a double value that is the total amount of intervals the car stayed parked
- */
- public static double getNumIntervals (int arrivalTime, int departTime) {
- double numIntervals = 0;
- int totalParkingTime = getTotalParkingTime(arrivalTime, departTime);
- numIntervals = totalParkingTime / INTERVAL_TIME;
- return numIntervals;
- }
- /**
- * Calculates the rate based on what day of the week it is
- * @param getDayOfWeek - passes in the day of the week the user entered
- * @return returns a double value that is the rate the user is charged for based on a certain day
- */
- public static double getRate (String getDayOfWeek) {
- String day1 = "Monday";
- String day2 = "Tuesday";
- String day3 = "Wednesday";
- String day4 = "Thursday";
- String day5 = "Friday";
- String day6 = "Saturday";
- String day7 = "Sunday";
- double rate = 0;
- String dayOfWeek = getDayOfWeek;
- if (dayOfWeek.equalsIgnoreCase(day1)) {
- rate = RATE_MON_FRI;
- } else if (dayOfWeek.equalsIgnoreCase(day2)) {
- rate = RATE_MON_FRI;
- } else if (dayOfWeek.equalsIgnoreCase(day3)) {
- rate = RATE_MON_FRI;
- } else if (dayOfWeek.equalsIgnoreCase(day4)) {
- rate = RATE_MON_FRI;
- } else if (dayOfWeek.equalsIgnoreCase(day5)) {
- rate = RATE_MON_FRI;
- } else if (dayOfWeek.equalsIgnoreCase(day6)) {
- rate = RATE_SAT_SUN;
- } else if (dayOfWeek.equalsIgnoreCase(day7)) {
- rate = RATE_SAT_SUN;
- }
- return rate;
- }
- /**
- * Calculates the total the user owes
- * @param totalParkTime - the total amount of time in minutes the user spent parked
- * @param rate - the rate used for the period their car was parked
- * @param numIntervals - the total number of intervals of 15 minutes over the span of the total parking time
- * @param dayOfWeek - the day of the week the car was parked
- * @return - returns a double value that is the total amount of money owed
- */
- public static double getTotalOwed (int totalParkTime, double rate, double numIntervals, String dayOfWeek) {
- String day1 = "Monday";
- String day2 = "Tuesday";
- String day3 = "Wednesday";
- String day4 = "Thursday";
- String day5 = "Friday";
- String day6 = "Saturday";
- String day7 = "Sunday";
- double remainder = 0;
- double totalOwed = 0;
- double floorInterval = 0;
- if (totalParkTime < INTERVAL_TIME) {
- totalOwed = 0;
- } else if (numIntervals % 100 > 0) {
- numIntervals = Math.ceil(numIntervals);
- }
- totalOwed = rate * numIntervals;
- if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day1)) {
- totalOwed = MAX_MON_FRI;
- } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day2)) {
- totalOwed = MAX_MON_FRI;
- } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day3)) {
- totalOwed = MAX_MON_FRI;
- } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day4)) {
- totalOwed = MAX_MON_FRI;
- } else if (totalOwed > MAX_MON_FRI && dayOfWeek.equals(day5)) {
- totalOwed = MAX_MON_FRI;
- } else if (totalOwed > MAX_SAT_SUN && dayOfWeek.equals(day6)) {
- totalOwed = MAX_SAT_SUN;
- } else if (totalOwed > MAX_SAT_SUN && dayOfWeek.equals(day7)) {
- totalOwed = MAX_SAT_SUN;
- } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day1) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_MON_FRI;
- } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day2) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_MON_FRI;
- } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day3) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_MON_FRI;
- } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day4) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_MON_FRI;
- } else if (totalOwed < MIN_MON_FRI && dayOfWeek.equals(day5) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_MON_FRI;
- } else if (totalOwed < MIN_SAT_SUN && dayOfWeek.equals(day6) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_SAT_SUN;
- } else if (totalOwed < MIN_SAT_SUN && dayOfWeek.equals(day7) && totalParkTime >= INTERVAL_TIME) {
- totalOwed = MIN_SAT_SUN;
- } else if (totalParkTime < INTERVAL_TIME) {
- totalOwed = 0.0;
- }
- return totalOwed;
- }
- /**
- * Validates whether the user entered time is valid or invalid
- * @param time - the variable passed in for the user entered arrival/departure time
- * @return - returns a boolean is the time is valid or invalid
- */
- public static boolean isTimeValid (int time) {
- if (time > MAX_TIME) {
- System.out.println("Error: Invalid time entry. Entered time is too high. Please try again!");
- System.out.println();
- return true;
- }
- if (time % 100 > 59) {
- System.out.println("Error: Please enter the arrival time as standard 24 hour time input!");
- System.out.println();
- return true;
- }
- if (time < MIN_TIME) {
- System.out.println("Error: Invalid time entry, Entered time is too low. Please try again!");
- System.out.println();
- return true;
- }
- return false;
- }
- //Displays the Lab information
- public static void displayLabInformation () {
- System.out.println();
- System.out.println("Welcome! This Program allows the user to enter");
- System.out.println("a day of the week and then an arrival");
- System.out.println("time and finally a departure time. The");
- System.out.println("program then calculates the final cost");
- System.out.println("for parking there during the duration");
- System.out.println("the user entered and shows a receipt!");
- }
- /**
- * Displays the receipt for the user entered info for the parking garage
- * @param dayOfWeek - User entered day of the week
- * @param arrivalTime - User entered time the car arrived
- * @param departTime - User entered time the car departed
- * @param totalParkingTime - Total time spent parked in minutes
- * @param rate - Rate used for a certain time of the week for the parking gargae
- * @param amountCharged - Total amount charged for the user's parking duration
- */
- public static void displayReceipt (String dayOfWeek, int arrivalTime, int departTime, int totalParkingTime, double rate, double amountCharged) {
- System.out.println();
- System.out.println("**************************************");
- System.out.println();
- System.out.println(" Renth Parking Gargage ");
- System.out.println(" Sales Receipt ");
- System.out.println();
- System.out.println("**************************************");
- System.out.println("Day of Week: " + dayOfWeek);
- System.out.println("Rate: $" + rate + " per 15 minute interval");
- System.out.println();
- System.out.printf("%-19s%5d%n","Arrival Time:", arrivalTime);
- System.out.printf("%-19s%5d%n","Departure Time:", departTime);
- System.out.printf("%-19s%5d%s%n%n","Parking Duration:", totalParkingTime, " minutes");
- System.out.printf("%-18s%s%.2f%n", "Amount Charged: ","$", amountCharged);
- System.out.println("***************************************");
- }
- /**
- * Calculates the running total owed of parking charges
- * @param allTotalOwed - variable that holds the total charge for every purchase made.
- */
- public static void displayFinalTotal (double allTotalOwed) {
- System.out.println();
- System.out.printf("%s%.2f" , "Total Charged Today: $" , allTotalOwed);
- System.out.println();
- }
- //Displays the goodbye message
- public static void displayGoodbye() {
- System.out.println();
- System.out.println("----Program Terminating----");
- System.out.println(" Goodbye!");
- System.out.println();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement