Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

NEED TO TRANSFER THIS CODE FROM JAVA TO C++ 3. TicketManager.java import java.io.*; import java.util.*; public class TicketManager { // instance Variables: // the size

NEED TO TRANSFER THIS CODE FROM JAVA TO C++

3. 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 loop to control what columns it checks, i which equals their desired column start. // Then goes untilit checks the amounr of tickets they want by using j - numTickets. 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 // i dont get why it is a boolean, it just updates stuff doesn't return a true/false, // returning that can be done in main (i.e. if Y, then CLASS.purchaseTickets(....) not this way). 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(" "); } // 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 _______________________________ Seats Sold: " + seatsSold + " Seats available: " + (450-seatsSold) + " Total revenue to date: $" + totalRevenue);

} }

*******************************************************************************************

4. 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();

do { // display menu printMenu(); // ask the user to choose a command System.out.println(" Please 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(" Desired row (1 - 15): "); rowDesired = scan.nextInt(); System.out.print(" Desired 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(); // well its supposed to be an input but u dont use it as one so just setting it to whatever then //double amountPaid = 200;

// 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 1. View Available Seats 2. Request Tickets 3. Display Theater Sales Report 4. Exit the Program"); } // end printMenu method } // end class

Compile: javac Assignment8.java

Execute: java Assignment8 (or) java -Xms128M -Xms16M

Assignment8+

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

DATABASE Administrator Make A Difference

Authors: Mohciine Elmourabit

1st Edition

B0CGM7XG75, 978-1722657802

More Books

Students also viewed these Databases questions