forked from BenjaminChun/OODP
-
Notifications
You must be signed in to change notification settings - Fork 0
/
OrderItem.java
107 lines (98 loc) · 2.82 KB
/
OrderItem.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
import java.util.Scanner;
/**
* Represents the Order Item in a restaurant.
* @author Tan Zheng Kai
* @version 2.0
* @since 2021-11-13
*/
public class OrderItem {
/**
* The menu item of this order item.
*/
private MenuItem menuItem;
/**
* The quantity of this order item.
*/
private int quantity;
/**
* Empty Constructor.
*/
public OrderItem() {
}
/**
* Creates an order item with a given quantity.
* @param item The menu item of this order item.
* @param qty The quantity of this order item.
*/
public OrderItem(MenuItem item, int qty){
this.menuItem = item;
this.quantity = qty;
}
/**
* Gets the menu item of this order item
* @return the menu item of this order item.
*/
public MenuItem getMenuItem() {
return menuItem;
}
/**
* Changes the menu item of this order item
* @param menuItem The menu item of this order item.
*/
public void setMenuItem(MenuItem menuItem) {
this.menuItem = menuItem;
}
/**
* Gets the quantity of this order item.
* @return the quantity of this order item.
*/
public int getQuantity() {
return this.quantity;
}
/**
* Changes the quantity of this order item.
* @param qty the quantity of this order item.
*/
public void setQuantity(int qty) {
this.quantity = qty;
}
/**
* Creates an order item by scanning input for each attribute.
*/
public void createOrderItem() {
Scanner sc = new Scanner(System.in);
RestaurantApp.globalMenuManager.printMenu(); //create a globalmenuManager so that other classes can access the menu
try {
System.out.println("Which item would you like to order?");
int menuIndex = sc.nextInt();
sc.nextLine();
if (menuIndex <= 0 || menuIndex > RestaurantApp.globalMenuManager.getSizeOfMenu()){
throw new ArrayIndexOutOfBoundsException("Please input a valid index from 1 to "+RestaurantApp.globalMenuManager.getSizeOfMenu());
}
this.menuItem = RestaurantApp.globalMenuManager.getMenuItem(menuIndex-1);
System.out.println("How many of this are you ordering?");
this.quantity = sc.nextInt();
sc.nextLine();
System.out.println("Order item added. printing details...");
this.printOrderItem();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.out.println("program exiting ...");
System.exit(0);
}
}
/**
* Calculate the price of one entry of menu items.
* @return total price of the menu items in that entry.
*/
public double calculatePrice() {
return menuItem.getPrice() * quantity;
}
/**
* Show text of the calculation of one entry of the menu item list.
*/
public void printOrderItem(){
System.out.print(this.quantity+" x "+this.menuItem.getName()+" ---- ");
System.out.printf("$%.2f\n",this.calculatePrice());
}
}