This program is a java program where the main point is to build a little on-line
ID: 3688829 • Letter: T
Question
This program is a java program where the main point is to build a little on-line food shop. Try to use methods to complete functions.
Program requirements: A food market store invites you to help them build an on-line food order program. Customers can use this program to order food on-line.
The program should include at least 4 functionalities: 1) Add food in order; 2)Remove food from order; 3) Review order; 4) Check out the order. You can use a menu as a UI (user interface).
Assume there are 4 types of food: Hamburger ($8), Lemonade ($3), Chicken Wing ($1), and Pizza ($6). Customer can order any kinds of food, and any amount of them.
Scenario 1: Costumer A ordered 3 Hamburger and 2 Lemonade, and then removed 1 Hamburger and ordered 5 Chicken wings. Check them out finally.
Scenario 2: Costumer B ordered 10 chicken wings and 1 pizza, and then he reviewed his order,
and then add 1 more pizza. Check them out finally.
i) In the add food in order module, your program may need a menu too to show all sorts of food and their price. The input should be food ID and amount. Your program should record the information to calculate the total fee.
ii) In the review order module, all kinds of food information the customer ordered will be shown. The information includes unit price, amount, subtotal fee, and total fee.
iii) In the remove food from order module, customer can remove food from order. The input should be the food ID and amount. Your program should record the changed information.
iv) In the checking out module, the program will print out the bill (review information and tax fee and checkout fee). Checkout fee= total + tax. Taxfee = total * 7.77%. Taxrate: 7.77%. In addition, you can add other modules, such as customer login module, food delivery module, etc.
Output looks like the following:
This output is just an example. You may make it better.
Hint: You may need to declare several global variables to contain customer's order information, like the quantity of any food, the prices, and so on, so that your methods can use them directly. Or the methods pass the data to each other by parameters and returning data.
Hint: You may need the following methods to be created: functionMenu(), foodMenu(), add FoodToOrder(), removeFoodFromOrder(), reviewOrder(), and checkout().
Hint:In the main method, you may need a
sentinel control led loop to control the whole process.
Hint: Think about the situations that the customers mayinput a wrong character/number.
Explanation / Answer
/**
* This is the way classes are defined in Java, the public bit just says it's visible
* to every other class in the system.
*/
public class Beverage
{
//These are the attributes (fields) of the class. It's good practice to make them
//private so that they can only be accessed from within the class.
private String name;
private BigDecimal cost;
/**
* This is the constructor, which is used to create instances of the class. In
* this case it takes the arguments used to initialize the attributes of the class.
*/
public Beverage(String name, BigDecimal cost)
{
this.name = name;
this.cost = cost;
}
/**
* This is a getter, which provides access to the attributes from outside of the
* class.
*/
public BigDecimal getCost()
{
return this.cost;
}
public String getName()
{
return this.name;
}
}
public class Order
{
//This line is assigning an instance of HashMap (a standard data structure class
//in Java). A map is a bit like a dictionary, you have a key in this case the
//beverage that allows you to look-up another value, the quantity.
private Map<Beverage, Integer> beverages = new HashMap<Beverage, Integer>();
public BigDecimal getTotal()
{
BigDecimal total = BigDecimal.ZERO;
//Loop over all the beverages that have been added to the map summing the cost.
for (Beverage beverage : this.beverages.keySet())
{
//Convert the quantity in the map to a BigDecimal needed for the multiply method.
BigDecimal quantity = new BigDecimal(this.beverages.get(beverage));
total = total.add(beverage.getCost().multiple(quantity));
}
return total;
}
public void add(Beverage beverage, Integer quantity)
{
//Store the quantity against the beverage.
this.beverages.put(beverage, quantity);
}
pubic void remove(string item, Integer quantity)
{
for (Iterator<beverage.Entry<String, int>> i = map.entrySet().iterator(); i.hasNext(); ) {
beverage.Entry<String, int> entry = i.next();
if (item.equals(entry.getKey())) {
// Removes entry from Hashtable; note, this is not the Hashtable.remove() method
// but the Iterator.remove() method
i.remove();
}
}
}
public class Restaurant
{
/**
* The main method is static meaning it can be accessed without creating an instance
* of the Restaurant class.
*/
public static void main(String[] args)
{
Scanner in = new Scanner(System.in);
Map<String, Beverage> menu = new HashMap<String, Beverage>();
//Create the instances of Beverage and add them to the menu.
menu.put("Hamburger", new Beverage("Hamburger", new BigDecimal(8.00)));
menu.put("lemonade", new Beverage("lemonade", new BigDecimal(3.00)));
menu.put("Chicken Wing", new Beverage("Chicken Wing", new BigDecimal(1.00)));
menu.put("Pizza", new Beverage("Pizzae", new BigDecimal(5.00)));
System.out.println("Welcome to Mcdonald's storw!! Please choose one option (0-> exit)
1. Add food to order 2. Remove food from order 3. Review the order 4. Checkout the order");
int ch = in.nextInt();
Order order1 = new Order();
do{
switch(ch)
{
case 0: exit();
case 1:
System.out.println("Please choose one food (0-> back to menu) 1.Hamburger 2.lemonade 3.Chicken Wing 4.Pizza");
int i = in.nextInt();
switch(i)
{
case 0: main();
case 1: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.add(menu.get("Hamburger"), q);
break;
case 2: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.add(menu.get("lemonade"), q);
break;
case 3: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.add(menu.get("Chicken Wing"), q);
break;
case 4: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.add(menu.get("Pizza"), q);
break;
default: System.out.println("Invalid choice");
}
case 2:
System.out.println(this.beverages);
System.out.println("Please choose one food from cart (0-> back to menu) 1.Hamburger 2.lemonade 3.Chicken Wing 4.Pizza");
int i = in.nextInt();
switch(i)
{
case 0: main();
case 1: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.remove("Hamburger", q);
break;
case 2: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.remove("lemonade", q);
break;
case 3: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.remove("Chicken Wing", q);
break;
case 4: System.out.println("Enter Quantity:");
int q = in.nextInt();
order1.remove("Pizza", q);
break;
default: System.out.println("Invalid choice");
}
case 3:System.out.println("Your order is " + this.beverges);
case 4:System.out.println("Total for order 2: " + order1.getTotal());
default: System.out.println("Invalid choice");
}
}while(ch!=0);
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.