a. Write an application that displays a menu of three items in a restaurant as f
ID: 3905311 • Letter: A
Question
a. Write an application that displays a menu of three items in a restaurant as follows:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Prompt the user to choose an item using the number (1, 2, or 3) that corresponds to the item, or to enter 0 to quit the application. After the user makes the first selection, if the choice is 0, display a bill of $0. Otherwise, display the menu again. The user should respond to this prompt with another item number to order or 0 to quit. If the user types 0, display the cost of the single requested item. If the user types 1, 2, or 3, add the cost of the second item to the first, and then display the menu a third time. If the user types 0 to quit, display the total cost of the two items; otherwise, display the total for all three selections. Save the file as FastFood.java.
b. Modify the application in Exercise 1a so that if the user makes a menu selection he or she has already made, ignore the selection—that is, do not add a second price for the same item to the total. The user still is allowed only three entries. Save the file as FastFood2.java.
Explanation / Answer
A.
PROGRAM:
import java.util.*;
import java.lang.*;
import java.io.*;
class FastFood
{
public static void main (String[] args)
{
float ans=0;
Scanner sc=new Scanner(System.in);
for(int i=0;i<3;i++)
{
System.out.println("Menu:");
System.out.println("(1) Cheeseburger 3.99");
System.out.println("(2) Pepsi 1.50");
System.out.println("(3) Chips 1.00");
System.out.println("Choose an item:");
float choice=sc.nextFloat();
if(choice==0)
{
if(i==0)
{
System.out.println("$0");
break;
}
else
{
System.out.print("$"+ans);
break;
}
}
else
{
if(choice==1) ans+=3.99;
else if(choice==2) ans+=1.50;
else if(choice==3) ans+=1.00;
}
}
if(ans>0)
{
System.out.print("$"+ans);
}
}
}
Sample Input and Output:
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
1
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
2
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
3
$6.49
B.
PROGRAM:
import java.util.*;
import java.lang.*;
import java.io.*;
class FastFood2
{
public static void main (String[] args)
{
float ans=0;
Scanner sc=new Scanner(System.in);
int choice_before[]=new int[3];
for(int i=0;i<3;i++)
{
System.out.println("Menu:");
System.out.println("(1) Cheeseburger 3.99");
System.out.println("(2) Pepsi 1.50");
System.out.println("(3) Chips 1.00");
System.out.println("Choose an item:");
int choice=sc.nextInt();
if(choice!=0)
{
choice_before[choice-1]+=1;
}
if(choice==0)
{
if(i==0)
{
System.out.println("$0");
break;
}
else
{
System.out.print("$"+ans);
break;
}
}
else
{
if(choice==1 && choice_before[choice-1]==1) ans+=3.99;
else if(choice==2 && choice_before[choice-1]==1) ans+=1.50;
else if(choice==3 && choice_before[choice-1]==1) ans+=1.00;
}
}
if(ans>0)
{
System.out.print("$"+ans);
}
}
}
Sample Input and Output:
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
3
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
3
Menu:
(1) Cheeseburger 3.99
(2) Pepsi 1.50
(3) Chips 1.00
Choose an item:
3
1.0
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.