In: Computer Science
Question:
Write a program that prompts users to pick either a seat or a price. Mark sold seats by changing the price to 0. When a user specifies a seat, make sure it is available. When a user specifies a price, find any seat with that price.
Code Provided:
(Please help me finish the line marked with "Complete this method", Please don't change other lines.)
Besides, if you could kindly mark each your line is for what purpose, I'll be very happy.
import java.util.Scanner;
public class SeatingChart
{
/**
Prints the price of seats in a grid like pattern.
@param seats a 2D array of prices
*/
public void printSeats(int[][] seats)
{
System.out.print(" ");
for (int j = 0; j < seats[0].length; j++)
{
System.out.print("s" + (j+1) + " ");
}
System.out.println();
for (int i = 0; i < seats.length; i++)
{
System.out.print("r" + (seats.length-i) + ":");
for (int j = 0; j < seats[i].length; j++)
{
System.out.printf("%3d", seats[i][j]);
}
System.out.println();
}
}
/**
Marks a seat with the price given to 0.
If there is no seat with the price, print an error message.
@param seats: the array of seat prices
@param price: the price to mark to zero
*/
public void sellSeatByPrice(int[][] seats, int price)
{
// COMPLETE THIS METHOD
// System.out.println("Sorry, no seat found with that
price.");
}
/**
Marks a seat based on a given row and seat number from input.
If seat or row numbers are invalid, print error messages.
If the seat is already occupied, print error message.
@param seats: the array of seat prices
@param rownum: row number
@param seatnum: seat number
*/
public void sellSeatByNumber(int[][] seats, int rownum, int
seatnum)
{
// COMPLETE THIS METHOD
//System.out.println("Sorry, seat already occupied.");
//System.out.println("Sorry, invalid seat number.");
//System.out.println("Sorry, invalid row.");
}
public static void main(String[] args)
{
// initial values come from problem description
int[][] seats = { { 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 } };
SeatingChart seating = new SeatingChart();
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or
<q> to quit: ");
Scanner in = new Scanner(System.in);
String choice = in.next();
while (!choice.equals("q"))
{
if (choice.equals("s"))
{
System.out.println("Enter the row number you want:
");
int rownum = in.nextInt();
System.out.println("Enter the seat number you want: ");
int seatnum = in.nextInt();
seating.sellSeatByNumber(seats, rownum, seatnum);
}
else
{
// pick by price
System.out.println("What price do you want to buy?");
int price = in.nextInt();
seating.sellSeatByPrice(seats, price);
}
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or
<q> to quit: ");
choice = in.next();
}
in.close();
}
}
PLEASE GIVE THUMBS UP, THANKS
SAMPLE OUTPUT :
code :
/**
*
* @author VISHAL
*/
import java.util.Scanner;
public class SeatingChart
{
/**
Prints the price of seats in a grid like pattern.
@param seats a 2D array of prices
*/
public void printSeats(int[][] seats)
{
System.out.print(" ");
for (int j = 0; j < seats[0].length; j++)
{
System.out.print("s" + (j+1) + " ");
}
System.out.println();
for (int i = 0; i < seats.length; i++)
{
System.out.print("r" + (seats.length-i) + ":");
for (int j = 0; j < seats[i].length; j++)
{
System.out.printf("%3d", seats[i][j]);
}
System.out.println();
}
}
/**
Marks a seat with the price given to 0.
If there is no seat with the price, print an error message.
@param seats: the array of seat prices
@param price: the price to mark to zero
*/
public void sellSeatByPrice(int[][] seats, int price)
{
// COMPLETE THIS METHOD
boolean seatfound=false;
int row=0,col=0;
for(int i=0; i<seats.length && seatfound==false;
i++)
{
for(int j=0; j<seats[0].length &&seatfound==false;
j++)
{
if(seats[i][j]==price)
{
seatfound=true;
row=i;
col=j;
seats[row][col]=0;
break;
}
}
}
if(seatfound)
{
System.out.println("Congratulation, you got seat with row no
:"+(seats.length-row)+" and seat no : "+(col+1));
}
else
{
System.out.println("Sorry, no seat found with that price.");
}
}
/**
Marks a seat based on a given row and seat number from input.
If seat or row numbers are invalid, print error messages.
If the seat is already occupied, print error message.
@param seats: the array of seat prices
@param rownum: row number
@param seatnum: seat number
*/
public void sellSeatByNumber(int[][] seats, int rownum, int
seatnum)
{
// COMPLETE THIS METHOD
if((rownum<1 || rownum>seats.length))
{
System.out.println("Sorry, invalid row.");
}
else if((seatnum<1 || seatnum>seats[0].length))
{
System.out.println("Sorry, invalid seat number.");
}
else
{
if(seats[seats.length-rownum][seatnum-1]==0)
{
System.out.println("Sorry, seat already occupied.");
}
else
{
seats[seats.length-rownum][seatnum-1]=0;
System.out.println("You got the Seat");
}
}
}
public static void main(String[] args)
{
// initial values come from problem description
int[][] seats = { { 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 } };
SeatingChart seating = new SeatingChart();
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or
<q> to quit: ");
Scanner in = new Scanner(System.in);
String choice = in.next();
while (!choice.equals("q"))
{
if (choice.equals("s"))
{
System.out.println("Enter the row number you want: ");
int rownum = in.nextInt();
System.out.println("Enter the seat number you want: ");
int seatnum = in.nextInt();
seating.sellSeatByNumber(seats, rownum, seatnum);
}
else
{
// pick by price
System.out.println("What price do you want to buy?");
int price = in.nextInt();
seating.sellSeatByPrice(seats, price);
}
seating.printSeats(seats);
System.out.println("Pick by <s>eat or <p>rice or
<q> to quit: ");
choice = in.next();
}
in.close();
}
}