Objectives : Create a Java program using programming fundamentals (file I/O, loo
ID: 3785351 • Letter: O
Question
Objectives: Create a Java program using programming fundamentals (file I/O, loops, conditional statements, arrays, functions) Problem: In preparation for the release of Guardians of the Galaxy 2, you have been hired by the owner of a small movie theater to develop the backend for an online ticket reservation system. Patrons will be able to reserve seats in one of three auditoriums. Once the patron has selected an auditorium, the program should display the current seating arrangement and allow the patron to select seats. A report should be generated at the end of the program to specify for each individual auditorium and overall for all auditoriums how many seats were sold/unsold and how much money was earned.
Details
The seating arrangement for each auditorium will be stored in separate files. These files will be named A1.txt, A2.txt, and A3.txt for auditorium 1, 2 and 3 respectively.
Each line in the file will represent a row in the auditorium. The number of rows in each auditorium is unknown to you.
The number of seats in each row of a single auditorium will be the same. For example, if the first line of the file has 15 seats, then every subsequent row in the theater will also have 15 seats. This does not mean that each auditorium has the same number of seats in each row. One auditorium may have 15 seats per row and another may have 20 seats.
Each auditorium will be held in a two-dimensional array.
Empty seats are represented by a pound sign (#).
Reserved seats are represented by a period (.).
Tickets can only be reserved the day of the screening and all screenings are at 7 PM that night. There is no need to worry about multiple screenings or reserving seats on a specific day.
All tickets are $7 regardless of patron age or auditorium. User Interface and Input: Present a user-friendly menu system for the user to select the auditorium. First, ask for the auditorium:
1. Auditorium 1
2. Auditorium 2
3. Auditorium 3
4. Exit
Although in reality the user would likely only make one purchase, for testing purposes, assume the user will repeat the ticket buying process until they decide to quit.
Once the auditorium has been selected, display the current seating availability for that auditorium. An example seating chart is provided below for an auditorium with 5 rows and 20 seats per row.
The seats are numbered sequentially from left to right and only the one's digit is displayed above each column to make it easier to display the chart. It is understood that the second set of digits from 1-0 are for the numbers 11- 20 in the above example. After the user has selected the auditorium and the seating chart has been displayed, prompt the user for the following information in the order below:
Row number
Starting seat number
Number of tickets
Assume that the user wants to reserve sequential seats to the right of the first seat entered. If the desired seats are not available, offer the user the best available seats that meet their criteria on that row only. The best available seats are the seats closest to the middle of the row.
Prompt the user to enter a Y to reserve the best available or N to refuse the best available. Once the selection has been processed, return to the main menu. All input will be of the valid data type. You do not have to worry about the user entering a letter when a number is expected or a floating point number when an integer is expected. You are responsible for validating that the data falls within the proper range and that a user does not try to reserve a seat that is already reserved. Output: At the end of the program, write the current status of each auditorium to the respective file. Also, display a formatted report to the console. The report should consist of 4 columns:
Column 1 – labels
o Auditorium 1
o Auditorium 2
o Auditorium 3
o Total
Column 2 - number of seats reserved for each label
Column 3 - the number of open seats for each label
Column 4 - the total of the ticket sales for each label
Here is the Pseudocode I have made and verified by the professor.
Read Auditorium
Arguments: auditorium array
Returns: auditorium array
Open file
While not EOF
o Read line
o For 0 to line length
Copy character in string to array
Close file
Display Auditorium
Arguments: auditorium array
Returns: nothing
Display column header
For each row
o Display row number
o For each column
Display array[row][col]
o Display newline
Reserve Seats
Arguments: auditorium array, row, seat, quantity
Returns: nothing
For 1 to quantity
o Array[row][seat+i] = #
Check Availability
Arguments: auditorium array, row, seat, quantity
Returns: Boolean
For 1 to quantity
o If array[row][seat+i] is a period
Return false
Return true
Count Seats
Arguments: auditorium array
Returns: open seats and reserved seats
Open file based on auditorium
For each row o For each column
If array[row][col] = #
Increment open seats
Else increment reserved seats
Print Report
Arguments: none
Returns: none
For 1 to 3
o Call Get Seat Info function
o Print out open seats, reserved seats, money earned
o Increment total open seats, total reserved seats, total money earned
Print out total open seats, total reserved seats, total money earned
Best Available
Arguments: auditorium array, row, quantity
Returns: left endpoint of best available section
Determine middle of row
From mid to 1 (i) o If array[row][i] = #
For 1 to quantity (j)
Check if array[row][i-j] = #
If all available
Left side = Hold i
From mid to last column (i) o If array[row][i] = #
For 1 to quantity (j)
Check if array[row][i+j] = #
If all available
Right side = Hold i
Return min (right side, left side - quantity)
Main
Loop (do…while not exit)
o Print main menu
o Get user input
o If user does not want to exit
Print auditorium
Get auditorium number
If user wants to reserve seats
Ask user for row, seat, and quantity
Check if seats are available
If available reserve seats
If not available search for best available
o If best available, offer to user
If user wants to see auditorium
Call display auditorium function
Print report
Explanation / Answer
Here is the full solution to the problem. Note that it has two classes: Audotorium and AuditoriumTester(class with main method). Create two different classes in eclipse with the same name as mentioned here.
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
public class Auditorium {
public static final String path = "D:\Chegg";
//audiName should be same as the text file name.
public String[][] readAuditorium(String audiName){
String[][] auditorium = null;
BufferedReader br = null;
FileReader fr = null;
try{
fr = new FileReader(path+"\"+audiName);
br = new BufferedReader(fr);
String currentLine;
List<char[]> rowList = new ArrayList<char[]>();
while((currentLine=br.readLine())!=null){
char[] row = currentLine.toCharArray();
rowList.add(row);
}
int col = rowList.get(0).length;
auditorium = new String[rowList.size()][col];
for(int i=0;i<rowList.size();i++){
for(int j=0;j<col;j++){
auditorium[i][j] = rowList.get(i)[j]+"";
}
}
}catch(IOException e){
e.printStackTrace();
}finally{
try {
br.close();
fr.close();
} catch (IOException e) {
e.printStackTrace();
}
}
return auditorium;
}
//displays the auditorium on console according to the mentioned format
public void displayAuditorium(String[][] auditorium){
int colums = auditorium[0].length;
System.out.print(" ");
for(int i=1;i<=colums;i++){
if(i>9){
String num = i+"";
System.out.print(num.charAt(num.length()-1));
}
else{
System.out.print(i);
}
}
System.out.println();
for(int i=0;i<auditorium.length;i++){
System.out.print((i+1)+" ");
for(int j=0;j<auditorium[i].length;j++){
System.out.print(auditorium[i][j]);
}
System.out.println();
}
}
//Method to reserve seats.
public void reserveSeats(String auditorium[][], int row, int seat, int quantity){
//Assume that user will enter seat number starting from 1, subtract 1 from row and seat as array index starts with 0.
for(int i=0;i<quantity;i++){
auditorium[row-1][(seat-1)+i] = ".";
}
}
//Method to check availability
public boolean checkAvailability(String auditorium[][], int row, int seat, int quantity){
boolean isAvailable=true;
for(int i=0;i<quantity;i++){
if(auditorium[row-1][(seat-1)+i].equals("."))
isAvailable =false;
}
return isAvailable;
}
//Count number if open and reserve seats. Open seats are stored at 0th index and reserve at first.
public List<Integer> countSeats(String[][] auditorium){
List<Integer> countOpenReserve = new ArrayList<Integer>();
int open =0;
int reserve =0;
for(int i=0;i<auditorium.length;i++){
for(int j=0;j<auditorium[i].length;j++){
if(auditorium[i][j].equals("."))
reserve++;
else
open++;
}
}
countOpenReserve.add(open);
countOpenReserve.add(reserve);
return countOpenReserve;
}
public int bestAvailable(String auditorium[][], int row, int quantity){
int bestSeat =0;
for(int i=0;i<auditorium[row].length;i++){
int mid=auditorium[row].length/2;
if(checkAvailability(auditorium, row, mid, quantity)){
bestSeat = mid;
}
}
return bestSeat;
}
//update the modified information of auditorium in the text file.
public void updateAuditorium(String[][] auditorium,String name){
FileWriter fileWriter = null;
BufferedWriter bufferedWriter = null;
try {
fileWriter = new FileWriter(path+"\"+name);
bufferedWriter= new BufferedWriter(fileWriter);
StringBuilder sb;
for(int i=0;i<auditorium.length;i++){
sb= new StringBuilder("");
for(int j=0;j<auditorium[i].length;j++){
sb.append(auditorium[i][j]);
}
bufferedWriter.write(sb.toString()+System.lineSeparator());
}
} catch (IOException e) {
e.printStackTrace();
}finally{
try {
bufferedWriter.close();
fileWriter.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
//Generate Report
public void printReport(String[][]audi,String name){
updateAuditorium(audi,name);
String[][] audi1 = readAuditorium("audi1.txt");
String[][] audi2 = readAuditorium("audi2.txt");
String[][] audi3 = readAuditorium("audi3.txt");
System.out.println("Labels"+" "+"SeatsReserved"+" "+"SeatsOpen"+" "+"TotalSale");
List<Integer> seatsAudi1 = countSeats(audi1);
List<Integer> seatsAudi2 = countSeats(audi2);
List<Integer> seatsAudi3 = countSeats(audi3);
int totalOpenSeats = seatsAudi1.get(0)+seatsAudi2.get(0)+seatsAudi3.get(0);
int totalReserveSeats = seatsAudi1.get(1)+seatsAudi2.get(1)+seatsAudi3.get(1);
int totalSale = totalReserveSeats*7;
System.out.println("Auditorium1"+" "+seatsAudi1.get(1)+" "+seatsAudi1.get(0)+" "+"$"+seatsAudi1.get(1)*7);
System.out.println("Auditorium2"+" "+seatsAudi2.get(1)+" "+seatsAudi2.get(0)+" "+"$"+seatsAudi2.get(1)*7);
System.out.println("Auditorium3"+" "+seatsAudi3.get(1)+" "+seatsAudi3.get(0)+" "+"$"+seatsAudi3.get(1)*7);
System.out.println("Total"+" "+totalReserveSeats+" "+totalOpenSeats+" "+"$"+totalSale);
}
}
Main Class
import java.util.Scanner;
public class AuditoriumTester {
public static void main(String[] args) {
String ch = "";
String audiName;
Scanner sc = new Scanner(System.in);;
String[][] audi =null;
Auditorium auditorium = new Auditorium();
do{
System.out.println("*******Select an Auditorium**********");
System.out.println("Auditorium 1: Enter 1");
System.out.println("Auditorium 2: Enter 2");
System.out.println("Auditorium 3: Enter 3");
System.out.println("Exit: Enter 4");
System.out.println("**************************************");
System.out.print("Your Input: ");
String choice = sc.next();
if(choice.equals("1")){
audi = auditorium.readAuditorium("audi1.txt");
audiName="audi1.txt";
}
else if(choice.equals("2")){
audi = auditorium.readAuditorium("audi2.txt");
audiName="audi2.txt";
}else if(choice.equals("3")){
audi = auditorium.readAuditorium("audi3.txt");
audiName="audi3.txt";
}else
break;
//Display the selected auditorium.
auditorium.displayAuditorium(audi);
System.out.println("Select your seats");
System.out.print("Row Number: ");
int row = sc.nextInt();
System.out.println();
System.out.print("Starting Seat Number: ");
int seat = sc.nextInt();
System.out.println();
System.out.print("Number of tickets: ");
int quantity = sc.nextInt();
if(auditorium.checkAvailability(audi, row, seat, quantity)){
System.out.println("Do you want to reserve seat(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
auditorium.reserveSeats(audi, row, seat, quantity);
}else{
continue;
}
}else{
int bestSeat = auditorium.bestAvailable(audi, row, quantity);
if(bestSeat==0){
System.out.println("No seat available for your selection in this row. Please try another row or change auditorium");
System.out.println("Do you want to continue(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
continue;
}else{
break;
}
}
System.out.println("Your current choice is not available. However, you can choose seat: "+bestSeat);
System.out.print("Do you want to reserve seat(Y/N):");
String reserve = sc.next();
if(reserve.equalsIgnoreCase("y")){
auditorium.reserveSeats(audi, row, bestSeat, quantity);
}else{
continue;
}
}
auditorium.printReport(audi, audiName);
System.out.print("Do you want to continue(y/n): ");
ch = sc.next();
}while(ch.equalsIgnoreCase("y"));
sc.close();
}
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.