Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /**
- * Name: Dien Kim Tran
- * NetID: DKT180000
- * Date: 9/17/2019
- *
- * Submission for Project 1: Movie Ticket Reservation System
- */
- package Tickets;
- import java.io.*;
- import java.util.Scanner;
- import java.util.stream.*;
- /**
- * A class for storing auditoriums.
- */
- class Auditorium {
- // Store the file name of this instance's auditorium.
- private String fileName;
- // 2D char array containing the actual auditorium seats.
- public char[][] seats;
- // Ticket prices for adults, children and seniors
- public static final double PRICE_ADULT = 10.00;
- public static final double PRICE_CHILD = 5.00;
- public static final double PRICE_SENIOR = 7.50;
- /**
- * Constructor, takes file name and dimensions of seat grid
- *
- * @param fileName The file name of the auditorium
- * @param MAX_ROWS The number of rows of the seat grid
- * @param MAX_COLS The number of columns of the seat grid
- */
- Auditorium(String fileName, char[][] seats) {
- // Store auditorium file name
- this.fileName = fileName;
- // Allocate the seats grid
- this.seats = seats;
- }
- /**
- * Returns the file name of the instance's auditorium.
- *
- * @return the file name of the auditorium
- */
- public String getFileName() {
- return fileName;
- }
- /**
- * Reads in the auditorium 2D char array from a given auditorium file.
- *
- * @param auditoriumFile a `File` of auditorium seats
- * @return A 2D char array of seats from the file
- */
- static char[][] readSeats(File auditoriumFile) throws Exception {
- // Used to initialize the `seats` array
- int numRows = 0, numCols = 0;
- // Set up a Scanner applied to the file
- try (Scanner input = new Scanner(auditoriumFile)) {
- // Get the number of columns
- if (input.hasNext()) {
- numCols = input.nextLine().length();
- numRows = 1; // we read in a row; set it to 1
- }
- // Get the number of rows by reading until EOF
- while (input.hasNext()) {
- input.nextLine();
- ++numRows;
- }
- }
- // Initialize the `seats` array
- char[][] seats = new char[numRows][numCols];
- // Populate `seats`
- try (Scanner input = new Scanner(auditoriumFile)) {
- // Read in row
- for (int row = 0; row < numRows; ++row) {
- seats[row] = input.nextLine().toCharArray();
- }
- }
- // Done, return `seats`
- return seats;
- }
- }
- public class Main {
- /**
- * Takes a 2D char array representing a seat layout and displays it in a
- * formatted fashion.
- *
- * @param seats The seat layout, as a 2D char array
- */
- private static void displaySeatLayout(char[][] seats) {
- // Print the column axis
- System.out.print(" ");
- for (int col = 0; col < seats[0].length; ++col) {
- System.out.print((char) Character.toUpperCase(('A' + col)));
- }
- System.out.println();
- // Print seat rows
- for (int row = 0; row < seats.length; ++row) {
- System.out.print("" + (row + 1) + " ");
- for (int col = 0; col < seats[row].length; ++col) {
- switch (seats[row][col]) {
- case '.':
- System.out.print('.');
- break;
- default:
- System.out.print('#');
- break;
- }
- }
- System.out.println();
- }
- }
- /**
- * Displays information about booked seats for a given auditorium
- * @param aud The auditorium to display information about
- */
- private static void displayReport(Auditorium aud) {
- System.out.println("Report on auditorium \"" + aud.getFileName() + "\"");
- // Flatten seats
- char[] flatSeats = new char[aud.seats.length * aud.seats[0].length]; // size = numRows * numCols
- for (int row = 0; row < aud.seats.length; ++row) {
- for (int col = 0; col < aud.seats[row].length; ++col) {
- flatSeats[aud.seats[row].length * row + col] = aud.seats[row][col];
- }
- }
- // Compute all required values
- long adultTicketsSold = 0, childTicketsSold = 0, seniorTicketsSold = 0;
- for (int index = 0; index < flatSeats.length; ++index) {
- switch (flatSeats[index]) {
- case 'A':
- ++adultTicketsSold;
- break;
- case 'C':
- ++childTicketsSold;
- break;
- case 'S':
- ++seniorTicketsSold;
- break;
- }
- }
- double totalTicketSales = Auditorium.PRICE_ADULT * adultTicketsSold
- + Auditorium.PRICE_CHILD * childTicketsSold
- + Auditorium.PRICE_SENIOR * seniorTicketsSold;
- // Display them
- System.out.println("Adult tickets sold: " + adultTicketsSold);
- System.out.println("Child tickets sold: " + childTicketsSold);
- System.out.println("Senior tickets sold: " + seniorTicketsSold);
- System.out.println("Total tickets sold: " + (adultTicketsSold + childTicketsSold + seniorTicketsSold));
- System.out.printf("Total ticket sales: $%.2f\n", totalTicketSales);
- }
- private static int printGetMenuChoice(Scanner input) {
- System.out.println("1. Reserve Seats\n2. Exit");
- System.out.print("Type desired menu entry and press Enter: ");
- int choice;
- choice = input.nextInt();
- return choice;
- }
- /**
- * Checks whether the desired seats on the `row`-th row, starting from
- * [row][col] and ending at [row][col + numSeats - 1], are available. All
- * coordinates must be zero-based.
- *
- * @param seatRow The row of seats
- * @param col The index of the column where desired seats are located
- * @param numSeats The number of seats to query
- * @return A boolean value, true if such seats are available to book, false if
- * not
- */
- private static boolean queryAvailable(char[] seatRow, int col, int numSeats) {
- // The last seat in the sequence must not be out-of-bounds
- if (col + numSeats > seatRow.length)
- return false;
- // Loop along desired seats until we find one non-available seat
- for (int index = 0; index < numSeats; ++index) {
- if (seatRow[col + index] != '.') {
- return false;
- }
- }
- return true;
- }
- /**
- * Writes the seat layout of an `Auditorium` object to a file.
- *
- * @param aud the auditorium object of which seat layout is to be written
- * @throws Exception
- */
- private static void writeSeatLayout(Auditorium aud) throws Exception {
- try (PrintWriter output = new PrintWriter(new File(aud.getFileName()))) {
- for (int index = 0; index < aud.seats.length; ++index) {
- output.println(new String(aud.seats[index]));
- }
- }
- }
- /**
- * Finds the best available seats, biased towards the middle of the row, towards
- * the left, then towards the right
- *
- * @param seatRow The row of the seats the user wants to book
- * @param numSeats The number of seats to book
- * @return -1 if no seats available on this row, or the starting column index if
- * available
- */
- private static int findBestAvailableSeats(char[] seatRow, int numSeats) {
- // Cache these values, to be used for computation
- int seatSequenceLengthMiddle = numSeats / 2, rowLengthMiddle = seatRow.length / 2;
- // Trivial case: Check if `seatRow.length < numSeats`, return -1 if true
- if (seatRow.length < numSeats)
- return -1;
- // We check from the middle of the row, then query whether the seat sequence
- // shifted by 1 to the left are available, until we get to the start of the row
- for (int offset = 0; offset < rowLengthMiddle - seatSequenceLengthMiddle + 1; ++offset) {
- if (queryAvailable(seatRow, rowLengthMiddle - seatSequenceLengthMiddle - offset, numSeats)) {
- return rowLengthMiddle - seatSequenceLengthMiddle - offset;
- }
- }
- // Do the same as above but shift to the right instead
- for (int offset = 1; offset < (seatRow.length - rowLengthMiddle + seatSequenceLengthMiddle - numSeats
- + 1); ++offset) {
- if (queryAvailable(seatRow, rowLengthMiddle - seatSequenceLengthMiddle + offset, numSeats)) {
- return rowLengthMiddle - seatSequenceLengthMiddle + offset;
- }
- }
- // If we get here, we obviously haven't returned the starting position of the
- // first available seat yet, so return -1
- return -1;
- }
- /**
- * Returns a new row with seats booked.
- *
- * @param seatRow Current row
- * @param start Starting column where seats are booked
- * @param seatTypes Encapsulates adult, child, and senior tickets
- * @return A `char` array with committed changes
- */
- private static char[] bookSelectedSeats(char[] seatRow, int start, int[] seatTypes) {
- for (int index = 0; index < seatTypes[0]; ++index) {
- seatRow[start + index] = 'A';
- }
- for (int index = seatTypes[0]; index < seatTypes[0] + seatTypes[1]; ++index) {
- seatRow[start + index] = 'C';
- }
- for (int index = seatTypes[0] + seatTypes[1]; index < seatTypes[0] + seatTypes[1] + seatTypes[2]; ++index) {
- seatRow[start + index] = 'S';
- }
- return seatRow;
- }
- public static void main(String[] args) throws Exception {
- // Create a null reference to an `Auditorium` array.
- Auditorium[] auditoriums;
- // Read from current directory
- File currentDir = new File(".");
- // Filter out files that end with ".txt"
- File[] auditoriumFiles = Stream.of(currentDir.listFiles())
- .filter((txtFile) -> txtFile.getName().endsWith(".txt")).toArray(File[]::new);
- // Allocate enough space for all auditoriums
- auditoriums = new Auditorium[auditoriumFiles.length];
- // Instantiate all auditoriums
- for (int index = 0; index < auditoriums.length; ++index) {
- auditoriums[index] = new Auditorium(auditoriumFiles[index].getName(),
- Auditorium.readSeats(auditoriumFiles[index]));
- }
- // Ask the user for which file to choose
- System.out.println("Auditorium files found:");
- for (int index = 0; index < auditoriumFiles.length; ++index) {
- System.out.println("" + (index + 1) + ": " + auditoriumFiles[index].getName());
- }
- int auditoriumIndex;
- System.out.print("Type the number corresponding to the auditorium you want: ");
- try (Scanner input = new Scanner(System.in)) {
- auditoriumIndex = input.nextInt() - 1;
- while (auditoriumIndex < 0 || auditoriumIndex >= auditoriums.length) {
- System.out.println("Invalid file number. Input again.");
- System.out.print("Type the number corresponding to the auditorium you want: ");
- auditoriumIndex = input.nextInt() - 1;
- }
- // Store current auditorium to a variable so it's easier to work with
- // and write out
- Auditorium currentAuditorium = auditoriums[auditoriumIndex];
- System.out.println("You chose file number " + (auditoriumIndex + 1) + ", which is "
- + currentAuditorium.getFileName() + "\n");
- // We now perform our tasks on the file at `auditoriumFiles[auditoriumIndex]`.
- // MAIN LOGIC LOOP
- int menuChoice = 2;
- do {
- menuChoice = printGetMenuChoice(input);
- System.out.println();
- switch (menuChoice) {
- // There is no other case than case 1.
- case 1:
- // User wants to reserve seats
- // Print current seat layout
- System.out.println("Current seat layout:");
- displaySeatLayout(currentAuditorium.seats);
- System.out.println();
- // Prompt for needed information
- System.out.println("You can book multiple seats on the same row.");
- System.out.print("Row of your desired seats: ");
- int bookingRow = input.nextInt();
- System.out.print("Starting seat letter: ");
- int bookingColumn = Character.toUpperCase(input.next().charAt(0)) - 'A' + 1;
- System.out.print("Number of adult tickets: ");
- int numAdultTickets = input.nextInt();
- System.out.print("Number of child tickets: ");
- int numChildTickets = input.nextInt();
- System.out.print("Number of senior tickets: ");
- int numSeniorTickets = input.nextInt();
- System.out.println();
- // Compute the number of tickets to book
- int numSeats = numAdultTickets + numChildTickets + numSeniorTickets;
- // Query if they are available
- if (!queryAvailable(currentAuditorium.seats[bookingRow - 1], bookingColumn - 1, numSeats)) {
- System.out.println(
- "The seats you requested are unavailable. Finding the next best available seats.");
- // Find the best available seats
- int bestAvailableColumn = findBestAvailableSeats(currentAuditorium.seats[bookingRow - 1],
- numSeats);
- if (bestAvailableColumn == -1) {
- System.out.println("No seats available this row.\n");
- } else {
- // Ask the user if they want these seats
- System.out.print("Best available seats: ");
- for (int col = bestAvailableColumn; col < bestAvailableColumn + numSeats; ++col) {
- System.out.print("" + bookingRow + (char) (col + 'A'));
- if (col != numSeats - 1) {
- System.out.print(' ');
- }
- }
- System.out.print("\nDo you want these seats? (y/n) ");
- char bestAccepted = input.next().charAt(0);
- if (bestAccepted == 'y') {
- currentAuditorium.seats[bookingRow - 1] = bookSelectedSeats(
- currentAuditorium.seats[bookingRow - 1], bestAvailableColumn,
- new int[] { numAdultTickets, numChildTickets, numSeniorTickets });
- System.out.println("\nChanges written.\n");
- // Save layout to file
- writeSeatLayout(currentAuditorium);
- } else {
- // User does not want these seats, move on
- System.out.println();
- }
- }
- } else {
- // Seats available -- book them
- currentAuditorium.seats[bookingRow - 1] = bookSelectedSeats(
- currentAuditorium.seats[bookingRow - 1], bookingColumn - 1,
- new int[] { numAdultTickets, numChildTickets, numSeniorTickets });
- System.out.println("\nChanges written.\n");
- // Save layout to file
- writeSeatLayout(currentAuditorium);
- }
- }
- } while (menuChoice != 2);
- }
- // Print report on all auditoriums
- for (Auditorium aud : auditoriums) {
- displayReport(aud);
- System.out.println();
- }
- // Done.
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement