Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Your job is to create a reservation system for a restaurant. The restaurant has

ID: 3591715 • Letter: Y

Question

Your job is to create a reservation system for a restaurant. The restaurant has 20 tables. Here is the functionality required for this system (you may want to display a menu and let user choose options 1 to 4, make sure to put your program in a loop so program does not exit until user chooses menu 0):

1- Reserve a Table
   User needs to input the table number (Tables are numbered from 0 to 19). If the table is not available (reserved), inform the user that the selected table is not available. If the table is available, then ask for the name (name is a single word, no space) and mark the table as reserved.
  
2- Clear Reservation
   User needs to input the table number (Tables are numbered from 0 to 19). If the table is not reserved, inform the user that the selected table is already available (nothing to clear). If the table is reserved, mark it as available. That table is no longer reserved.

3- Report
   Prints out the state of each table. Each table is printed on a separate line -- so your report will print out 20 rows. If reserved, it will print out the name on the reservation next to the table number. If available, it should print out "available".
  
0- Exit Program.

I would recommend either:

a) keeping two arrays (parallel arrays!), both are size 20, one of type boolean (true/false -- reserved/available) and the other one of type string (name on reservation if reserved, blank otherwise).

b) keeping a single array of size 20 of type string. At the beginning of the program, set all the tables to "AVAILABLE". As tables get reserved, set the table array item as the name on the reservation . If the value on a table is "AVAILABLE" then the table is available (not reserved) otherwise, the table is reserved and the value is the name on the reservation.

NOT THIS ANSWER BELOW (It's not working):

import java.util.Scanner;

public class restaurant

{

static Scanner sc=new Scanner(System.in);

static boolean[] check=new boolean[20];

static String[] name=new String[20];

public static void reserve()

{

System.out.print(" Enter the table number[0 to 19]:");

int num=sc.nextInt();

String na;

if(num<20 && num>0)

{

if(check[num]==true)

{

System.out.println("The selected table is not available.");

}

else

{

System.out.print("Enter the name:");

na=sc.next();

name[num]=na;

check[num]=true;

System.out.println("Table "+num+" is reserved for you.");

}

}

else

{

System.out.println("Please enter the number between 0 to 19.");

}

}

public static void clear()

{

System.out.print(" Enter the table number[0 to 19]:");

int num=sc.nextInt();

if(num<20 && num>0)

{

if(check[num]==true)

{

name[num]=null;

check[num]=false;

System.out.println("Table "+num+" is cleared.");

}

else

{

System.out.print("The selected table is already available (nothing to clear).");

}

}

else

{

System.out.println("Please enter the number between 0 to 19.");

}

}

public static void report()

{

for(int i=0;i<20;i++)

{

if(check[i]==true)

{

System.out.println("Table number:"+i+" "+name[i]);

}

else

{

System.out.println("Table number:"+i+" Available");

}

}

}

public static void main(String []args)

{

int ch;

System.out.println("Reservation system of a restaurant");

do

{

System.out.print(" 1.Reserve a table 2.Clear reservation 3.Report 0.Exit");

System.out.print(" Enter your choice:");

ch=sc.nextInt();

switch(ch)

{

case 1:reserve();

break;

case 2:clear();

break;

case 3:report();

break;

case 0:System.out.println("Bye!");

System.exit(0);

default :System.out.println("Invalid choice.");

}

}while(ch!=0);   

}

}

Explanation / Answer

import java.util.Scanner;
public class Restaurant
{
static Scanner sc=new Scanner(System.in);
static boolean[] check=new boolean[20];
static String[] name=new String[20];
public static void reserve()
{
System.out.print(" Enter the table number[0 to 19]:");
int num=sc.nextInt();
String na;
if(num<20 && num>=0)
{
if(check[num]==true)
{
System.out.println("The selected table is not available.");
}
else
{
System.out.print("Enter the name:");
na=sc.next();
name[num]=na;
check[num]=true;
System.out.println("Table "+num+" is reserved for you.");
}
}
else
{
System.out.println("Please enter the number between 0 to 19.");
}
}
public static void clear()
{
System.out.print(" Enter the table number[0 to 19]:");
int num=sc.nextInt();
if(num<20 && num>=0)
{
if(check[num]==true)
{
name[num]=null;
check[num]=false;
System.out.println("Table "+num+" is cleared.");
}
else
{
System.out.print("The selected table is already available (nothing to clear).");
}
}
else
{
System.out.println("Please enter the number between 0 to 19.");
}
}
public static void report()
{
for(int i=0;i<20;i++)
{
if(check[i]==true)
{
System.out.println("Table number:"+i+" "+name[i]);
}
else
{
System.out.println("Table number:"+i+" Available");
}
}
}
public static void main(String []args)
{
int ch;
System.out.println("Reservation system of a restaurant");
do
{
System.out.print(" 1.Reserve a table 2.Clear reservation 3.Report 0.Exit");
System.out.print(" Enter your choice:");
ch=sc.nextInt();
switch(ch)
{
case 1:reserve();
break;
case 2:clear();
break;
case 3:report();
break;
case 0:System.out.println("Bye!");
System.exit(0);
default :System.out.println("Invalid choice.");
}
}while(ch!=0);
}
}

Your code had some logical errors but now its working properly.