Advertisement
Guest User

Untitled

a guest
Mar 31st, 2015
219
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 13.57 KB | None | 0 0
  1. //CUS 1156 Assignment#2 Parking Garage by Nicole Kim and Wilson Zhao
  2.  
  3. import java.util.*;
  4. import java.text.ParseException;
  5. import java.text.SimpleDateFormat;
  6.  
  7. public class ParkingGarageTester
  8. {
  9.    
  10.     //instance variables
  11.     final static int TOTAL_SPACES = 50;
  12.     final static int DAYS = 7;
  13.     final static int HOURS = 24;
  14.     static ParkingSpace[] garage = new ParkingSpace[TOTAL_SPACES];
  15.     static Scanner input = new Scanner(System.in);
  16.     static Date start = new Date();
  17.     static Date end = new Date();
  18.     static Date currentTime = new Date();
  19.     static SimpleDateFormat dt = new SimpleDateFormat("MM/dd/yyyy HH:mm");
  20.  
  21.     public static void main(String[] args)
  22.     {
  23.        
  24.         //initialize the array of 50 parking spaces
  25.         for (int i = 0; i < TOTAL_SPACES; i++)
  26.         {
  27.             garage[i] = new ParkingSpace();
  28.         }
  29.        
  30.         menu();
  31.     }
  32.    
  33.     //provides main menu of the program
  34.     public static void menu()
  35.     {
  36.         int choice = 0;
  37.        
  38.         menuDisplayText();
  39.         System.out.println("Please enter a choice (1-4).");
  40.         choice = input.nextInt();
  41.         input.nextLine();
  42.        
  43.         while (choice != 4)
  44.         {
  45.             switch (choice)
  46.             {
  47.                 case 1:
  48.                     addReservation();
  49.                     break;
  50.                    
  51.                 case 2:
  52.                     modifyReservation();
  53.                     break;
  54.                    
  55.                 case 3:
  56.                     extendReservation();
  57.                     break;
  58.                    
  59.                 case 4:
  60.                     System.out.println("Exiting program");
  61.                     break;
  62.                    
  63.                 default:
  64.                     System.out.println("Invalid choice, please select a number from 1-4");
  65.                     menu();
  66.                     break;
  67.             }
  68.         }
  69.     }
  70.    
  71.     //creates a reservation from a date supplied by user, adds to garage[].reservations for that parking space #
  72.     public static void addReservation()
  73.     {
  74.         int spaceNum = 0;
  75.  
  76.         System.out.println("You have chosen to add a reservation.");
  77.         System.out.println("Please select a parking space # to add a reservation to (1-50)");
  78.         while (!(spaceNum > 0 && spaceNum <= 50))
  79.         {
  80.             spaceNum = input.nextInt();
  81.             input.nextLine();
  82.             if (!(spaceNum > 0 && spaceNum <= 50))
  83.             {
  84.                 System.out.println("Error, you must enter a number between 1-50.");
  85.             }
  86.         }
  87.        
  88.         System.out.println("You have chosen to add a reservation to parking space #" + spaceNum);
  89.         //check for overlap
  90.        
  91.         setStartingTime();
  92.         setEndingTime();
  93.         if (checkOverlap(garage, (spaceNum-1), start, end) == false)
  94.         {
  95.             Reservation res = new Reservation(start, end);
  96.             garage[spaceNum-1].addRes(res);
  97.             System.out.println("You have successfully created a reservation for the following time: " + start + " to " + end);
  98.         }
  99.        
  100.         else
  101.         {
  102.             System.out.println("Error, that parking space is in use for that time period already.");
  103.         }
  104.         updateStatus();
  105.         menu();
  106.     }
  107.    
  108.     //gets a valid starting time for a reservation from a user, if any exceptions will call itself again
  109.     public static void setStartingTime()
  110.     {
  111.         String startingTime = "";
  112.        
  113.         try
  114.         {
  115.             System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
  116.             System.out.println("Example: 03/31/2015 13:00 = March 31, 2015 1 PM");
  117.            
  118.             do
  119.             {
  120.                 startingTime = input.nextLine();
  121.                 start = dt.parse(startingTime);
  122.                
  123.                 if(start.before(currentTime))
  124.                 {
  125.                     System.out.println("Invalid start time, must be after current date and time: " + currentTime);
  126.                     System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
  127.                 }
  128.                
  129.             }while(!(start.after(currentTime)));
  130.            
  131.             System.out.println("You have chosen " + start + " as the starting time for your reservation.");
  132.         }
  133.        
  134.         catch (ParseException e)
  135.         {
  136.             System.out.println("Error, invalid parse from String to Date.");
  137.             setStartingTime();
  138.         }
  139.     }
  140.    
  141.     //gets a valid ending time for a reservation from a user, if any exceptions will call itself again
  142.     public static void setEndingTime()
  143.     {
  144.         String endingTime = "";
  145.        
  146.         try
  147.         {
  148.             System.out.println("Please enter in the ending time for your reservation in MM/DD/YYYY HH:MM format");
  149.             System.out.println("Example: 03/31/2015 14:00 = March 31, 2015 2 PM");
  150.            
  151.             do
  152.             {
  153.                 endingTime = input.nextLine();
  154.                 end = dt.parse(endingTime);
  155.                
  156.                 if(end.before(start))
  157.                 {
  158.                     System.out.println("Invalid end time, must be after start date and time: " + start);
  159.                     System.out.println("Please enter in the starting time for your reservation in MM/DD/YYYY HH:MM format");
  160.                 }
  161.                
  162.             }while(!(end.after(start)));
  163.            
  164.             System.out.println("You have chosen " + end + " as the ending time for your reservation.");
  165.         }
  166.        
  167.         catch (ParseException e)
  168.         {
  169.             System.out.println("Error, invalid parse from String to Date.");
  170.             setEndingTime();
  171.         }
  172.     }
  173.    
  174.     //checks to see if there will be any overlap conflict with reservations, returns true if there is, false if not
  175.     public static boolean checkOverlap(ParkingSpace [] p, int index, Date s, Date e)
  176.     {
  177.         ParkingSpace currentSpace = p[index];
  178.         Calendar calStart = Calendar.getInstance();
  179.         Calendar calEnd = Calendar.getInstance();
  180.         calStart.setTime(start);
  181.         calEnd.setTime(end);
  182.         int startDay = calStart.get(Calendar.DAY_OF_WEEK);
  183.         int startHour = calStart.get(Calendar.HOUR_OF_DAY);
  184.         int endDay = calEnd.get(Calendar.DAY_OF_WEEK);
  185.         int endHour =  calEnd.get(Calendar.HOUR_OF_DAY);
  186.        
  187.        
  188.         if (!(p[index].getReservations().isEmpty()))
  189.         {
  190.             for (int i = 0; i < p[index].getReservations().size(); i++)
  191.             {
  192.                 if(currentSpace.getState(startDay, startHour).equals("Available") == false)
  193.                 {
  194.                     return true;
  195.                 }
  196.                
  197.                 if(currentSpace.getState(endDay, endHour).equals("Available") == false)
  198.                 {
  199.                     return true;
  200.                 }
  201.             }
  202.         }
  203.        
  204.         return false;
  205.     }
  206.    
  207.     //allows a user to modify a reservation on a parking space if there is one, can also cancel an existing reservation
  208.     public static void modifyReservation()
  209.     {
  210.         int spaceNum = 0;
  211.         int resNum = 0;
  212.         int modifyOption = 0;
  213.         ParkingSpace pSpace;
  214.        
  215.         System.out.println("You have chosen to modify a reservation on a parking space.");
  216.         System.out.println("Displaying all reservations:");
  217.  
  218.         for (int i = 0; i < TOTAL_SPACES; i++)
  219.         {
  220.             if (garage[i].getReservations().isEmpty() == false)
  221.             {
  222.                 System.out.print("Parking Space #" + (i+1) + "\t");
  223.                 garage[i].displayReservations();
  224.                 System.out.println();
  225.             }
  226.         }
  227.        
  228.         System.out.println("Please select a parking space # to modify a reservation for (1-50)");
  229.        
  230.         while (!(spaceNum > 0 && spaceNum <= 50))
  231.         {
  232.             spaceNum = input.nextInt();
  233.             input.nextLine();
  234.             if (!(spaceNum > 0 && spaceNum <= 50))
  235.             {
  236.                 System.out.println("Error, you must enter a number between 1-50.");
  237.             }
  238.         }
  239.        
  240.         if (garage[spaceNum-1].getReservations().isEmpty())
  241.         {
  242.             System.out.println("Error, there are no reservations created for this parking space.");
  243.         }
  244.        
  245.         else
  246.         {
  247.             System.out.println("Displaying all reservations for parking space #" + spaceNum);
  248.             garage[spaceNum-1].displayReservations();
  249.             System.out.println("Please select a reservation # to modify:");
  250.             while ((resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()))== false)
  251.             {
  252.                 resNum = input.nextInt();
  253.                 input.nextLine();
  254.                 if ((resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size())) == false)
  255.                 {
  256.                     System.out.println("Error, you must enter a number between 1 and " + (garage[spaceNum].getReservations().size()+1));
  257.                 }
  258.             }
  259.        
  260.             pSpace = garage[spaceNum-1];
  261.             modifyDisplayText();
  262.             modifyOption = input.nextInt();
  263.             input.nextLine();
  264.             while (modifyOption != 4)
  265.             {
  266.                 switch (modifyOption)
  267.                 {
  268.                     case 1:
  269.                         pSpace.getReservations().remove(resNum-1);
  270.                         System.out.println("You have successfully removed reservation #" + resNum + " for parking space #" + spaceNum);
  271.                         break;
  272.                        
  273.                     case 2:
  274.                         System.out.println("You have chosen to modify the starting time for a reservation.");
  275.                         setStartingTime();
  276.                         garage[spaceNum-1].getReservations().get(resNum-1).setStartTime(start);
  277.                        
  278.                         if(start.after(garage[spaceNum-1].getReservations().get(resNum-1).getEndTime()))
  279.                         {
  280.                             System.out.println("You have changed the starting time to a time that is now after the current end time");
  281.                             System.out.println("You must now change the ending time to a new valid time after the new start time.");
  282.                             setEndingTime();
  283.                             garage[spaceNum-1].getReservations().get(resNum-1).setEndTime(end);
  284.                         }
  285.                         updateStatus();
  286.                         break;
  287.                        
  288.                     case 3:
  289.                         System.out.println("You have chosen to modify the ending time for a reservation.");
  290.                         setEndingTime();
  291.                         if(end.before(garage[spaceNum-1].getReservations().get(resNum-1).getStartTime()))
  292.                         {
  293.                             System.out.println("You have changed the ending time to a time that is now before the current start time");
  294.                             System.out.println("You must now change the starting time to a new valid time before the new end time.");
  295.                             setStartingTime();
  296.                             garage[spaceNum-1].getReservations().get(resNum-1).setStartTime(start);
  297.                         }
  298.                         updateStatus();
  299.                         break;
  300.                        
  301.                     case 4:
  302.                         menu();
  303.                         break;
  304.                        
  305.                     default:
  306.                         System.out.println("Invalid choice, please select a number from 1-4");
  307.                         modifyOption = input.nextInt();
  308.                         input.nextLine();
  309.                         break;
  310.                 }
  311.             }
  312.         }
  313.        
  314.         menu();
  315.     }
  316.    
  317.     //allows the user to select a reservation to try and extend it,
  318.     //if the current time is still before the end time of the reservation, allows user to extend the reservation
  319.     public static void extendReservation()
  320.     {
  321.         int spaceNum = 0;
  322.         int resNum = 0;
  323.         Date endTime;
  324.         Reservation currentRes;
  325.        
  326.         System.out.println("You have chosen to extend an occupied reservation.");
  327.         System.out.println("Displaying occupied reservations...");
  328.        
  329.         for (int i = 0; i < TOTAL_SPACES; i++)
  330.         {
  331.             if (garage[i].getReservations().isEmpty() == false)
  332.             {
  333.                 System.out.print("Parking Space #" + (i+1) + "\t");
  334.                 garage[i].displayReservations();
  335.                 System.out.println();
  336.             }
  337.         }
  338.        
  339.         System.out.println("Please enter the parking space # of the reservation you want to extend:");
  340.         while((spaceNum > 0 && spaceNum <= 50) == false || garage[spaceNum-1].getReservations().isEmpty())
  341.         {
  342.             spaceNum = input.nextInt();
  343.             input.nextLine();
  344.             if (!(spaceNum > 0 && spaceNum <= 50))
  345.             {
  346.                 System.out.println("Error, you must enter a number between 1-50.");
  347.             }
  348.            
  349.             if(garage[spaceNum-1].getReservations().isEmpty())
  350.             {
  351.                 System.out.println("Invalid parking space #, no reservations listed.");
  352.                 System.out.println("Please choose a parking space that contains reservations");
  353.    
  354.             }
  355.         }
  356.        
  357.         garage[spaceNum-1].displayReservations();
  358.         System.out.println("Please select a reservation # to extend:");
  359.         while (!(resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()+1)))
  360.         {
  361.             resNum = input.nextInt();
  362.             input.nextLine();
  363.             if (!(resNum > 0 && resNum <= (garage[spaceNum-1].getReservations().size()+1)))
  364.             {
  365.                 System.out.println("Error, you must enter a number between 1 and " + (garage[spaceNum].getReservations().size()+1));
  366.             }
  367.         }
  368.        
  369.         System.out.println("You have selected to attempt to extend Parking Space #" + spaceNum + ", reservation # " + resNum);
  370.         currentRes = garage[spaceNum-1].getReservations().get(resNum-1);
  371.         endTime = currentRes.getEndTime();
  372.         if (currentTime.before(endTime))
  373.         {
  374.             System.out.println("Your current time is still before the end time of your reservation so you may extend it.");
  375.             setEndingTime();
  376.             currentRes.setEndTime(end);
  377.         }
  378.        
  379.         else
  380.         {
  381.             System.out.println("Your current time is past the end time of your reservation, you may not extend it");
  382.         }
  383.         updateStatus();
  384.         menu();
  385.     }
  386.    
  387.     //method to constantly update the status of Parking Spaces in the garage from Available/Reserved/Occupied
  388.     public static void updateStatus()
  389.     {
  390.         int startHour;
  391.         int endHour;
  392.         int startDay;
  393.         Reservation currentRes;
  394.         ParkingSpace currentSpace;
  395.        
  396.         //updates all 50 parking spaces
  397.         for (int i = 0; i < TOTAL_SPACES; i++)
  398.         {
  399.             //if the parking space has reservations, update the status to either reserved or occupied
  400.             if (!(garage[i].getReservations().isEmpty()))
  401.             {
  402.                 for (int r = 0; r < garage[i].getReservations().size(); r++)
  403.                 {
  404.                     currentRes = garage[i].getReservations().get(r);
  405.                     currentSpace = garage[i];
  406.                     startHour = currentRes.getStartHour();
  407.                     endHour = currentRes.getEndHour();
  408.                     startDay = currentRes.getStartDay();
  409.                    
  410.                     for (int k = startHour; k <= endHour; k++)
  411.                     {
  412.                         if(currentRes.getStartTime().after(currentTime))
  413.                         {
  414.                             currentSpace.setOccupied(startDay, k);
  415.                         }
  416.                        
  417.                         else
  418.                         {
  419.                             currentSpace.setReserved(startDay, k);
  420.                         }
  421.                     }
  422.                    
  423.                 }
  424.             }
  425.            
  426.             //if the parking space is empty of reservations, reset all status to available
  427.             else
  428.             {
  429.                 for(int d = 0; d < DAYS; d++ )
  430.                 {
  431.                     for (int h = 0; h < HOURS; h++)
  432.                     {
  433.                         garage[i].setAvailable(d, h);
  434.                     }
  435.                 }
  436.             }
  437.         }
  438.     }
  439.    
  440.     public static void menuDisplayText()
  441.     {
  442.         System.out.println("Main Menu:");
  443.         System.out.println("Option 1: Reserve a parking space:");
  444.         System.out.println("Option 2: Modify existing reservation:");
  445.         System.out.println("Option 3: Extend an occupied reservation:");
  446.         System.out.println("Option 4: Exit the program");
  447.     }
  448.    
  449.     public static void modifyDisplayText()
  450.     {
  451.         System.out.println("What would you like to do for this reservation?");
  452.         System.out.println("Option 1: Cancel this reservation");
  453.         System.out.println("Option 2: Change the start time for this reservation");
  454.         System.out.println("Option 3: Change the end time for this reservation");
  455.         System.out.println("Option 4: Return to main menu");
  456.     }
  457.  
  458. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement