Question
Using Java P7.5 A theater seating chart is implemented as a two-dimensional array of ticket prices, like this: 10 10 10 10 10 10 10
Using Java
P7.5 A theater seating chart is implemented as a two-dimensional array of ticket prices, like this:
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 10 10 10 10 10 10 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
10 10 20 20 20 20 20 20 10 10
20 20 30 30 40 40 30 30 20 20
20 30 30 40 50 50 40 30 30 20
30 40 50 50 50 50 50 50 40 30
(Note that there are 9 rows and 10 columns).
Program Function
We will write a program that allows a user to pick a seat to purchase. The program will:
- Display the seating chart with the prices as a 9x10 table. Rows are numbered 1 thru 9 from the stage to the back and, in each row, seats are numbered 1 thru 10 from the left as seen from the stage facing the audience.
- Prompt the user to enter a value indicating if they want to pick a seat by location (1) or by price (2) or have the system pick the best seat available (3). The user may also enter a 0 to quit the program. If they want to pick by location, prompt for row and seat number. If by price, prompt for price (10,20,30,40, or 50).
- When a user specifies a seat by location or price, make sure it is available. If not, tell the user it is not available and give them another chance. If all seats are sold, tell the user and dont give them another chance.
- If the seat is available to mark it sold by changing the price to 0 in the table and tell the user:
- The price if the selection was by location; or
- The location if the selection was by price; or
- The price and the location if the selection was best available.
- After each seat is sold, re-display the current seating chart (showing the available seats).
- End the program when all seats are sold or when the user enters 0 to quit, whichever comes first.
Program Design
You will have two classes in this program:
- The TheaterSeatTester class that accepts seat requests from the user and services those requests with the appropriate method calls. It runs until all seats are sold or the user quits.
- The TheaterSeatSeller class that displays the seating chart on request, looks for available seats on request and marks requested seats sold
TheaterSeatTester Class
- Declare and initialize a test array with the seat price values shown in the table above and pass that array to a newly created TheaterSeatSeller object as a constructor parameter. (Seat prices may vary depending on what who/what the entertainment is and what day/time the shows are).
- Declare and initialize a totalSold counter to count the seats sold. The definition of no more seats available will be when totalSold equals the total number of seats in the theater.
- Declare and initialize any additional variables necessary to perform the functions below.
- Run (loop) to repeatedly find and mark sold seats in the theater until all are sold or the user enters 0 to quit.
- Display the current seating chart by calling the printSeats method.
- Prompt the user for input as described in the program function above.
- Edit the input received for validity and repeat the prompt until a valid response is received.
- If the input is 0 display a message (e.g. Goodbye) and quit the program.
- If the input is 1 invoke the getByLoc method, passing the row and seat requested.
- If the input is 2 invoke the getByPrice method passing the price requested.
- If the input is 3 invoke the getBest method.
- For every seat successfully sold (a good return from any of the get methods), increment the totalSold counter and use it to stop the program when all seats are sold.
TheaterSeatSeller Class
- Declare a 2D array named seats to be a matrix representing the rows and seats in the theater. This is the instance variable for the class. The array will be initialized in the class Constructor by copying the test array passed in the constructor parameter to seats. This will be a true copy, not a copy by reference.
- Code a constructor for the class that will accept an array reference of test values for the seat prices and use it to initialize seats.
- Code a printSeats method to display the formatted seating chart on request.
- Code a getByLoc method. It accepts two integer parameters for row and seat and returns a Boolean response indicating success or failure (seat was available or not). Use the row and seat to locate the corresponding price element in the array and determine availability (not available if 0). If available, display a message that the seat at that location (name the location) will be reserved and the price. Mark the seat sold by changing price to 0.
- Code a getByPrice method. It accepts a price (an integer, as prices will always be expressed as whole dollars), and returns a Boolean response indicating success or failure (seat was available or not). Use price to locate the first available seat in a row at that price closest to the front (stage). If available, display a message that the seat at that price (name the price) will be reserved and the location (row and seat number). Mark the seat sold by changing the price to 0.
- Code a getBest method. It has no input parameters and returns a Boolean response indicating success or failure (seat was available or not). Locate the best available seat where best in this case is defined as the highest price seat still availalable closest to the stage. The stage is located in front of the row with the most expensive seats (i.e. the last row in the array). (Note that the best available might not be in the first row even if there are seats available in the first row.) If available, display a message that the seat at that location and that price (name both) will be reserved. Mark the seat sold by changing the price to 0.
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started