In: Computer Science
JAVA: Le Chef Heureux Restaurant has 20 tables that can be reserved at 5 p.m., 7 p.m., or 9 p.m. Design a JAVA program that accepts reservations for specific tables at specific times; the user enters the number of customers, the table number, and the time. Do not allow more than four guests per table or invalid table numbers or times. If an attempt is made to reserve a table already taken, re-prompt the user. Continue to accept reservations until the user enters a sentinel value or all slots are filled. Then display all empty tables in each time slot.
The answer should be a JAVA program this is the second time I'm asking this question because the first answer was written in python. A java program is needed, written in the Java programming language.
Here is the Java code:
import java.util.Scanner;
public class Main
{
public static void main(String[] args) {
int[][] guests = new int[3][20];
String[] timesToShow = {"5 pm.", "7 pm.", "9 pm."};
for (int i = 0; i < 3; i++)
for (int j = 0; j < 20; j++)
guests [i][j] = 0; // All unbooked initially
int maxReservations = 60, curntReservations = 0;
Scanner sc = new Scanner (System.in);
do {
System.out.println(" Le Chef Heureux Restaurant Reservations");
System.out.println(" ---------------------------------------");
System.out.print (" Please enter R to make a reservation, any other key to quit: ");
String s = sc.nextLine ().trim().toLowerCase ();
if (!s.equals("r")) break;
System.out.print (" Please enter time for reservation (5/7/9): ");
int tim = sc.nextInt();
System.out.print (" Please enter table number 1-20: ");
int tabl = sc.nextInt();
System.out.print (" Please enter number of guests 1-4: ");
int g = sc.nextInt();
sc.nextLine (); // clear up input buffer
if ((tim != 5) && (tim != 7) && (tim != 9)) {
System.out.println (" Invalid time entered, please retry.");
continue;
}
if ((tabl < 1) || (tabl > 20)) {
System.out.println (" Invalid table entered, please retry.");
continue;
}
if ((g < 1) || (g > 4)) {
System.out.println (" Invalid guests entered, please retry.");
continue;
}
int i = (tim == 5? 0 : (tim == 7? 1 : 2));
if (guests [i][tabl-1] == 0) {
guests [i][tabl-1] = g;
System.out.println (" Table booked.");
curntReservations++;
}
else {
System.out.println (" Table already reserved by somebody else, please try again.");
}
} while (curntReservations < maxReservations);
for (int i = 0; i < 3; i++) {
System.out.println (" Empty tables at " + timesToShow[i] + " (if any) ");
for (int j = 0; j < 20; j++) {
if (guests[i][j] == 0)
System.out.print (j+1);
System.out.print (" ");
}
System.out.println ();
}
}
}