Problem: You often enjoy dining at a Chinese restaurant near your house. Along w
ID: 3568409 • Letter: P
Question
Problem:
You often enjoy dining at a Chinese restaurant near your house. Along with good food you also enjoy chatting with the manager, who always has interesting stories to tell. One day while chatting with the manager, she asks you if you could write a simple software program for them. She explains how most of their patrons prefer to place their order by specifying an id, rather than the name of the dish. The patrons look at the menu and say "We'll have two plates of number 15". The waiter normally writes down the dish id and the number of servings,a nd gives it to the cook. However, the cook would prefer to have the name of the dish and not a number.
The manager wants you to write a simple software which maintains data for all the dishes on their menu. When run the system should ask the user for an integer representing the 'id' of the dish. When the user enters an id, the system should print the name of the dish.
In the exercise below, we have already provided you with an array of Strings called 'dishes', where each item in the array is the name of the dish and the index of the array is the id of the dish. The user's selection is given to you in an integer variable called 'selection'. You have to look up the name of the dish from the array, and print it to the system console.
What we expect:
We describe an example menu below with dish id's and names:
0. Wanton Soup
1. Egg Drop Soup
2. Sea Food Soup
3. Vegetable Fried Rice
4. Vegetable Lo Mien
When the system is run, it will ask the user for a dish id.
If the user enters the id 1, we expect the program to print "Egg Drop Soup",
if the user enters the id 7 (which does not exist), we expect the program to print "id not found".
---------------------------------------------------------------------
//package ex42;
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
String dishes[] = new String[5];
dishes[0] = "Wanton Soup";
dishes[1] = "Egg Drop Soup";
dishes[2] = "Sea Food Soup";
dishes[3] = "Vegetable Fried Rice";
dishes[4] = "Vegetable Lo Mein";
System.out.println("We have the following dishes. Please enter the id of a dish: ");
System.out.println("0- Wanton Soup:");
System.out.println("1- Egg Drop Soup:");
System.out.println("2- Sea Food Soup:");
System.out.println("3- Vegetable Fried Rice:");
System.out.println("4- Vegetable Lo Mein:");
Scanner scanner = new Scanner(System.in);
int selection = scanner.nextInt();
System.out.println("You have selected:");
///{
//write your code here
//start
//end
///}
}
}
Explanation / Answer
import java.util.Scanner;
public class Menu {
public static void main(String[] args) {
String dishes[] = new String[5];
dishes[0] = "Wanton Soup";
dishes[1] = "Egg Drop Soup";
dishes[2] = "Sea Food Soup";
dishes[3] = "Vegetable Fried Rice";
dishes[4] = "Vegetable Lo Mein";
System.out.println("We have the following dishes. Please enter the id of a dish: ");
System.out.println("0- Wanton Soup:");
System.out.println("1- Egg Drop Soup:");
System.out.println("2- Sea Food Soup:");
System.out.println("3- Vegetable Fried Rice:");
System.out.println("4- Vegetable Lo Mein:");
Scanner scanner = new Scanner(System.in);
int selection = scanner.nextInt();
System.out.println("You have selected:");
{
//Code for converting the dish id to dish name...
if( selection < 5 )
{
System.out.println(dishes[selection]);
}
else
{
System.out.println("Dish ID not found");
}
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.