Question: Computer Science I want to use file read and write from customer and order but was in restaurant class Menu.java import java.util.Arrays; import java.util.Scanner; public

Computer Science I want to use file read and write from customer and order but was in restaurant class

Menu.java

import java.util.Arrays; import java.util.Scanner;

public class Menu { private Order[] orders = new Order[100];// This will place the orders made from the menu private int ordersMade;// This will help to populate the orders array private int menuid; // These arrays contain the food in our restaurant and the price private String[] appetizers = { "french fries", "mozerella sticks", "onion rings" }; private double[] appetizersPrices = { 3, 5, 5 };

// main dishes private String[] mainDishes = { "cheese burger", "chicken burger", "hotdog", "chicken tenders 4 pieces" }; private double[] mainDishesPrice = { 5, 6, 4, 5 };

// drinks private String[] drinks = { "soft drinks", "water", "milkshake" }; private double[] drinksPrices = { 2, 1, 4 };

public Menu() { displayMenu(); int option = 1; System.out.println( "Enter the # value followed by quantity, for instance enter 4 1 to order 1 cheese burger"); do { Scanner sc = new Scanner(System.in); int foodId, quantity; foodId = validateOrder(sc.nextInt()); quantity = validateInput(sc.nextInt()); switch (foodId) { // appetizers case 1: orders[ordersMade] = new Order(quantity, appetizers[foodId - 1], appetizersPrices[foodId - 1]); ordersMade++;// Increment the orders made break; case 2: orders[ordersMade] = new Order(quantity, appetizers[foodId - 1], appetizersPrices[foodId - 1]); ordersMade++;// Increment the orders made break; case 3: orders[ordersMade] = new Order(quantity, appetizers[foodId - 1], appetizersPrices[foodId - 1]); ordersMade++;// Increment the orders made break; // main dishes case 4: orders[ordersMade] = new Order(quantity, mainDishes[foodId - 4], mainDishesPrice[foodId - 4]); ordersMade++;// Increment the orders made break; case 5: orders[ordersMade] = new Order(quantity, mainDishes[foodId - 4], mainDishesPrice[foodId - 4]); ordersMade++;// Increment the orders made break; case 6: orders[ordersMade] = new Order(quantity, mainDishes[foodId - 4], mainDishesPrice[foodId - 4]); ordersMade++;// Increment the orders made break; case 7: orders[ordersMade] = new Order(quantity, mainDishes[foodId - 4], mainDishesPrice[foodId - 4]); ordersMade++;// Increment the orders made break; case 8: orders[ordersMade] = new Order(quantity, drinks[foodId - 8], drinksPrices[foodId - 8]); ordersMade++;// Increment the orders made break; // drinks case 9: orders[ordersMade] = new Order(quantity, drinks[foodId - 8], drinksPrices[foodId - 8]); ordersMade++;// Increment the orders made break; case 10: orders[ordersMade] = new Order(quantity, drinks[foodId - 8], drinksPrices[foodId - 8]); ordersMade++;// Increment the orders made break; }

System.out.println("Order another? Type 1 for Yes and any key for No"); option = sc.nextInt(); } while (option == 1); }

private int validateOrder(int orderNum) { int orderNumber = validateInput(orderNum); Scanner sc = new Scanner(System.in);

while (!(orderNumber >= 1 && orderNumber <= 10))// order number must me between 1 and 9

{

System.out.println("order number must me between 1 and 9");

orderNumber = sc.nextInt();

}

return orderNumber; }

private int validateInput(int input) {

Scanner sc = new Scanner(System.in);

while (input < 0)

{

System.out.println("Input cannot be negative. Please enter valid positive input: ");

input = sc.nextInt();

}

return input; }

public Menu(int menuid) {

super(); this.menuid = menuid; displayMenu(); }

/** * This method display the menu */ private void displayMenu() { System.out.println("Menu "); System.out.println("# appetizers:"); for (int i = 0; i < appetizers.length; i++) { System.out.println((i + 1) + " " + "- " + appetizers[i] + "\t" + appetizersPrices[i] + "$"); } System.out.println("# main dishes:"); for (int i = 0; i < mainDishes.length; i++) { System.out.println((i + 4) + " " + "- " + mainDishes[i] + "\t" + mainDishesPrice[i] + "$"); } System.out.println("#drinks:"); for (int i = 0; i < drinks.length; i++) { System.out.println((i + 8) + " " + "- " + drinks[i] + "\t" + drinksPrices[i] + "$"); } }

public int getmenuid() {

return menuid;

}

public String printDetails() {

String result = "";

result += "Menu: ";

// Order[] myOrders = getOrders();

/* * for (int i = 0; i < myOrders.length; i++) * * result += "[Order Name: " + myOrders[i].getOrderName() + " Quantity: " + * myOrders[i].getQuantity() + " Price: " + myOrders[i].getPrice() + "]" + " "; */

result += "Menu Id: " + menuid;

return result;

}

/** * This method is for getting the amount of orders made * * @return the array of orders made */ public Order[] getOrders() { // the size of orders array is 100 // So lets Copy only what we need from it Order[] or = new Order[ordersMade]; for (int i = 0; i < ordersMade; i++) { or[i] = orders[i]; } return or; }

} Order.java

public class Order {

private int quantity;

private String orderName;

private double price;

public Order()

{

}

public Order(int quantity, String orderName, double price) {

super();

this.quantity = quantity;

this.orderName = orderName;

this.price = price;

}

public int getQuantity() {

return quantity;

}

public void setQuantity(int quantity) {

this.quantity = quantity;

}

public String getOrderName() {

return orderName;

}

public void setOrderName(String orderName) {

this.orderName = orderName;

}

public double getPrice() {

return price;

}

public void setPrice(double price) {

this.price = price;

}

public double getTotalPrice() {

return price * quantity;

}

public String printDetails() {

return "Order: [quantity=" + quantity + ", orderName=" + orderName + ", price=" + price + "]";

}

} Customer.java

public class Customer {

private String firstName;

private String lastName;

private int customerId;

public Customer() {

}

public Customer(String firstName, String lastName, int customerId) {

super();

this.firstName = firstName;

this.lastName = lastName;

this.customerId = customerId;

}

public String getFirstName() {

return firstName;

}

public void setFirstName(String firstName) {

this.firstName = firstName;

}

public String getLastName() {

return lastName;

}

public void setLastName(String lastName) {

this.lastName = lastName;

}

public int getCustomerId() {

return customerId;

}

public void setCustomerId(int customerId) {

this.customerId = customerId;

}

public String printDetails() {

return "Customer [First Name=" + firstName + ", Last Name=" + lastName + ", Customer Id=" + customerId + "]";

}

} Restaurant.java(The Main class)

/** * The program will first of all display the menu for the customer to choose */ import java.util.Random;

import java.util.Scanner;

public class Restaurant {

public static void main(String[] args) {

System.out.println("**********************************************************"); System.out.println("************ Restaurant *************"); System.out.println("*********** ************"); System.out.println("**********************************************************"); Scanner sc = new Scanner(System.in);

System.out.println("Enter menu id: ");

int id = validateInteger(sc.nextInt()); Menu menu = new Menu(); Order []orders=menu.getOrders();

System.out.println("Please enter first name of the customer: "); sc=new Scanner(System.in); String fName = sc.nextLine();

System.out.println("Please enter last name of the customer: ");

String lName = sc.nextLine();

Random rand = new Random();

int custId = rand.nextInt(100) + 100;

Customer customer1 = new Customer(fName, lName, custId);

System.out.println(customer1.printDetails());

System.out.println(); for (int i = 0; i < orders.length; i++) {

System.out.println(orders[i].printDetails());

System.out.println("Total order price: " + orders[i].getTotalPrice());

}

System.out.println();

System.out.println(menu.printDetails());

System.out.println("Updated customer details: " + customer1.printDetails());

}

private static int validateInteger(int input) {

Scanner sc = new Scanner(System.in);

while (input < 0)

{

System.out.println("Input cannot be negative. Please enter valid positive input: ");

input = sc.nextInt();

}

return input;

}

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!