Advertisement
Guest User

Untitled

a guest
Dec 17th, 2017
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.26 KB | None | 0 0
  1. package CinemaBookingSystem;
  2.  
  3. import java.text.ParseException;
  4. import java.text.SimpleDateFormat;
  5. import java.util.Date;
  6. import java.util.Scanner;
  7.  
  8. public class CinemaSystem {
  9.  
  10.     //Strings to hold user keyboard input and current selected movie.
  11.     public static String movieChoice = "";
  12.     public static String userInput = "";
  13.     public static Scanner in = new Scanner(System.in);
  14.     //Booleans used for loops based on whether the user has chosen a movie/time yet.
  15.     public static boolean movieChosen = false;
  16.     public static boolean timeChosen = false;
  17.     //SimpleDateFormats to switch between 24 hour and 12 hour time.
  18.     public static SimpleDateFormat displayFormat = new SimpleDateFormat("HH:mm");
  19.     public static SimpleDateFormat parseFormat = new SimpleDateFormat("hh:mm a");
  20.     //Public int to store the theater for use in other classes.
  21.     public static int theaterNum = 0;
  22.     public static Date chosenTime;
  23.     //Line break string.
  24.     public static final String lb = "----------------------------------------";
  25.  
  26.     //The main method calls all the methods throughout the program.
  27.     public static void main(String[] args) throws ParseException {
  28.         //Set up the theater and show hash maps, then prompt the user to find a movie.
  29.         Theater.createTheaters();
  30.         Show.addMovies();
  31.         System.out.println("Welcome to CinemaBooking 2.0!");
  32.         System.out.println(lb);
  33.         //Method for the user to search through the movies.
  34.         findMovie();
  35.         System.out.println(lb);
  36.         //Once the user finds a movie they verify it and choose the theater.
  37.         chooseTheater();
  38.         System.out.println(lb);
  39.         //User chooses a time to see the movie.
  40.         while (!timeChosen) {
  41.             chooseTime();
  42.         }
  43.         System.out.println(lb);
  44.         //Find out how many adult/child seats and choose the rows/seats.
  45.         SeatBooking.reserveSeats(theaterNum);
  46.         System.out.println(lb);
  47.         //Get the users name and phone number, and generate an ID.
  48.         Customer.getCustomerInfo();
  49.         System.out.println(lb);
  50.         //Finally print out the "stub" with all the information on it,
  51.         Customer.printFinalInfo();
  52.         System.out.println(lb);
  53.     }
  54.  
  55.     //This method uses a while loop to let the user choose how to find a movie, either show a list, search by name, or search by time.
  56.     //It also uses the movieChosen boolean to decide when to move on to seat reservations.
  57.     public static void findMovie() throws ParseException {
  58.  
  59.         //Let the user search for a movie 1 of 3 ways, this continues until they type "done".
  60.         while (!movieChosen) {
  61.             System.out.println("Type 1 to see a list of movies that are currently playing, type 2 to search for a movie by name, type 3 to search for movies playing at a certain time.");
  62.             System.out.print("Once you have found the movie you would like to see, type 'done' to procede to seat reservations: ");
  63.             userInput = in.nextLine();
  64.             if (userInput.equals("1")) {
  65.                 System.out.println("");
  66.                 Show.listMovies();
  67.                 System.out.println("");
  68.             }
  69.             else if (userInput.equals("2")) {
  70.                 System.out.print("\nWhat movie would you like to search for?: ");
  71.                 userInput = in.nextLine();
  72.                 if (Show.isMovie(userInput))
  73.                     System.out.println("Movie found!\n");
  74.                 else
  75.                     System.out.println("Movie not found, please try again.\n");
  76.             }
  77.             //If the user wants to search via time, it must be converted from 12 hour to 24 hour so it can be compared to the array.
  78.             //For this I use SimpleDateFormat and parseInt.
  79.             else if (userInput.equals("3")) {
  80.                 System.out.print("\nWhat time would you like to search for? (Use the format hh:mm am/pm): ");
  81.                 userInput = in.nextLine();
  82.                 Date formatTime;
  83.                 try {
  84.                     formatTime = parseFormat.parse(userInput);
  85.                     String formatTimeString = displayFormat.format(formatTime).toString();
  86.                     formatTimeString = formatTimeString.replaceAll("[^0-9]", "");
  87.                     int timeToSearch = Integer.parseInt(formatTimeString);
  88.                     Show.searchTimes(timeToSearch);
  89.                 } catch (Exception e) {
  90.                     System.out.println("\nPlease enter a time in the format hh:mm am/pm\n");
  91.                 }
  92.             }
  93.             else if (userInput.equalsIgnoreCase("done")) {
  94.                 movieChosen = true;
  95.             }
  96.             else {
  97.                 System.out.println("Invalid choice, please try again.");
  98.             }
  99.         }
  100.     }
  101.  
  102.     //Verify what movie they want to see.
  103.     //This way they can search for multiple movies before actually choosing one.
  104.     public static void chooseTheater() {
  105.  
  106.         boolean theaterChosen = false;
  107.  
  108.         while (!theaterChosen) {
  109.             System.out.print("Type the name of the movie you are reserving seats for: ");
  110.             userInput = in.nextLine();
  111.             theaterNum = Show.findTheaterNum(userInput);
  112.             //Print what theater the movie is in and the format.
  113.             Theater.returnTheaterInfo(theaterNum);
  114.             if (theaterNum > 0)
  115.                 theaterChosen = true;
  116.         }
  117.     }
  118.  
  119.     //Let the user choose what time to see that movie.
  120.     public static void chooseTime() {
  121.         //Print the times for the chosen movie.
  122.         Show.showTimes(theaterNum);
  123.         System.out.print("\nWhich time would you like to reserve seats for?: ");
  124.         userInput = in.nextLine();
  125.         //Try/catch block to prevent format exception in the input.
  126.         try {
  127.             chosenTime = parseFormat.parse(userInput);
  128.             System.out.println(lb);
  129.             System.out.println("Your chosen time is: " + parseFormat.format(chosenTime));
  130.             timeChosen = true;
  131.         } catch (Exception e) {
  132.             System.out.println("\nPlease enter a time in the format hh:mm am/pm\n");
  133.         }
  134.     }
  135. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement