Hi so here is the problem my code only works have way as to what the specificati
ID: 3815899 • Letter: H
Question
Hi so here is the problem my code only works have way as to what the specifications are. They are listed in the comments above each method, First printing out the the array listed with rows and alphabetical chairs. * Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing (but makes changes to the 2D array)
*
* What it does:
* It lets the user know how many rows of seats and seats per row there are ???Visually
* so that the user knows what seats s/he can choose from
* It asks the user how many seat s/he wants to reserve *** if number of seats is unavalable then tell them
* It prompts the user to choose the number of seats s/he specified specifiy via 1a, 3e
* If all these seats are available,
* they are marked as reserved (by putting a 0 in the array
* as shown in the lab description), and
* the program prints out the transaction's total price
* IF seates are taken the program should let the user know that some or
* all seats are already reserved (and lists them),
* proposes to the user to make another reservation (proceeds if user agrees, exits otherwise)
* if the user is willing to modify the request:
* ask again
* otherwise
* print out that no transaction took place
* exit
* Keep doing this until the user either decides not to
* modify the request or the request is valid
If This part could work then it would at least pass
###################################################################
import java.io.*;
import java.util.Scanner;
import java.util.*;
public class lab12 {
/************************************************************************************************************
* The following method is not for you to modify.
* It is given to you because it is needed in the main
* method.
* It takes two integers, which represent a number of rows
* and a number of seats per row
* It generates and returns a 2D array of integers of size
* row x seats, which contains integer prices for each
* corresponding seat
*********************************************************/
public static int[][] loadInfo(int row, int seats) {
int[][] seatsPrices = new int[row][seats];
//assigns prices to each seat in row
for (int i=0; i<row; i++) {
for (int j=0; j< seats; j++) {
// return s a decimal to the nearest tenth times and (int) to make it a integer
seatsPrices[i][j]=(int) Math.floor(Math.random() * 101);
// if less than 30 prices are set to 30$
if (seatsPrices[i][j] < 30) seatsPrices[i][j]+=30;
System.out.print(seatsPrices[i][j] + " ");
}
System.out.println();
}
return seatsPrices;
}
/************************************************************************************************************
* Method: takeReservation
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing (but makes changes to the 2D array)
*
* What it does:
* It lets the user know how many rows of seats and seats per row there are
* so that the user knows what seats s/he can choose from
* It asks the user how many seat s/he wants to reserve
* It prompts the user to choose the number of seats s/he specified
* If all these seats are available,
* they are marked as reserved (by putting a 0 in the array
* as shown in the lab description), and
* the program prints out the transaction's total price
* and proposes to the user to make another reservation (proceeds if user agrees, exits otherwise)
* Otherwise,
* the program should let the user know that some or
* all seats are already reserved (and lists them), and
* if the user is willing to modify the request:
* ask again
* otherwise
* print out that no transaction took place
* exit
* Keep doing this until the user either decides not to
* modify the request or the request is valid
*********************************************************/
public static void takeReservation(int[][] seats) {
Scanner input = new Scanner(System.in);
System.out.println("Welcome to the Plaza Theater's seat reservation system!");
// tell user about number of rows and seats and then asks them to input the number of seats they want
int c = 15;
System.out.println("There are" + "Number of rows :"+seats.length+" " + "and A - " + (char)(c +seats[0].length) + "of seats per Row"); //number of seats in each row;
System.out.println("Please enter the number of seats you would like to reserve:");//
int noOfSeats = 0;
noOfSeats = input.nextInt();
int seatNums[] = new int[noOfSeats]; //declaring a new array based on the number pf seats input
String str = "";
while (!str.equalsIgnoreCase("exit")) {
int selectedRow[] = new int[noOfSeats];// declaring a new arrayto list seats wanted setting it to the maximum number of seats you could possibly have in an array
// invalid seat number
boolean notSelected=true;
for (int i=0; i<seats.length && notSelected; i++) {
showAvailableSeats(seats, i); // next method showing number of seats available
int yesNo=0;
System.out.println("Enter 1 if you wish to select from this Row else Enter 0");
yesNo=input.nextInt();
if (yesNo == 1){
for (int j=0; j<noOfSeats; j++) {
selectedRow[j] = i+1;
System.out.println(" Enter seat number "+ (j+1)+" :");
int seatNum = input.nextInt()-1;// -1 brcause we are going by the index
System.out.println("selected row" + (i+1) + " seat:"+(seatNum+1)); //again be cause of the indexing
seatNums[j]=seatNum+1; //
}
notSelected=false;
}
}
//making reservation
int sum = 0;
for (int i = 0; i<noOfSeats; i++) {
sum = sum + seats[selectedRow[i]][seatNums[i]]; // adding the prices of the selected seats
seats[selectedRow[i]][seatNums[i]]=0; //replacing the seats you resaerved with zeros
}
System.out.println("*************************************************");
print(seats);
System.out.println("*************************************************");
System.out.println("Transaction Reciept");
for ( int i=0 ; i<noOfSeats; i++){
System.out.println("SEAT " + (i+1) + ":Row:" + selectedRow[i] + ":seat:" + (seatNums[i]));
}
System.out.println("total price of the tickets $" + sum);
System.out.println("Do you want to make another transaction,type : yes Otherwise type exit to quit ");
str =input.next();
}
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 2 integers (min and max) that represent the lower
* and upper bound for the price of seats of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* price range specified by [min, max]
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int min, int max) {
int count = 0;
for (int i = 0; i < seats.length; i++) {
for (int j = 0; j < seats[0].length; j++) {
// your code goes here
if (seats[i][j] >= min && seats[i][j] <= max && seats[i][j]!=0) { // if seats are more than min and less than max and they are not labled zero
count++;
System.out.print(seats[i][j]+" ");
}
}
}
System.out.println();
if (count == 0) System.out.println("It looks like there is no seat that matches your price requirements."); // if the variable stored is = 0
}
/************************************************************************************************************
* Method: showAvailableSeats
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* and 1 integer (row) that represent the row of the seats
* of interest
* Returns: nothing
*
* What it does:
* It prints out all the seats in the 2D array that are within the
* given row and available
* If there is no such seat, the user is told so.
*********************************************************/
public static void showAvailableSeats(int[][] seats, int row) {
int seatsAva = 0;
for (int j = 0; j < seats[0].length; j++) {
if(seats[row][j]!=0){ // if place in array is not zero
seatsAva++;
System.out.print(seats[row][j]+" ");
}
}
System.out.println();
if (seatsAva == 0) System.out.println("It looks like there is no seat available in row " + row + ".");
}
/************************************************************************************************************
* Method: copy (non recursive: using loops)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: a 2D array which is a replica of the input 2D array
*********************************************************/
public static int[][] copy(int[][] seats) {
int[][] backUp = new int[seats.length][seats[0].length];
for(int i=0;i<seats.length;i++)
for(int j=0;j<seats[0].length;j++)
{
backUp[i][j] = seats[i][j];
}
return backUp;
}
/************************************************************************************************************
* Method: copyR (recursive: no loop)
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Creates (but does not return): a 2D array which is a replica of the input 2D array
*
* copyR uses what is at times called an "auxiliary" method: copyRowR
* which copies a given row of the input of copyR into the expected output 2D array of copyR
*********************************************************/
public static void copyR(int[][] seats, int[][] result, int start) {
// your code goes here
if (start >= seats.length) return;
result[start] = seats[start];
copyR(seats, result, start+1);
}
/************************************************************************************************************
* Method: copyRowR (recursive: no loop)
* Takes: a 1D array that represents a row of the input of method copyR
* Creates (but does not return): a 1D array which is a replica of the input 1D array
*********************************************************/
public static void copyRowR(int[] seatsRow, int[] resultRow, int init) {
if (init >= seatsRow.length) return;
resultRow[init] = seatsRow[init];
copyRowR(seatsRow, resultRow, init+1);
}
/************************************************************************************************************
* Method: print
* Takes: a 2D array that contains seats (the number of
* rows and columns) and prices for each seat
* (the integer stored in each location of the array)
* Returns: nothing
* Prints the content of the input 2D array (row by row)
*********************************************************/
public static void print(int[][] A) {
// your code goes here
System.out.println("Printing backup");
for(int i=0;i<A.length;i++) { // Per row
for(int j=0;j<A[0].length;j++) { // Per seat in each row
System.out.print(A[i][j]+" "); // Prints Array and a space between elements of Array
}
System.out.println();
}
}
/***************************************************************************************************************
* This is the main method, from which all above methods are being used
* Do not modify this method.
***************************************************************************************************************/
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
String answer;
int min, max, row;
// load a 2D array of prices
int[][] seatsAndPrices = loadInfo(5,10);
// copy seatsAndPrices in a backup 2D array in case we need to change reservations
int[][] backUp = copy(seatsAndPrices);
// int[][] backUp = new int[seatsAndPrices.length][seatsAndPrices[0].length];
copyR(seatsAndPrices, backUp, 0);
print(backUp);
// runs the method that shows seats within a given price range
System.out.println("Would you like to see the seats options within a chosen price range?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What is the max anount you are ready to pay for a seat?");
max = in.nextInt();
System.out.println("What is the lowest price you'd like to consider?");
min = in.nextInt();
showAvailableSeats(seatsAndPrices, min, max);
}
// runs the method that shows seats within a given row
System.out.println("Would you like to see the available seats in a given row?");
answer = in.next();
if (answer.toLowerCase().compareTo("yes")==0) {
System.out.println("What row are you interested in?");
row = in.nextInt();
showAvailableSeats(seatsAndPrices, row);
}
// runs the reservation method
takeReservation(seatsAndPrices);
}
}
Explanation / Answer
Sorry to say this is not quite a good way to do it. It is better to use objects like I did in my code.
Seat.java
====
public class Seat {
// Reserved or not.
private boolean reserved;
private int price;
//private int seatNum;
//private static seatCount = 0;
/** CONSTRUCTOR **/
public Seat() {
reserved = false;
price = 0;
//seatNum = ++seatCount;
};
// Getters
public boolean getReserved() { return this.reserved; }
public int getPrice() { return this.price; }
//public int getSeatNum() { return this.seatNum; }
// Setters
public void setReserved(boolean res) { this.reserved = res; }
public void setPrice(int price) { this.price = price; }
//public void setSeatNum(int seatNum) { this.seatNum = seatNum; }
public String toString() {
return reserved;
}
}
SeatReservation.java
====
import java.lang.NullPointerException;
public class SeatReservation {
int numRows;
int numCols;
public Seat[][] seats;
/** CONSTRUCTOR **/
public SeatReservation() {
numRows = 10;
numCols = 10;
};
/**
* Generates seats according to the given number of rows and columns.
* @param { int } rows - Number of rows in the seating.
* @param { int } cols - Number of columns in the seating.
*/
public void generateSeats(int rows, int cols) {
this.seats = new Seat[rows][cols];
for (int i = 0; i < rows; ++i)
for (int j = 0; j < cols; ++j)
this.seats[i][j] = new Seat();
};
/**
* Prints seats to the screen.
*/
public void showSeats() {
try {
for (int i = 0; i < numRows; ++i) {
System.out.print("Row" + i + " ");
for (int j = 0; j < numCols; ++j) {
System.out.println(this.seats[i][j].getReserved());
/*
if (! this.seats[i][j].getReserved())
System.out.print(" " + j + " ");
else
System.out.print(" X ");
*/
}
System.out.println(" ");
}
} catch (NullPointerException err) {
System.out.println(err);
}
};
/**
* Reserves a seat given its row and column numbers.
* @param { int } rowNum - Rownumber of the seat.
* @param { int } colNum - Colum number of the seat.
*/
public void reserveSeat(int rowNum, int colNum) {
seats[rowNum][colNum].setReserved(true);
};
}
SeatReservationDriver.java
====
import java.util.Scanner;
public class SeatReservationDriver {
public static void main(String args[]) {
Scanner cin = new Scanner(System.in);
SeatReservation myTheatre = new SeatReservation();
myTheatre.generateSeats(10, 10);
myTheatre.showSeats();
System.out.println( "Enter row number of seat to reserve: " );
int rows = cin.nextInt();
System.out.println( "Enter seat number to reserve: " );
int cols = cin.nextInt();
myTheatre.reserveSeat(rows, cols);
System.out.println("====");
myTheatre.showSeats();
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.