In: Computer Science
Write in java
Let's assume I go a restaurant, List of meals such as breakfast and lunch and dinner is displayed to me
I can pick breakfast, lunch, and dinner after I pick any of the breakfast lunch and dinner, a list of food/prices offered for breakfast is shown
so I will see the breakfast menu such an egg, milk and their prices,
after that I pick milk.
then I am asked to two more sides from the menu which will display the meal again, I pick one from the meal, and it shows what is offered in the mealtime,
I pick that food/price too.
then I will have a subtotal and total.
Something like this
Meal> Breakfast lunch and dinner
Lunch> Rice 2.99 Beans5.99
Output: you have picked rice2.99 for lunch
Add two more sides
Meal> breakfast, Lunch and dinner
Dinner> Chinese soup 3.99, Indian soup 2.99
Indian soup 2.99
Output: Side 1 is indian soup, please select one more side:
meal> breakfast lunch and dinner
breakfast> scrambled eggs 2.99, Milk 2.44
2
Output your side 2 is milk 2.44
Your subtotal is
and total is
Java Code:
package mealpackage;
import java.util.Scanner;
public class Meal {
//Initializing constants for the
class
static Scanner sc = new Scanner(System.in);
static int mealchoice;
static int side1;
static int side2;
static String breakfast[] = {"Scrambled Egg",
"Milk"};
static String lunch[] = {"Rice", "Beans"};
static String dinner[] = {"Chicken Soup", "Indian
Soup"};
static double breafastPrices[] = {2.99, 2.44};
static double lunchPrice[] = {2.99, 5.99};
static double dinnerPrices[] = {3.99, 2.99};
//Main method
public static void main(String[] args) {
//Variable to store user
choice of meal
String userChoice;
System.out.println("Enter the meal
of your choice");
System.out.println("1. Breakfast,
2. Lunch, 3. Dinner");
userChoice = sc.next();
//Display the
meals
displayMeals(userChoice);
System.out.println();
System.out.println("1. Breakfast,
2. Lunch, 3. Dinner");
userChoice = sc.next();
//Display the side
1
displaySide1(userChoice);
System.out.println();
System.out.println("1. Breakfast,
2. Lunch, 3. Dinner");
userChoice = sc.next();
//Display the side
2
displaySide2(userChoice);
System.out.println();
//Calculate the
total
subTotal();
}
static void displayMeals(String userChoice) {
//Show breakfast, lunch or
dinner item to the user
if(userChoice.toLowerCase().equals("breakfast") ||
userChoice.toLowerCase().equals("1")) {
System.out.println("Breakfast items are: ");
//We get
the choice of item in another function
pickMainMeal(breakfast, breafastPrices);
}else
if(userChoice.toLowerCase().equals("lunch") ||
userChoice.toLowerCase().equals("2")) {
System.out.println("Lunch items are: ");
pickMainMeal(lunch, lunchPrice);
}else
if(userChoice.toLowerCase().equals("dinner") ||
userChoice.toLowerCase().equals("3")) {
System.out.println("Dinner items are: ");
pickMainMeal(dinner, dinnerPrices);
}
}
static void displaySide1(String userChoice) {
//Show breakfast, lunch or
dinner item to the user
if(userChoice.toLowerCase().equals("breakfast") ||
userChoice.toLowerCase().equals("1")) {
System.out.println("Breakfast items are: ");
//We get
the choice of item in another function
pickSide1(breakfast, breafastPrices);
}else
if(userChoice.toLowerCase().equals("lunch") ||
userChoice.toLowerCase().equals("2")) {
System.out.println("Lunch items are: ");
pickSide1(lunch,
lunchPrice);
}else
if(userChoice.toLowerCase().equals("dinner") ||
userChoice.toLowerCase().equals("3")) {
System.out.println("Dinner items are: ");
pickSide1(dinner, dinnerPrices);
}
}
static void displaySide2(String userChoice) {
//Show breakfast, lunch or
dinner item to the user
if(userChoice.toLowerCase().equals("breakfast") ||
userChoice.toLowerCase().equals("1")) {
System.out.println("Breakfast items are: ");
//We get
the choice of item in another function
pickSide2(breakfast, breafastPrices);
}else
if(userChoice.toLowerCase().equals("lunch") ||
userChoice.toLowerCase().equals("2")) {
System.out.println("Lunch items are: ");
pickSide2(lunch,
lunchPrice);
}else
if(userChoice.toLowerCase().equals("dinner") ||
userChoice.toLowerCase().equals("3")) {
System.out.println("Dinner items are: ");
pickSide2(dinner, dinnerPrices);
}
}
//Show the items and get the choice of item from the
user
static void pickMainMeal(String meal[], double
mealPrices[]) {
for(int i=0; i<meal.length; i++
) {
System.out.println((i+1) + ". " + meal[i] + ": " +
mealPrices[i]);
}
mealchoice = sc.nextInt();
//Display the main meal
picked
System.out.println("You have picked
" + meal[mealchoice-1] + ": " + mealPrices[mealchoice-1]);
System.out.println("Add two more
sides: ");
System.out.println();
}
//Show the items and get the choice of item
from the user
static void pickSide1(String meal[], double
mealPrices[]) {
for(int i=0; i<meal.length; i++
) {
System.out.println((i+1) + ". " + meal[i] + ": " +
mealPrices[i]);
}
side1 = sc.nextInt();
//Display
Side1
System.out.println("Side 1 is " +
meal[side1-1] + ", please select one more side: ");
System.out.println();
}
//Show the items and get the choice of item
from the user
static void pickSide2(String meal[], double
mealPrices[]) {
for(int i=0; i<meal.length; i++
) {
System.out.println((i+1) + ". " + meal[i] + ": " +
mealPrices[i]);
}
side2 = sc.nextInt();
//Display
side2
System.out.println("You side 2 is "
+ meal[side2-1]);
System.out.println();
}
static void subTotal() {
//Calculate and display the
subtotal
System.out.println("Your subtotal
is: " + (breafastPrices[mealchoice-1] + lunchPrice[mealchoice-1] +
dinnerPrices[mealchoice-1]));
System.out.println("Your total is:
" + (breafastPrices[mealchoice-1] + lunchPrice[mealchoice-1] +
dinnerPrices[mealchoice-1]));
}
}
Sample Output:
Enter the meal of your choice
1. Breakfast, 2. Lunch, 3. Dinner
1
Breakfast items are:
1. Scrambled Egg: 2.99
2. Milk: 2.44
2
You have picked Milk: 2.44
Add two more sides:
1. Breakfast, 2. Lunch, 3. Dinner
2
Lunch items are:
1. Rice: 2.99
2. Beans: 5.99
1
Side 1 is Rice, please select one more side:
1. Breakfast, 2. Lunch, 3. Dinner
3
Dinner items are:
1. Chicken Soup: 3.99
2. Indian Soup: 2.99
1
You side 2 is Chicken Soup
Your subtotal is: 11.42
Your total is: 11.42