Create a series of parallel arrays for common menu categories in restaurants (ap
ID: 3764350 • Letter: C
Question
Create a series of parallel arrays for common menu categories in restaurants (appetizers, main course, desserts, and drinks
Create 4 input file variables, 4 input files, and 4 counter variables for reading in data for each of the categories
Using while loops to read in the data from each file and store the data into the arrays, be sure to check for files that cannot be opened
Create a main menu with user options for each category, include a subtotal value within the menu
When a user selects a category, the data stored within the array for that category is displayed on the screen
Allow the user to select the item from the category, and then update the subtotal for the current amount owed based on the item's price
While the user selects items, use another array to store the item purchased (name and price)
Allow the user to move between the main menu and category menus until the user has finished ordering items
When finished, display a receipt for the user with an itemized list of food/drink items purchased with names and prices for each
Display a subtotal, tax, and total for the user's purchase. Use 10% tax for easy math
Prompt the user for amount of money entered, then determine change due to the customer and display the amount on the screen
For full points, you MUST use functions. You may choose the code to move into functions. You will have at least 3 functions in your program for full points
Explanation / Answer
import java.io.*;
import java.util.*;
class appetizers{
String name;
double price;
appetizers(String n,double p){
name = n;
price = p;
}
}
class main_course{
String name;
double price;
main_course(String n,double p){
name = n;
price = p;
}
}
class deserts{
String name;
double price;
deserts(String n,double p){
name = n;
price = p;
}
}
class drinks{
String name;
double price;
drinks(String n,double p){
name = n;
price = p;
}
}
class main{
public static void fill_appetizers(BufferedReader br,ArrayList<appetizers> temp){
String line;
while (line = br.readLine()) != null){
String[] ss = line.split(' ');
appetizers ap = new appetizers(ss[0],Double.parseDouble(ss[1]));
temp.add(ap);
}
}
public static void fill_main_course(BufferedReader br,ArrayList<main_course> temp){
String line;
while (line = br.readLine()) != null){
String[] ss = line.split(' ');
main_course ap = new main_course(ss[0],Double.parseDouble(ss[1]));
temp.add(ap);
}
}
public static void fill_deserts(BufferedReader br,ArrayList<deserts> temp){
String line;
while (line = br.readLine()) != null){
String[] ss = line.split(' ');
deserts ap = new deserts(ss[0],Double.parseDouble(ss[1]));
temp.add(ap);
}
}
public static void fill_drinks(BufferedReader br,ArrayList<drinks> temp){
String line;
while (line = br.readLine()) != null){
String[] ss = line.split(' ');
drinks ap = new drinks(ss[0],Double.parseDouble(ss[1]));
temp.add(ap);
}
}
public static void display_menu(ArrayList<appetizers> temp){
for (int i = 0; i < temp.size(); i++){
System.out.println((i+1)+" "temp.get(i).name+" "+temp.get(i).price);
}
System.out.println("0 if do not want to choose anything");
}
public static void display_menu(ArrayList<main_course> temp){
for (int i = 0; i < temp.size(); i++){
System.out.println((i+1)+" "temp.get(i).name+" "+temp.get(i).price);
}
System.out.println("0 if do not want to choose anything");
}
public static void display_menu(ArrayList<deserts> temp){
for (int i = 0; i < temp.size(); i++){
System.out.println((i+1)+" "temp.get(i).name+" "+temp.get(i).price);
}
System.out.println("0 if do not want to choose anything");
}
public static void display_menu(ArrayList<drinks> temp){
for (int i = 0; i < temp.size(); i++){
System.out.println((i+1)+" "temp.get(i).name+" "+temp.get(i).price);
}
System.out.println("0 if do not want to choose anything");
}
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
BufferedReader br_1 = new BufferedReader(new FileReader("appetizers.txt"));
BufferedReader br_2 = new BufferedReader(new FileReader("main_course.txt"));
BufferedReader br_3 = new BufferedReader(new FileReader("deserts.txt"));
BufferedReader br_4 = new BufferedReader(new FileReader("drinks.txt"));
ArrayList<appetizers> temp_1 = new ArrayList<appetizers>();
ArrayList<main_course> temp_2 = new ArrayList<main_course>();
ArrayList<deserts> temp_3 = new ArrayList<deserts>();
ArrayList<drinks> temp_4 = new ArrayList<drinks>();
fill_appetizers(br_1,temp_1);
fill_main_course(br_2,temp_2);
fill_desert(br_3,temp_3);
fill_drinks(br_4,temp_4);
int total_item = 0;
float total_cost = 0.0;
while (true){
System.out.println("Select Category");
System.out.println("1 : Appetizers");
System.out.println("2 : Main Course");
System.out.println("3 : Deserts");
System.out.println("4 : Drinks");
System.out.println("5 : Exit");
int n = sc.nextInt();
if (n == 1){
display_menu(temp_1);
System.out.print("Select Your option (choose option > max value to choose nothing): ");
int ch = sc.nextInt();
if (ch != 0){
total_item += 1;
total_cost += temp_1.get(ch).price;
}
}
else if (n == 2){
display_menu(temp_2);
System.out.print("Select Your option : ");
int ch = sc.nextInt();
if (ch != 0){
total_item += 1;
total_cost += temp_1.get(ch).price;
}
}
else if (n == 3){
display_menu(temp_3);
System.out.print("Select Your option : ");
int ch = sc.nextInt();
if (ch != 0){
total_item += 1;
total_cost += temp_1.get(ch).price;
}
}
else if (n == 4){
display_menu(temp_4);
System.out.print("Select Your option : ");
int ch = sc.nextInt();
if (ch != 0){
total_item += 1;
total_cost += temp_1.get(ch).price;
}
}
else
break;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.