In: Computer Science
LastNameFirstNameWeek4.java Purpose: This program will take an order on a local beverage parlor. This beverage parlor offers a limited set of drinks by a glass. Any day of the week (except Mondays when they are closed) they offer three possible alternatives: “Juice”, “Milk”, or “Soda”. Additionally only Fridays, Saturdays and Sundays, the beverage parlor serve also “Beer” and “Wine”. The program should ask the users for her/his beverage choices and produce a summary of the order in the end. To do so, the program will initially get the name of a day in a week (Monday, Tuesday, Wednesday, etc.) from the user. If the day is Monday, the program should print “Sorry. This establishment is closed on Mondays. Your order includes nothing.” And the program will stop. If the day is Tuesday, Wednesday or Thursday, the program will ask the user: “Which beverages would you like to drink?: juice, milk, or soda?" and receive the answer. If the day is Friday, Saturday or Sunday, the program would ask to the user: "Which beverages would you like to drink?: juice, milk, soda, beer, or wine?" and get an answer. After every new beverage order, the program will ask if the user wants to order something else. If the user wants to order something else, the requesting process is repeated within a loop, but the day given at the beginning of the session will remain. Every time the user selects one of the beverages, a corresponding counter for each of these meals should be updated (adding 1 to it). When the user indicates s/he does not want to order more, the program will print the total order with the correct count of each beverage that was ordered and stop. If a beverage was not ordered, its name must not appear in the final list. Notice that responses from the user may be entered in any combination of uppercase or lowercase letters. The best will be to read all responses using the next() method of the Scanner, convert these responses to uppercase letters with the toUpperCase() method of the String class, and use this uppercase version of the response to do comparisons with the choices (also in upper case). Your output should be similar to the samples shown below. Do not change the order of the questions neither the messages that are displayed. Sample 1 Which day are you visiting the Restaurant (Monday, Tuesday, Wednesday, etc.)?Tuesday Which beverages would you like to drink?: juice, milk, or soda?milk Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, or soda?soda Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, or soda?juice Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, or soda?water This is an Invalid order. Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, or soda?milk Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, or soda?soda Do you want to add anything else to the order? (y/n) n Your order includes: 1 juice(s) 2 milk(s) 2 soda(s) Sample 2 Which day are you visiting the Restaurant (Monday, Tuesday, Wednesday, etc.)?Sunday Which beverages would you like to drink?: juice, milk, soda, beer, or wine?beer Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?wine Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?soda Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?beer Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?wine Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?milk Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?water This is an Invalid order. Do you want to add anything else to the order? (y/n) y Which beverages would you like to drink?: juice, milk, soda, beer, or wine?beer Do you want to add anything else to the order? (y/n) n Your order includes: 1 milk(s) 1 soda(s) 3 beer(s) 2 wine(s) Sample 3 Which day are you visiting the Restaurant (Monday, Tuesday, Wednesday, etc.)?Monday Sorry. This establishment is closed on Mondays Your order includes nothing. Sample 4 Which day are you visiting the Restaurant (Monday, Tuesday, Wednesday, etc.)?asasdadads Invalid day. Your order includes nothing.
If you have any problem with the program feel free to comment
Program
import java.util.Scanner;
public class LastNameFirstNameWeek4 {
public static void main(String[] args) {
Scanner sc = new
Scanner(System.in);//for taking console input
String result, day;
day =
getDay(sc).toUpperCase();
//chekcing day and showing menu
accordingly
if(day.equals("FIRDAY") ||
day.equals("SATURDAY") || day.equals("SUNDAY"))
result =
beerWineMenu(sc);
else
result =
normalMenu(sc);
System.out.println(result);//printing the result
}
private static String getDay(Scanner sc) {
String day;
System.out.print("Which day are you
visiting the Restaurant (Monday, Tuesday, Wednesday, etc.)?
");
day = sc.nextLine();
switch(day.toUpperCase()) {
case "MONDAY": //when its
monday
System.out.println("Sorry. This establishment is closed on Mondays
Your order includes nothing.");
System.exit(0);
case "TUESDAY": break;
case "WEDNESDAY": break;
case "THURSDAY": break;
case "FRIDAY": break;
case "SATURDAY": break;
case "SUNDAY": break;
default: //invalid input
System.out.println("Invalid day. Your order includes
nothing.");
System.exit(0);
}
return day;
}
private static String normalMenu(Scanner sc)
{
int[] ar = {0, 0, 0};//for storing
beverages count
String choice;
while(true) {
System.out.print("Which beverages would you like to drink?: juice,
milk, or soda? ");
String order =
sc.nextLine();
switch(order.toUpperCase()) {
//increamenting
beverages count accordingly
case "JUICE":
ar[0]++; break;
case "MILK":
ar[1]++; break;
case "SODA":
ar[2]++; break;
//invalid
input
default:
System.out.println("This is an Invalid order."); break;
}
//checking if
user want to order more
System.out.print("Do you want to add anything else to the order?
(y/n) ");
choice =
sc.nextLine();
if(choice.equalsIgnoreCase("n"))
break;
}
String result =
createResult(ar);
return result;
}
private static String beerWineMenu(Scanner sc)
{
int[] ar = {0, 0, 0, 0, 0};//for
storing beverages count
String choice;
while(true) {
System.out.print("Which beverages would you like to drink?: juice,
milk, soda, beer, or wine? ");
String order =
sc.nextLine();
switch(order.toUpperCase()) {
//increamenting
beverages count accordingly
case "JUICE":
ar[0]++; break;
case "MILK":
ar[1]++; break;
case "SODA":
ar[2]++; break;
case "BEER":
ar[3]++; break;
case "WINE":
ar[4]++; break;
//invalid
input
default:
System.out.println("This is an Invalid order."); break;
}
//checking if
user want to order more
System.out.print("Do you want to add anything else to the order?
(y/n) ");
choice =
sc.nextLine();
if(choice.equalsIgnoreCase("n"))
break;
}
String result =
createResult(ar);
return result;
}
//for creating the result
private static String createResult(int[] ar) {
String result= "Your order
includes: ";
if(ar.length<=3) {//when normal
menu is selected
if(ar[0] >
0)
result += ar[0]+" juice(s) ";
if(ar[1] >
0)
result += ar[1]+" milk(s) ";
if(ar[2] >
0)
result += ar[2]+" soda(s) ";
}
else {//when beer and wine menu is
selected
if(ar[0] >
0)
result += ar[0]+" juice(s) ";
if(ar[1] >
0)
result += ar[1]+" milk(s) ";
if(ar[2] >
0)
result += ar[2]+" soda(s) ";
if(ar[3] >
0)
result += ar[3]+" beer(s) ";
if(ar[4] >
0)
result += ar[4]+" wine(s) ";
}
return result;
}
}
Outputs