private static void listMovies(int cineplex) {

    if (cineplex == 0) {

      for (int i = 0; i < cineplexList.size(); i++) {

        System.out.println(cineplexList.get(i).getCineplexName() + ":");
        cineplexList.get(i).listMovie();
        System.out.println();
      }

    } else {
      // first populate all the movie from all cineplex first
      ArrayList<Movie> movieList = new ArrayList<Movie>();
      for (int i = 0; i < cineplexList.size(); i++) {
        ArrayList<Movie> currentMovieList = cineplexList.get(i).getMovieList();
        for (int j = 0; j < currentMovieList.size(); j++) {
          Movie curMovie = currentMovieList.get(j);
          boolean pass = true;
          for (int k = 0; k < movieList.size(); k++) {
            if (movieList.get(k).getTitle().equals(curMovie.getTitle())) {
              pass = false;
              break;
            }
          }
          // add the movie into movieList
          if (pass) {
            movieList.add(curMovie);
          }
        }
      }

      Cineplex.printMovieForUser(movieList);
    }
  }
  private static void displayTicket(int timingChoice) throws Exception {

    // System.out.println("Transaction ID: " + customer.getTransactionId());
    // generate the transaction Id here
    String transactionId = "";
    transactionId += (String) customer.getName().substring(0, 3).toUpperCase();
    DateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmm");
    Calendar calendar = Calendar.getInstance();
    transactionId += dateFormat.format(calendar.getTime());

    String bookingHistory = "";
    bookingHistory += movieChosen.getTitle() + ", TransactionId:" + transactionId;
    customer.addToBookedMovie(bookingHistory);
    System.out.println("TransactionId: " + transactionId);
    System.out.println("Your seat: " + customer.getSeat());
    System.out.println("choice date: " + choiceDate);
    System.out.println(
        "Showing cinema: "
            + movieChosen
                .getAvailableMovieDateList()
                .get(choiceDate - 1)
                .getCinema()
                .getCinemaName());
    System.out.println("Showing time: " + movieScheduleList.get(timingChoice - 1).getTime());
    System.out.println("\n");
  }
 public static void removeTheDate() {
   System.out.println("Schedule " + currentMovie.getTitle() + " :");
   currentMovie.getListDateStatusMovie();
   System.out.println("\n");
   ArrayList<DateMovie> listMovie = currentMovie.getArrayListOfDateMovie();
   System.out.println("Please choose which date you want to configure");
   int indexListMovie = scan.nextInt();
   DateMovie currentDate = listMovie.get(indexListMovie - 1);
   listMovie.remove(currentDate);
 }
 private static void configuringDate() {
   // getting the list of date on the currentMovie
   System.out.flush();
   System.out.println("Please choose one option");
   System.out.println("1) Add another date");
   System.out.println("2) Editing the existing date");
   System.out.println("3) Remove the date");
   int addDate = scan.nextInt();
   if (addDate == 1) {
     setNewDateOfMovie(currentMovie);
   } else if (addDate == 2) {
     editStatusTimeCinema();
   } else if (addDate == 3) {
     removeTheDate();
   } else {
     System.out.println("Sorry your choice is not listed in our system");
     System.out.println("Please choose the below option");
     System.out.println("1) Quit to " + currentMovie.getTitle() + " configuration");
     System.out.println("2) Editting Date again");
     int option = scan.nextInt();
     if (option == 1) {
       return;
     } else if (option == 2) {
       System.out.println("");
       configuringDate();
       return;
     }
   }
 }
  private static ArrayList<String> searchCineplex(String title, Cineplex cineplex) {

    ArrayList<String> result = new ArrayList<String>();

    movieListForSpecificCineplex = cineplex.getMovieList();

    for (int i = 0; i < movieListForSpecificCineplex.size(); i++) {
      Movie curMovie = new Movie();
      curMovie = movieListForSpecificCineplex.get(i);
      if ((curMovie.getTitle().toLowerCase()).equals(title.toLowerCase())) {
        // processing the status first
        String status = "";
        for (int j = 0; j < curMovie.getArrayListOfMovieDate().size(); j++) {
          status = curMovie.getArrayListOfMovieDate().get(j).getStatus();
          if (status.equals("End of Showing")) {
            status = "Now Showing";
          }
        }

        result.add(
            cineplex.getCineplexName()
                + ":  "
                + "\n"
                + curMovie.getTitle()
                + "\nPrice: "
                + Double.toString(curMovie.getNormalPrice())
                + "\nDuration: "
                + curMovie.getDuration()
                + "\nStatus:  "
                + status);
      }
    }

    return result;
  }
 private static void editStatusTimeCinema() {
   System.out.println("Schedule " + currentMovie.getTitle() + " :");
   currentMovie.getListDateStatusMovie();
   System.out.println("\n");
   ArrayList<DateMovie> listMovie = currentMovie.getArrayListOfDateMovie();
   System.out.println("Please choose which date you want to configure");
   int indexListMovie = scan.nextInt();
   DateMovie currentDate = listMovie.get(indexListMovie - 1);
   System.out.println(
       "\nPlease choose which one you want to edit on " + currentDate.getStatusTimeMovie());
   System.out.println("1) Time");
   System.out.println("2) status");
   System.out.println("3) cinema");
   int timestatus = scan.nextInt();
   if (timestatus == 1) {
     editTime(listMovie.get(indexListMovie - 1));
   } else if (timestatus == 2) {
     System.out.println("insert the new Status");
     scan.nextLine();
     String newStatus = chooseStatusMovie();
     currentDate.setStatus(newStatus);
     if (!newStatus.equals("Coming Soon")) {
       movieRated.addMovieList(currentMovie);
     }
   } else if (timestatus == 3) {
     System.out.println("List Cinema: ");
     int choosenCinema = getIndexCinema();
     currentDate.setCinema(currentCineplex.getCinema(choosenCinema - 1));
   } else {
     System.out.println("Sorry your choice is not listed in the system");
     System.out.println("Please choose the below option");
     System.out.println("1) Quit to Date configuration");
     System.out.println("2) Editing Date attribute again");
     int option = scan.nextInt();
     if (option == 1) {
       return;
     } else if (option == 2) {
       System.out.println("");
       editStatusTimeCinema();
       return;
     }
     return;
   }
 }
 private static void setNewDateOfMovie(Movie chooseMovie) {
   System.out.println("insert the new date with format");
   System.out.print("YYYY: ");
   int year = scan.nextInt();
   System.out.print("MM: ");
   int month = scan.nextInt();
   System.out.print("DD: ");
   int day = scan.nextInt();
   System.out.print("HH: ");
   int hour = scan.nextInt();
   System.out.print("mm: ");
   int minute = scan.nextInt();
   DateMovie datemovie = new DateMovie(year, month, day, hour, minute);
   System.out.println("insert status to this new Date " + datemovie.getTime());
   scan.nextLine();
   String newStatus = chooseStatusMovie();
   System.out.println("specify which cinema that will play on " + datemovie.getYearMonthDay());
   int cinemaoption = getIndexCinema();
   Cinema cinema = currentCineplex.getCinema(cinemaoption - 1);
   datemovie.setStatus(newStatus);
   datemovie.setCinema(cinema);
   currentMovie.setTimeStatus(datemovie);
   System.out.println("\nYour change has been saved to our system\n");
 }
  private static void updatingMovie() {
    System.out.println("\n------------------------\n");
    System.out.println("List movie of " + currentCineplex.getCineplexName() + "............");
    for (int i = 0; i < currentCineplex.getMovieList().size(); i++) {
      System.out.println(i + 1 + ")");
      System.out.printf("%s\n", currentCineplex.getMovieList().get(i).printDescription());
    }
    System.out.println("\n------------------------\n");
    System.out.println("\nPlease choose one movie to configure\n");
    int indexMovie = scan.nextInt();
    if (indexMovie > currentCineplex.getMovieList().size()) {
      System.out.println("Sorry your movie option is invalid");
      System.out.println("Please choose again");
      updatingMovie();
      return;
    }
    currentMovie = currentCineplex.getMovie(indexMovie - 1);

    if (currentCineplex.getMovieList().size() != 0) {
      // later need to add the Date attribute so it will detect current time and the time when you
      // put the object
      System.out.println("What description you want to configure");
      System.out.println("1) title");
      System.out.println("2) duration");
      System.out.println("3) ratingPG");
      System.out.println("4) Genre");
      System.out.println("5) price");
      System.out.println("6) Director");
      System.out.println("7) Cast");
      System.out.println("8) Synopsis");
      System.out.println("9) date and status");

      int MovieConfigurationDescription = scan.nextInt();
      System.out.flush();
      switch (MovieConfigurationDescription) {
        case 1:
          System.out.println("insert the new title");
          scan.nextLine();
          String newTitle = scan.nextLine();
          currentMovie.setTitle(newTitle);
          break;
        case 2:
          System.out.println("insert the new duration in minute");
          scan.nextLine();
          String newDuration = scan.nextLine();
          currentMovie.setDuration(newDuration);
          break;
        case 3:
          System.out.println("Set the new RatingPG");
          scan.nextLine();
          String newRatingPG = scan.nextLine();
          currentMovie.setRatingPG(newRatingPG);
          break;
        case 4:
          System.out.println("Set the new Genre of the movie");
          scan.nextLine();
          String newGenre = chooseMovieType();
          currentMovie.setGenre(newGenre);
          break;
        case 5:
          System.out.println("insert the new price of the movie in SGD");
          scan.nextLine();
          double price = scan.nextDouble();
          currentMovie.setPrice(price);
          break;
        case 6:
          System.out.println("insert the new Director of the movie");
          scan.nextLine();
          String director = scan.nextLine();
          currentMovie.setDirector(director);
          break;
        case 7:
          ArrayList<String> listCasting = currentMovie.getCastList();
          System.out.println("List of  cast: ");
          for (int i = 0; i < listCasting.size(); i++) {
            System.out.printf("%d) %s\n", i + 1, listCasting.get(i));
          }
          System.out.println("");
          System.out.println("Editting the cast");
          System.out.println("1) add new cast");
          System.out.println("2) Remove cast");
          System.out.println("3) Update new  List of cast");

          int optionCast = scan.nextInt();
          if (optionCast == 1) {
            System.out.println("insert the new cast ");
            scan.nextLine();
            String newCast = scan.nextLine();
            listCasting.add(newCast);
          } else if (optionCast == 2) {
            System.out.println("List of cast");
            // need to exception
            int deleteCast = getIndexCast(listCasting);
            listCasting.remove(deleteCast - 1);
          } else if (optionCast == 3) {
            System.out.println("How many cast you want to add");
            int castNumber = scan.nextInt();
            while (castNumber < 2) {
              System.out.println("please choose at least 2");
              System.out.println("How many cast you want to add(at least 2)");
              castNumber = scan.nextInt();
            }
            ArrayList<String> castList = new ArrayList<String>();
            scan.nextLine();
            for (int i = 0; i < castNumber; i++) {
              System.out.printf("name of cast %d: ", i + 1);
              String cast = scan.nextLine();
              castList.add(cast);
            }
            listCasting = castList;
          } else {
            System.out.println("Sorry your option is not listed in our system");
          }
          currentMovie.setCastList(listCasting);
          break;
        case 8:
          System.out.println("insert the new Synopsis");
          scan.nextLine();
          String synopsis = scan.nextLine();
          currentMovie.setSynopsis(synopsis);
          break;
        case 9:
          configuringDate();
          break;
        default:
          System.out.println("Sorry your choice is not listed in our system");
          System.out.println("Please choose the below option");
          System.out.println("1) Quit to " + currentCineplex.getCineplexName() + " configuration");
          System.out.println("2) Editing Movie again");
          int option = scan.nextInt();
          if (option == 1) {
            return;
          } else if (option == 2) {
            System.out.println("");
            updatingMovie();
            return;
          }
      }
      System.out.println("\nchanges has been saved to our system\n");
    } else {
      System.out.println(
          "\nDont have any movie on the list, please add at least one of the movie to the cinema");
    }
  }
  public static void main(String[] args) {

    boolean carryOn = true;
    try {
      while (carryOn) {

        int option = requestInput(MAIN_MENU);

        System.out.flush();

        switch (option) {
          case 1:
            listMovies(0);
            break;

          case 2:
            listMovies(1);
            break;

          case 3:
            ArrayList<String> searchResult = new ArrayList<String>();

            String searchMovie = "";

            System.out.println("Enter movie title: ");

            sc.nextLine();
            searchMovie = sc.nextLine();
            System.out.flush();
            for (int i = 0; i < cineplexList.size(); i++) {

              searchResult = searchCineplex(searchMovie, cineplexList.get(i));
              if (searchResult.size() == 0) {

                System.out.println(
                    cineplexList.get(i).getCineplexName() + ":  No movie to show. \n");

              } else {

                for (int j = 0; j < searchResult.size(); j++) {

                  System.out.println(searchResult.get(j));
                }
              }
              System.out.println("");
            }

            break;

          case 4:
            int cineplexChoice = 0;
            requestInput(BOOKING_MAIN_INTERFACE);
            if (customer.confirmCustomerInfo()) cineplexChoice = requestInput(CINEPLEX_INTERFACE);
            else {

              System.out.println("Cancelled");
              break;
            }

            movieListForSpecificCineplex =
                cineplexList.get(cineplexChoice - 1).getMovieListAvailable();

            int movieChoice = requestInput(MOVIE_INTERFACE_FOR_SELECTED_CINEPLEX);

            movieChosen = movieListForSpecificCineplex.get(movieChoice - 1);

            movieScheduleList = movieChosen.getAvailableMovieDateList();

            if (movieScheduleList.size() > 0) {
              int timingChoice = requestInput(SELECT_TIMING_INTERFACE);
              choiceDate = timingChoice;
              int ageGroup = requestInput(SELECT_AGE_GROUP_INTERFACE);
              double ticketPrice = movieChosen.getPrice(ageGroup, choosenDate);
              requestInput(SELECT_SEAT);

              movieChosen.notificationMessage();
              System.out.println(
                  "Ticket price is SGD " + ticketPrice + "." + " Proceed to payment? (y / n)");

              String confirmation = sc.next();

              if (confirmation.equals("y") || confirmation.equals("Y"))
                try {
                  displayTicket(timingChoice);
                } catch (Exception e) {
                  e.printStackTrace();
                }
              userDatabase.writeToDatabase("CineplexDatabase.dat", cineplexList);
              customerDatabase.writeToDatabase("CustomerDatabase.dat", customerList);
            } else {
              System.out.println("No schedule movie");
            }
            break;

          case 5:
            System.out.println("Select a movie to rate: ");
            requestInput(MOVIE_INTERFACE);
            break;

          case 6:
            requestInput(CHECK_MOVIE_INTERFACE);
            break;

          case 7:
            System.out.println("Quitting");
            System.exit(0);
        }
      }
    } catch (Exception e) {
      e.printStackTrace();
      System.out.println("Your input is invalid that make this program undergo exception fault");
      System.out.println("1) Restart Program");
      System.out.println("2) Quit");
      sc.nextLine();
      int option = sc.nextInt();
      if (option == 1) {
        UserApplication.main(args);
      } else {
        System.out.println("Quit");
        return;
      }
    }
  }
  private static int requestInput(int userInterface) {

    switch (userInterface) {
      case MAIN_MENU:
        System.out.println(
            "Choose one option:\n"
                + "1) List the movies available\n"
                + "2) List the movies with details\n"
                + "3) Search for movies\n"
                + "4) Book ticket\n"
                + "5) Rate,Review and see the top movie\n"
                + "6) View your booking\n"
                + "7) Exit\n");

        // input check
        while (true) {
          if (sc.hasNextInt()) {
            int choice = sc.nextInt();
            if (choice > 0 && choice <= 7) return choice;
            else continue;
          }
          sc.nextLine();
          System.out.println("Invalid Option, choose again: ");
        }

      case CINEPLEX_INTERFACE:
        System.out.println("Choose cineplex:");

        for (int i = 0; i < cineplexList.size(); i++) {

          System.out.printf("%d) ", i + 1);
          System.out.println(cineplexList.get(i).getCineplexName());
        }

        // input check
        while (true) {
          if (sc.hasNextInt()) {
            int choice = sc.nextInt();
            if (choice > 0 && choice <= cineplexList.size() + 1) return choice;
            else continue;
          }
          sc.nextLine();
          System.out.println("Invalid Option, choose again: ");
        }

      case MOVIE_INTERFACE_FOR_SELECTED_CINEPLEX:
        System.out.println("Choose movie:");

        for (int i = 0; i < movieListForSpecificCineplex.size(); i++) {

          System.out.printf("%d) ", i + 1);
          System.out.println(movieListForSpecificCineplex.get(i).getTitle());
        }

        // input check
        while (true) {
          if (sc.hasNextInt()) {
            int choice = sc.nextInt();
            if (choice > 0 && choice <= movieListForSpecificCineplex.size() + 1) return choice;
            else continue;
          }
          sc.nextLine();
          System.out.println("Invalid Option, choose again: ");
        }

      case MOVIE_INTERFACE:
        /*System.out.println("Choose movie:");

        for (int i = 0; i < movieListForAllCineplex.size(); i++){

        	System.out.printf("%d) ", i + 1);
        	System.out.println(movieListForAllCineplex.get(i).getTitle());

        }
        if (sc.hasNextInt()){

        	int choice = sc.nextInt();
        	while (choice >= movieListForAllCineplex.size() + 1){

        		System.out.println("Invalid Option, choose again: ");
        		choice = sc.nextInt();
        	}
        	return choice;

        }
        */
        // print all the movie from ratedmovie

        System.out.println("1) Top 5 Movie based on Rating");
        System.out.println("2) Top 5 movie based on Ticket Sales");
        System.out.println("3) Rate the movie");
        System.out.println("4) Write your review here");
        System.out.println("5) See the review of your favorite movie");
        int choiceOption = sc.nextInt();
        switch (choiceOption) {
          case 1:
            ratedMovie.printTopFiveMovieBasedOnRating();
            break;
          case 2:
            ratedMovie.printTopFiveMovieBasedOnTicket();
            break;
          case 3:
            ratedMovie.printTopFiveMovieBasedOnRating();
            System.out.println("which movie you want to rate");
            int movieRatedOption = sc.nextInt();
            while (true) {
              System.out.println("insert your rating here(1-5)");
              double rating = sc.nextDouble();
              if (rating > 5 || rating < 0) {
                System.out.println("sorry please put the acceptable range\n");
                continue;
              }
              ratedMovie.updateMovieRating(movieRatedOption - 1, rating);
              break;
            }
            break;
          case 4:
            ratedMovie.printNormalMovieList();
            System.out.println("Which movie you want to review");
            int optionReview = sc.nextInt();
            String movieHasRated = ratedMovie.getRatedMovie().get(optionReview - 1).getTitle();
            sc.nextLine();
            System.out.println("Insert your name please");
            String bufferName = sc.nextLine();
            System.out.println("Insert your review here");
            String reviewIns = "Review: \n";
            String reviewInserted = sc.nextLine();
            reviewIns += reviewInserted;
            System.out.println("");

            ratedMovie.updateMovieReview(bufferName, reviewIns, optionReview - 1);
            break;
          case 5:
            ratedMovie.printNormalMovieList();
            System.out.println("which movie you want to see the review");
            int optionReview1 = sc.nextInt();
            ratedMovie.printReview(optionReview1 - 1);
            break;
        }
        ratedMovieDatabase.writeToDatabase("RatedMovie.dat", readMovie);
        System.out.println("");
        return 0;

      case BOOKING_MAIN_INTERFACE:
        String customerEmailAdd;
        int customerPhoneNumber;
        System.out.println("Please enter your name: ");
        sc.nextLine();
        String customerName = sc.nextLine();
        System.out.println("Please enter your phone number: ");
        do {

          customerPhoneNumber = sc.nextInt();

          if (customerPhoneNumber / 10000000 == 8 || customerPhoneNumber / 10000000 == 9) break;
          else System.out.println("Invalid phone number, enter again: ");
          ;

        } while (true);

        System.out.println("Please enter your e-mail address: ");
        do {

          customerEmailAdd = sc.next();

          if (validateEmailAdd(customerEmailAdd) == false)
            System.out.println("Invalid email address, enter again: ");
          else break;

        } while (true);
        // need to check whether there has a people with the same name or not
        boolean pass = true;
        for (int i = 0; i < customerList.size(); i++) {
          customer = (Customer) customerList.get(i);
          if (customer.getEmailAdd().equals(customerEmailAdd)) {
            System.out.println("\nyour account has been in database");
            pass = false;
            break;
          }
        }
        if (pass) {
          customer = new Customer(customerName, customerPhoneNumber, customerEmailAdd);
          customerList.add(customer);
        }
        return 0;

      case RATING_INTERFACE:
        System.out.println("Select movie from list below to rate: ");
        listMovies(0);
        System.out.print("Your selection: ");

        return 0;

      case SELECT_TIMING_INTERFACE:
        System.out.println("Choose timing:");

        for (int i = 0; i < movieScheduleList.size(); i++) {

          System.out.printf("%d) ", i + 1);
          System.out.println(movieScheduleList.get(i).getTime());
        }

        // input check
        while (true) {
          if (sc.hasNextInt()) {
            int choice = sc.nextInt();
            if (choice > 0 && choice <= movieScheduleList.size() + 1) {
              choosenDate = movieScheduleList.get(choice - 1);
              return choice;
            } else continue;
          }
          sc.nextLine();
          System.out.println("Invalid Option, choose again: ");
        }

      case SELECT_AGE_GROUP_INTERFACE:
        System.out.println("Select your age group:");

        System.out.println(
            "1) Child (12 and below)"
                + "\n2) Adult (13-64)"
                + "\n3) Senior Citizen (65 and above)");

        // input check
        while (true) {
          if (sc.hasNextInt()) {
            int choice = sc.nextInt();
            if (choice > 0 && choice <= 4) return choice;
            else continue;
          }
          sc.nextLine();
          System.out.println("Invalid Option, choose again: ");
        }

      case SELECT_SEAT:
        Cinema showingCinema =
            movieChosen.getAvailableMovieDateList().get(choiceDate - 1).getCinema();
        boolean success = true;
        System.out.println(showingCinema.getSeatArrangement());
        System.out.println("Select seat from cinema layout above: ");
        System.out.print("Row (A - J): ");
        String row = sc.next();
        System.out.print("Column (1 - 10): ");
        int column = sc.nextInt();

        while (!showingCinema.requestSeat(row, column)) {

          System.out.println("Seat taken, please select again");
          System.out.print("(To cancel, enter 'z') Row (A - J): ");
          row = sc.next();
          if (row.equals("z")) {
            success = false;
            break;
          }
          System.out.print("Column (1 - 10): ");
          column = sc.nextInt();
        }

        if (success == true) {

          System.out.println("Successfully booked! movie name: " + movieChosen.getTitle());
          customer.assignSeat(row + Integer.toString(column));
          ratedMovie.updateMovieTicket(movieChosen.getTitle());

        } else {

          System.out.println("Cancelled");
        }

        return 0;

      case CHECK_MOVIE_INTERFACE:
        System.out.println("Enter your email address: ");
        do {

          customerEmailAdd = sc.next();

          if (validateEmailAdd(customerEmailAdd) == false)
            System.out.println("Invalid email address, enter again: ");
          else break;

        } while (true);

        Customer currentCustomer = null;

        for (int i = 0; i < customerList.size(); i++) {

          if (((Customer) customerList.get(i)).getEmailAdd().equals(customerEmailAdd)) {

            currentCustomer = (Customer) customerList.get(i);
            break;
          }
        }
        if (currentCustomer != null) {
          ArrayList<String> temp = currentCustomer.getBookedMovieList();
          String currentTransactionId = currentCustomer.getTransactionId();
          printBookedMovie(temp, currentTransactionId);
        } else {
          System.out.println("Sorry database do not save your email, have you ordered yet?\n");
        }
        return 0;

      default:
        return 0;
    }
  }