In: Computer Science
I want this program written in JAVA with the algorithm(The step by step process for the problem) . Please help it is due in a couple of hours. I don't want the C++ program, I want it in JAVA please
#20 Theater Ticket Sales
Create a TicketManager class and a program that uses it to sell tickets for a single performance theater production.
Here are the specifications:
• The theater's auditorium has 15 rows, with 30 seats in each row.
To represent the seats, the TicketManager class should have a
two-dimensional array of SeatStructures. Each of these structures
should have data members to keep track of the seat's price and
whether or not it is available or already sold.
• The data for the program is to be read in from two files located
in the Chapter 8 programs folder on this book's companion website.
The first one, SeatPrices.dat, contains 15 values representing the
price for each row. All seats in a given row are the same price,
but different rows have different prices. The second file,
SeatAvailability. dat, holds the seat availability information. It
contains 450 characters (15 rows with 30 characters each),
indicating which seats have been sold (' • ') and which are
available ( '#' ). Initially all seats are available. However, once
the program runs and the file is updated, some of the seats will
have been sold. The obvious function to read in the data from these
files and set up the array is the constructor that runs when the
TicketManager object is first created.
• The client program should be a menu-driven program that provides
the user with a menu of box office options, accepts and validates
user inputs, and calls appropriate class functions to carry out
desired tasks. The menu should have options to display the seating
chart, request tickets, print a sales report, and exit the
program.
• When the user selects the display seats menu option, a
TicketManager function should be called that creates and returns a
string holding a chart, similar to the one shown here. It should
indicate which seats are already sold ( *) and which are still
available for purchase (#) . The client program should then display
the string.
Seats 1234567890 12345678901234567890
Row 1 ***###***###******############
Row 2 ####*************####*******##
Row 3 **###**********########****###
Row 4 **######**************##******
Row 5 ********#####*********########
Row 6 ##############************####
Row 7 #######************###########
Row 8 ************##****############
Row 9 #########*****############****
Row 10 #####*************############
Row 11 #**********#################**
Row 12 #############********########*
Row 13 ###***********########**######
Row 14 ##############################
Row 15 ##############################
• When the user selects the request tickets menu option , the
program should prompt for the number of seats the patron wants, the
desired row number, and the desired starting seat number. A
TicketManager ticket request function should then be called and
passed this information so that it can handle the ticket request.
If any of the requested seats do not exist, or are not available,
an appropriate message should be returned to be displayed by the
client program. If the seats exist and are available, a string
should be created and returned that lists the number of requested
seats, the price per seat in the requested row, and the total price
for the seats. Then the user program should ask if the patron
wishes to purchase these seats.
• If the patron indicates they do want to buy the requested seats ,
a TicketManager purchase tickets module should be called to handle
the actual sale. This module must be able to accept money, ensure
that it is sufficient to continue with the sale, and if it is, mark
the seat(s) as sold, and create and return a string that includes a
ticket for each seat sold (with the correct row, seat number, and
price on it).
• When the user selects the sales report menu option, a
TicketManager report module should be called. This module must
create and return a string holding a report that tells how many
seats have been sold, how many are still available, and how much
money has been collected so far for the sold seats. Think about how
your team will either calculate or collect and store this
information so that it will be available when it is needed for the
report.
• When the day of ticket sales is over and the quit menu choice is
selected, the program needs to be able to write the updated seat
availability data back out to the file. The obvious place to do
this is in the TicketManager destructor.
Program code to copy
Assignment8.java
import java.util.*;
public class Assignment8
{
public static void main(String[] args)
{
// declare variables to control
menu
int usersChoice = 0;
Scanner scan = new
Scanner(System.in);
// declare and instantiate a
TicketManager object:
TicketManager objectDone = new
TicketManager();
// display menu
printMenu();
do
{
//
ask the user to choose a command
System.out.println("\nPlease enter a command:");
usersChoice = scan.nextInt();
switch
(usersChoice)
{
case 1:
// just has to print the seats
objectDone.displaySeats();
break;
case 2:
// three variables, seatsDesired, rowDesired,
startingSeat
int seatsDesired = 0;
int rowDesired = 0;
int startingSeat = 0;
// asks the three respective questions:
System.out.print("Number of seats desired (1 -
30): ");
seatsDesired = scan.nextInt();
System.out.print("\nDesired row (1 - 15):
");
rowDesired = scan.nextInt();
System.out.print("\nDesired starting seat number
in the row (1 - 30): ");
startingSeat = scan.nextInt();
// check if available
if(objectDone.requestTickets(seatsDesired,
rowDesired, startingSeat))
System.out.println("The seats
you have requested are available for purchase. ");
else
{
System.out.println("Unfortunately, the seats you have requested are
not available for purchase.");
break;
}
// Ask if they would like to buy, store in
variable answer
char answer = ' ';
System.out.print("Do you wish to purchase these
tickets (Y/N)? ");
answer = scan.next().charAt(0);
// if yes, print out like the receipt = purchase
tickets method, then print stuff, then prink tickets.
if (answer == 'Y' || answer == 'y')
{
// variable for total ticket
cost
double totalTicketCost =
(seatsDesired*objectDone.getPrice(rowDesired));
// print out stuff before the
ticket print
objectDone.purchaseTickets(seatsDesired, rowDesired,
startingSeat);
System.out.println("Num
Seats: " + seatsDesired);
System.out.println("The price
for the requested tickets is $" + totalTicketCost);
System.out.println("Please
input amount paid: ");
// amount to use as payment
store in double
double amountPaid =
scan.nextDouble();
// print the ticket
objectDone.printTickets(seatsDesired, rowDesired,
startingSeat);
// summary of stuff after
ticket
System.out.println("Tickets
purchased: " + seatsDesired);
System.out.println("Payment\t\t\t: $ " + amountPaid);
System.out.println("Total
ticket price \t: $" + totalTicketCost);
System.out.println("Change
due\t\t: $" + (amountPaid - totalTicketCost));
}
else
break;
break;
case 3:
objectDone.displaySalesReport();
break;
case 4:
System.exit(0);
break;
default:
System.out.println("Invalid choice!");
break;
}
} while (usersChoice != 4);
scan.close();
} // end main method
public static void printMenu()
{
System.out.println("ASU Gammage
Theater\n1. View Available Seats\n2. Request Tickets\n3. Display
Theater Sales Report\n4. Exit the Program");
} // end printMenu method
} // end class
===================================================================================
TicketManager.java
import java.io.*;
import java.util.*;
public class TicketManager
{
// instance Variables:
// the size of 2D arrays determined by the following
variables, rows and columns respectively
public static final int NUMROWS = 15; // Do not use
const, it is final in Java...
public static final int NUMCOLUMNS = 30;
//2-D array to keep track of seats.
private char[][] seats = new
char[NUMROWS][NUMCOLUMNS];
// 1-d array to keep track of price.
private double[] price = new double[NUMROWS];
// int to keep track of seats sold
private int seatsSold = 0;
// double to keep track of total revenue
private double totalRevenue = 0.0;
// Constructor
// TicketManager makes the array seats the from
reading the file seatinability.txt
public TicketManager()
{
try
{
//make a new
scanner object that will read from the file
Scanner avail =
new Scanner(new File("seatAvailability.txt"));
String line;
int col = 0, row = 0;
while(avail.hasNext())
{
line = avail.next();
for(col = 0; col < line.length() && col < NUMCOLUMNS;
col++)
{
seats[row][col] = line.charAt(col);
}
row++;
}
// closes
avail scanner.
avail.close();
// Repeat above
for price array, only one for loop caz its 1-d
Scanner priceTxt
= new Scanner(new File("seatPrices.txt"));
while(priceTxt.hasNext())
{
for (int i = 0; i < NUMROWS; i++)
{
double
temp1 = priceTxt.nextDouble();
price[i] =
temp1;
}
}
// close
scanner
priceTxt.close();
}
catch (FileNotFoundException
e)
{
System.err.println("error: "+e.getMessage());
}
}
// requestTickets method
public boolean requestTickets(int numTickets, int
desiredRow, int desiredSeatStart)
{
for (int i = desiredSeatStart; i
< NUMROWS - numTickets; i++)
{
// if the seat
is taken (*) then say u cant use it, false.
if
(seats[desiredRow][i] == '*')
return false;
}
return true;
}
// method purchaseTickets changes the seats array and
updates seatsSold and totalRevenue
public void purchaseTickets(int
numTickets, int desiredRow, int desiredSeatStart)
{
// need to change the seats in the
2-d array from # to *.
// use same loop, as
requestTickets, but instead of if statement, change the char.
for (int i = desiredSeatStart; i
< NUMROWS - numTickets; i++)
{
// change seat
from available, #, to taken, *.
seats[desiredRow][i] = '*';
}
//update seats sold:
seatsSold += numTickets;
// update revenue (desired row also
equals the price from the array times the num of seats sold.
totalRevenue +=
(price[desiredRow]*numTickets);
}
// method getTotalRevenue() will return total
revenue
public double getTotalRevenue()
{
return totalRevenue;
}
// method to return the number of seats sold
public int getSeatsSold()
{
return seatsSold;
}
// method to get price of current row
public double getPrice(int row)
{
// from entered row, access double
row's price associated with that row
double priceOfRow =
price[row];
// return it.
return priceOfRow;
}
// method not very clear, assuming it is returning a #
or *.
public char getSeat(int row, int column)
{
char seatOfSelected =
seats[row][column];
return seatOfSelected;
}
// method to print the tickets purchased.
public void printTickets(int numSeats, int row, int
column)
{
// go through seat by seat and
print out each ticket using for loop
for (int i = column; i < NUMROWS
- numSeats; i++)
{
System.out.println("***********************************************");
System.out.println("* Gammage Theater*");
System.out.println("* Row " + row + " Seat " + i + " *");
System.out.println("* Price: $ " + price[row] + " *");
System.out.println("***********************************************");
}
}
// method displaySeats displays the content of the 2-d
array
public void displaySeats()
{
// print header, that requires no
changing
System.out.println("\t\t\tSeats");
System.out.println("\t123456789012345678901234567890");
// use for loop to print out row by
row and then its rows contents
for (int i = 0; i < NUMROWS;
i++)
{
// print out
what row you're in, dont go to next line.
System.out.print("Row " + (i+1) + "\t");
for (int j = 0;
j < NUMCOLUMNS; j++)
{
// print out each content of that row
System.out.print(seats[i][j]);
}
// go to new
line
System.out.print("\n");
}
// print out the legend
System.out.println("\tLegend: * =
Sold/Occupied");
System.out.println("\t# =
Available");
}
// method to display sales report
public void displaySalesReport()
{
// print heading up to the first
requirement of variable
System.out.println("Gammage Sales
Report \n _______________________________\n Seats Sold: " +
seatsSold +
"\nSeats available: " + (450-seatsSold) +
"\nTotal revenue to date: $" + totalRevenue);
}
}
===================================================================================
seatAvailability.txt
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
##############################
===================================================================================
seatPrices.txt
12.50
12.50
12.50
12.50
10.00
10.00
10.00
10.00
8.00
8.00
8.00
8.00
5.00
5.00
5.00
==================================================================================
Program Screenshot
Assignment8.java
================================================================================
TicketManager.java
============================================================================
seatAvailability.txt
================================================================================
seatPrices.txt
=================================================================================
Sample output