-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathOrderManager.java
More file actions
119 lines (105 loc) · 3.37 KB
/
Copy pathOrderManager.java
File metadata and controls
119 lines (105 loc) · 3.37 KB
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
108
109
110
111
112
113
114
115
116
117
118
119
import java.util.Scanner;
import java.util.ArrayList;
/**
* Represents an order manager in a restaurant.
* @author Tan Zheng Kai
* @version 2.0
* @since 2021-11-13
*/
public class OrderManager {
/**
* The list of orders of this order manager.
*/
private ArrayList<Order> orderList;
//private Customer customer; do we need this?
/**
* Creates a new order manager.
*/
public OrderManager(){
orderList = new ArrayList<Order>();
}
/**
* Removes an order from the list of orders of this order manager.
*/
public void cancelOrder() {
Scanner sc = new Scanner(System.in);
this.printOrderList();
try {
System.out.println("Which order would you like to cancel?");
int orderIndex = sc.nextInt();
sc.nextLine();
if (orderIndex <= 0 || orderIndex > this.orderList.size()){
throw new ArrayIndexOutOfBoundsException("Please input a valid index from 1 to "+this.orderList.size());
}
this.orderList.remove(orderIndex - 1); //remove the index - 1 order from orderList
System.out.println("Order #" + orderIndex +" cancelled");
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.out.println("program exiting ...");
System.exit(0);
}
}
/**
* Creates an order and adds it into the order list.
*/
public void createOrder() {
Order temp = new Order();
//order obj calls makeNewOrder that adds orderItems to the arrayList
temp.makeNewOrder();
this.orderList.add(temp);
System.out.println("Order created. printing new order...");
temp.printOrder();
}
/**
* Change the order in the list of orders of this order manager.
*/
public void changeOrder() {
Scanner sc = new Scanner(System.in);
this.printOrderList();
try {
System.out.println("Which order would you like to change?");
int changeChoice = sc.nextInt();
sc.nextLine();
if (changeChoice <= 0 || changeChoice > this.orderList.size()){
throw new ArrayIndexOutOfBoundsException("Please input a valid index from 1 to "+this.orderList.size());
}
Order temp = this.orderList.get(changeChoice-1);
temp.updateOrder();
System.out.println("Order Updated. printing updated order...");
temp.printOrder();
} catch (ArrayIndexOutOfBoundsException e) {
System.out.println(e.getMessage());
System.out.println("program exiting ...");
System.exit(0);
}
}
/**
* Prints out the order list in this order manager.
*/
public void printOrderList() {
int orderListSize = this.orderList.size();
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++");
for(int i = 0; i<orderListSize; i++){
System.out.println("Order Number "+ (i+1) + ": ");
this.orderList.get(i).printOrder();
System.out.println("+++++++++++++++++++++++++++++++++++++++++++++++"); //separator between orders
}
System.out.println();
}
/**
* Gets an order item from the order list of this Order Manager with a given index.
* @param index The index of the order in the order list.
* @return the order of the given index.
*/
public Order getOrderListItem(int index) {
Order order = this.orderList.get(index);
return order;
}
/**
* Gets the size of the orderlist of this order manager.
* @return the size of the order list.
*/
public int getSizeOfOrderList() {
return this.orderList.size();
}
}