Question: I'm having an issue with my theater program. the main issue is when the user inputs a price. i want the code to assign them

I'm having an issue with my theater program. the main issue is when the user inputs a price. i want the code to assign them a seat and mark the seat as zero. But the code keeps looping and sets multiple seats to zero. The issue i believe is in the "seatByPrice" method.

import java.util.Scanner;

/** * * */ public class SeatingChart { Scanner in = new Scanner(System.in); int userRow; int userCol;

int [][] ticketPrices = { {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}, {0, 30, 40, 50, 50, 50, 50, 50, 50, 50, 50, 40, 30}, {0, 30, 30, 40, 40, 50, 50, 50, 50, 40, 40, 30, 30}, {0, 20, 30, 30, 40, 40, 40, 40, 40, 40, 30, 30, 20}, {0, 20, 30, 30, 40, 40, 40, 40, 40, 40, 30, 30, 20}, {0, 20, 20, 30, 30, 40, 40, 40, 40, 30, 30, 20, 20}, {0, 20, 20, 30, 30, 40, 40, 40, 40, 30, 30, 20, 20}, {0, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 10, 10}, {0, 10, 10, 20, 20, 20, 20, 20, 20, 20, 20, 10, 10}, {0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}, {0, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10, 10}}; /** * A method that prints the array of ticket prices via a for loop */ public void displaySeats() { for (int i = 1; i < ticketPrices.length; i++) { for (int j = 1; j < ticketPrices[i].length; j++) { System.out.print(ticketPrices[i][j] + " "); } System.out.println(); } System.out.println(); } /** * this method takes an input of row and col and changes the value of the * array to 0 because that seat has been purchased * @param row is a user input of row of the theater * @param col is a user input of column of the theater */ public void updateSeats(int row, int col) { ticketPrices[row][col] = 0; } /** * */ public void seatsByPrice() { System.out.println("Please choose a seat by price: $10, $20, $30, $40, $50"); System.out.println("Enter an integer only"); int seatPrice = in.nextInt(); // user types in desired price for (int i = 1; i < ticketPrices.length; i++) { for (int j = 1; j < ticketPrices[i].length; j++) { if (ticketPrices[i][j] == seatPrice) { updateSeats(i,j); System.out.println("seat: " + i + " " + j); break; } break; } } displaySeats(); } public void seatsByLocation() { System.out.println("Row one begins at the bottom of the theater where all the seats are $10."); System.out.println("Column one begins at the most left of the theater."); System.out.println("Please enter the row and column of the seat you would like seperated by a space"); userRow = in.nextInt(); // userRow is the 1st int the user input userCol = in.nextInt(); // userCol is the 2nd int the user input userRow = 10 - (userRow - 1); // reverses the user input row to match how the array is actually printed // System.out.println(userRow + " " + userCol); updateSeats(userRow, userCol); // update the the theater after the user picks a seat displaySeats(); } public void theaterMenu() { System.out.println(" Theater Menu"); System.out.println("------------------------------------------------"); System.out.println("(S) If you would like to pick by location"); System.out.println("(P) If you would like to pick a seat by price"); System.out.println("(D) If you would like to view seat display"); System.out.println("(Q) If you would like to quit "); } /** * @param args the command line arguments */ public static void main(String[] args) { char userOption; // variable that represents the user chosen option from the menu SeatingChart theater = new SeatingChart(); // object created to call methods from the SeatingChart method System.out.println("Hi. Welcome to the FIU theater!"); boolean picking = true; while (picking) { System.out.println("Please choose a seat"); System.out.println(); theater.theaterMenu(); System.out.println(); userOption = theater.in.next().charAt(0); //scans the user char input if ((userOption == 's') || (userOption == 'S')) { theater.seatsByLocation(); } else if ((userOption == 'p') || (userOption == 'P')) { theater.seatsByPrice(); } else if ((userOption == 'd') || (userOption == 'D')) { theater.displaySeats(); } else if ((userOption == 'q') || (userOption == 'Q')) { System.out.println("You chose to quit. Goodbye."); picking = false; } else { System.out.println("Error: Incorrect input"); } } } }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!