Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //CUS 1156 Assignment#2 Parking Garage by Nicole Kim and Wilson Zhao
- import java.util.*;
- import java.text.ParseException;
- import java.text.SimpleDateFormat;
- public class ParkingGarageTester
- {
- //instance variables
- final static int TOTAL_SPACES = 50;
- final static int DAYS = 7;
- final static int HOURS = 24;
- static ParkingSpace[] garage = new ParkingSpace[TOTAL_SPACES];
- static Scanner input = new Scanner(System.in);
- static Date start = new Date();
- static Date end = new Date();
- static Date currentTime = new Date();
- static SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yyyy HH:mm");
- public static void main(String[] args)
- {
- //initialize the array of 50 parking spaces
- for (int i = 0; i < TOTAL_SPACES; i++)
- {
- garage[i] = new ParkingSpace();
- }
- menu();
- }
- //provides main menu of the program
- public static void menu()
- {
- int choice = 0;
- menuDisplayText();
- System.out.println("Please enter a choice (1-4).");
- choice = input.nextInt();
- input.nextLine();
- while (choice != 4)
- {
- switch (choice)
- {
- case 1:
- addReservation();
- break;
- case 2:
- modifyReservation();
- break;
- case 3:
- extendReservation();
- break;
- case 4:
- System.out.println("Exiting program");
- break;
- default:
- System.out.println("Invalid choice, please select a number from 1-4");
- menu();
- break;
- }
- }
- }
- //creates a reservation from a date supplied by user, adds to garage[].reservations for that parking space #
- public static void addReservation()
- {
- int spaceNum = 0;
- System.out.println("You have chosen to add a reservation.");
- System.out.println("Please select a parking space # to add a reservation to (1-50)");
- while (!(spaceNum > 0 && spaceNum <= 50))
- {
- spaceNum = input.nextInt();
- input.nextLine();
- if (!(spaceNum > 0 && spaceNum <= 50))
- {
- System.out.println("Error, you must enter a number between 1-50.");
- }
- }
- System.out.println("You have chosen to add a reservation to parking space #" + spaceNum);
- //check for overlap
- setStartingTime();
- setEndingTime();
- if (checkOverlap(garage, (spaceNum-1), start, end) == false)
- {
- Reservation res = new Reservation(start, end);
- garage[spaceNum-1].addRes(res);
- System.out.println("You have successfully created a reservation for the following time: " + start + " to " + end);
- }
- else
- {
- System.out.println("Error, that parking space is in use for that time period already.");
- }
- updateStatus();
- menu();
- }
- //gets a valid starting time for a reservation from a user, if any exceptions will call itself again
- public static void setStartingTime()
- {
- String startingTime = "";
- try
- {
- System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
- System.out.println("Example: 03/31/2015 13:00 = March 31, 2015 1 PM");
- do
- {
- startingTime = input.nextLine();
- start = dt.parse(startingTime);
- if(start.before(currentTime))
- {
- System.out.println("Invalid start time, must be after current date and time: " + currentTime);
- System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
- }
- }while(!(start.after(currentTime)));
- System.out.println("You have chosen " + start + " as the starting time for your reservation.");
- }
- catch (ParseException e)
- {
- System.out.println("Error, invalid parse from String to Date.");
- setStartingTime();
- }
- }
- //gets a valid ending time for a reservation from a user, if any exceptions will call itself again
- public static void setEndingTime()
- {
- String endingTime = "";
- try
- {
- System.out.println("Please enter in the ending time for your reservation in MM/DD/YYYY HH:MM format");
- System.out.println("Example: 03/31/2015 14:00 = March 31, 2015 2 PM");
- do
- {
- endingTime = input.nextLine();
- end = dt.parse(endingTime);
- if(end.before(start))
- {
- System.out.println("Invalid end time, must be after start date and time: " + start);
- System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
- }
- }while(!(end.after(start)));
- System.out.println("You have chosen " + end + " as the ending time for your reservation.");
- }
- catch (ParseException e)
- {
- System.out.println("Error, invalid parse from String to Date.");
- setEndingTime();
- }
- }
- //checks to see if there will be any overlap conflict with reservations, returns true if there is, false if not
- public static boolean checkOverlap(ParkingSpace [] p, int index, Date s, Date e)
- {
- ParkingSpace currentSpace = p[index];
- Calendar calStart = Calendar.getInstance();
- Calendar calEnd = Calendar.getInstance();
- calStart.setTime(start);
- calEnd.setTime(end);
- int startDay = calStart.get(Calendar.DAY_OF_WEEK);
- int startHour = calStart.get(Calendar.HOUR_OF_DAY);
- int endDay = calEnd.get(Calendar.DAY_OF_WEEK);
- int endHour = calEnd.get(Calendar.HOUR_OF_DAY);
- if (!(p[index].getReservations().isEmpty()))
- {
- for (int i = 0; i < p[index].getReservations().size(); i++)
- {
- if(currentSpace.getState(startDay, startHour).equals("Available") == false)
- {
- return true;
- }
- if(currentSpace.getState(endDay, endHour).equals("Available") == false)
- {
- return true;
- }
- }
- }
- return false;
- }
- //allows a user to modify a reservation on a parking space if there is one, can also cancel an existing reservation
- public static void modifyReservation()
- {
- int spaceNum = 0;
- int resNum = 0;
- int modifyOption = 0;
- ParkingSpace pSpace;
- System.out.println("You have chosen to modify a reservation on a parking space.");
- System.out.println("Displaying all reservations:");
- for (int i = 0; i < TOTAL_SPACES; i++)
- {
- if (garage[i].getReservations().isEmpty() == false)
- {
- System.out.print("Parking Space #" + (i+1) + "\t");
- garage[i].displayReservations();
- System.out.println();
- }
- }
- System.out.println("Please select a parking space # to modify a reservation for (1-50)");
- while (!(spaceNum > 0 && spaceNum <= 50))
- {
- spaceNum = input.nextInt();
- input.nextLine();
- if (!(spaceNum > 0 && spaceNum <= 50))
- {
- System.out.println("Error, you must enter a number between 1-50.");
- }
- }
- if (garage[spaceNum-1].getReservations().isEmpty())
- {
- System.out.println("Error, there are no reservations created for this parking space.");
- }
- else
- {
- System.out.println("Displaying all reservations for parking space #" + spaceNum);
- garage[spaceNum-1].displayReservations();
- System.out.println("Please select a reservation # to modify:");
- while ((resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()))== false)
- {
- resNum = input.nextInt();
- input.nextLine();
- if ((resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size())) == false)
- {
- System.out.println("Error, you must enter a number between 1 and " + (garage[spaceNum].getReservations().size()+1));
- }
- }
- pSpace = garage[spaceNum-1];
- modifyDisplayText();
- modifyOption = input.nextInt();
- input.nextLine();
- while (modifyOption != 4)
- {
- switch (modifyOption)
- {
- case 1:
- pSpace.getReservations().remove(resNum-1);
- System.out.println("You have successfully removed reservation #" + resNum + " for parking space #" + spaceNum);
- break;
- case 2:
- System.out.println("You have chosen to modify the starting time for a reservation.");
- setStartingTime();
- garage[spaceNum-1].getReservations().get(resNum-1).setStartTime(start);
- if(start.after(garage[spaceNum-1].getReservations().get(resNum-1).getEndTime()))
- {
- System.out.println("You have changed the starting time to a time that is now after the current end time");
- System.out.println("You must now change the ending time to a new valid time after the new start time.");
- setEndingTime();
- garage[spaceNum-1].getReservations().get(resNum-1).setEndTime(end);
- }
- updateStatus();
- break;
- case 3:
- System.out.println("You have chosen to modify the ending time for a reservation.");
- setEndingTime();
- if(end.before(garage[spaceNum-1].getReservations().get(resNum-1).getStartTime()))
- {
- System.out.println("You have changed the ending time to a time that is now before the current start time");
- System.out.println("You must now change the starting time to a new valid time before the new end time.");
- setStartingTime();
- garage[spaceNum-1].getReservations().get(resNum-1).setStartTime(start);
- }
- updateStatus();
- break;
- case 4:
- menu();
- break;
- default:
- System.out.println("Invalid choice, please select a number from 1-4");
- modifyOption = input.nextInt();
- input.nextLine();
- break;
- }
- }
- }
- menu();
- }
- //allows the user to select a reservation to try and extend it,
- //if the current time is still before the end time of the reservation, allows user to extend the reservation
- public static void extendReservation()
- {
- int spaceNum = 0;
- int resNum = 0;
- Date endTime;
- Reservation currentRes;
- System.out.println("You have chosen to extend an occupied reservation.");
- System.out.println("Displaying occupied reservations...");
- for (int i = 0; i < TOTAL_SPACES; i++)
- {
- if (garage[i].getReservations().isEmpty() == false)
- {
- System.out.print("Parking Space #" + (i+1) + "\t");
- garage[i].displayReservations();
- System.out.println();
- }
- }
- System.out.println("Please enter the parking space # of the reservation you want to extend:");
- while((spaceNum > 0 && spaceNum <= 50) == false || garage[spaceNum-1].getReservations().isEmpty())
- {
- spaceNum = input.nextInt();
- input.nextLine();
- if (!(spaceNum > 0 && spaceNum <= 50))
- {
- System.out.println("Error, you must enter a number between 1-50.");
- }
- if(garage[spaceNum-1].getReservations().isEmpty())
- {
- System.out.println("Invalid parking space #, no reservations listed.");
- System.out.println("Please choose a parking space that contains reservations");
- }
- }
- garage[spaceNum-1].displayReservations();
- System.out.println("Please select a reservation # to extend:");
- while (!(resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()+1)))
- {
- resNum = input.nextInt();
- input.nextLine();
- if (!(resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()+1)))
- {
- System.out.println("Error, you must enter a number between 1 and " + (garage[spaceNum].getReservations().size()+1));
- }
- }
- System.out.println("You have selected to attempt to extend Parking Space #" + spaceNum + ", reservation # " + resNum);
- currentRes = garage[spaceNum-1].getReservations().get(resNum-1);
- endTime = currentRes.getEndTime();
- if (currentTime.before(endTime))
- {
- System.out.println("Your current time is still before the end time of your reservation so you may extend it.");
- setEndingTime();
- currentRes.setEndTime(end);
- }
- else
- {
- System.out.println("Your current time is past the end time of your reservation, you may not extend it");
- }
- updateStatus();
- menu();
- }
- //method to constantly update the status of Parking Spaces in the garage from Available/Reserved/Occupied
- public static void updateStatus()
- {
- int startHour;
- int endHour;
- int startDay;
- Reservation currentRes;
- ParkingSpace currentSpace;
- //updates all 50 parking spaces
- for (int i = 0; i < TOTAL_SPACES; i++)
- {
- //if the parking space has reservations, update the status to either reserved or occupied
- if (!(garage[i].getReservations().isEmpty()))
- {
- for (int r = 0; r < garage[i].getReservations().size(); r++)
- {
- currentRes = garage[i].getReservations().get(r);
- currentSpace = garage[i];
- startHour = currentRes.getStartHour();
- endHour = currentRes.getEndHour();
- startDay = currentRes.getStartDay();
- for (int k = startHour; k <= endHour; k++)
- {
- if(currentRes.getStartTime().after(currentTime))
- {
- currentSpace.setOccupied(startDay, k);
- }
- else
- {
- currentSpace.setReserved(startDay, k);
- }
- }
- }
- }
- //if the parking space is empty of reservations, reset all status to available
- else
- {
- for(int d = 0; d < DAYS; d++ )
- {
- for (int h = 0; h < HOURS; h++)
- {
- garage[i].setAvailable(d, h);
- }
- }
- }
- }
- }
- public static void menuDisplayText()
- {
- System.out.println("Main Menu:");
- System.out.println("Option 1: Reserve a parking space:");
- System.out.println("Option 2: Modify existing reservation:");
- System.out.println("Option 3: Extend an occupied reservation:");
- System.out.println("Option 4: Exit the program");
- }
- public static void modifyDisplayText()
- {
- System.out.println("What would you like to do for this reservation?");
- System.out.println("Option 1: Cancel this reservation");
- System.out.println("Option 2: Change the start time for this reservation");
- System.out.println("Option 3: Change the end time for this reservation");
- System.out.println("Option 4: Return to main menu");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement