-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrder.java
268 lines (225 loc) · 8.1 KB
/
Order.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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Class Name: Order
Author: King Lai, ZiCheng Huang, John Oh, Nancy Hao
Date: January 10, 2019
School: A.Y. Jackson Secondary School
Purpose: This class defines what an Order is, and calculates
the total of an Order based off the Food list.
*/
import java.text.DecimalFormat;
class Order {
//Encapsulation
private int maxPossibleFoodsInMenu;
private double tax;
private Date orderDate;
private int orderNumber;
private Food[] foodList;
private int numItems;
private double tip;
private double subtotal;
private double totalAfterCouponDiscount;
private double totalAfterCustomerDiscount;
private double taxAmount;
private double finalTotal;
private double totalCostToMake;
/*
Parameter(s):
- orderNumber: the current Order number assigned to this Order
- date: the current Date
- maxPossibleFoodsInMenu: an integer value that sets the maximum
number of Foods in an Order
- tax: the tax in double format
Description: This method is the constructor of the Order class.
Purpose: Creates an Order, and assigns the fields respectively.
*/
public Order (int orderNumber, Date date, int maxPossibleFoodsInMenu, double tax) {
this.orderNumber = orderNumber;
orderDate = date;
this.maxPossibleFoodsInMenu = maxPossibleFoodsInMenu;
foodList = new Food [maxPossibleFoodsInMenu];
numItems = 0;
this.tax = tax;
subtotal = -1;
totalAfterCouponDiscount = -1;
totalAfterCustomerDiscount = -1;
taxAmount = -1;
finalTotal = -1;
totalCostToMake = -1;
tip = 0;
}
// The following are accessors and mutators.
// Encapsulation
public double getTaxAmount() {
return taxAmount;
}
public double getFinalTotal() {
return finalTotal;
}
public double getSubtotal() {
return subtotal;
}
public double getTotalAfterCustomerDiscount() {
return totalAfterCustomerDiscount;
}
public Food[] getFoodList ()
{
return foodList;
}
public int getNumItems ()
{
return numItems;
}
public Date getOrderDate () {
return orderDate;
}
public double getTotalCostToMake () {
return totalCostToMake;
}
public int getOrderNumber () {
return orderNumber;
}
public double getTip () {
return tip;
}
public void setNumItems (int numItems) {
this.numItems = numItems;
}
public void setOrderNumber (int num)
{
orderNumber = num;
}
public void setTip (double tip) {
this.tip = tip;
}
public void setOrderDate (Date date) {
orderDate = date;
}
/*
Description: This method is used to reset an Order.
Purpose: For every item on the Food list, sets it to null.
*/
public void resetOrder ()
{
for (int i = 0; i < foodList.length; i ++)
{
foodList[i] = null; //Array of Object
}
}
/*
Parameter(s):
- orderFood: the Food object to be added to the Order
Description: This method is checks if adding the Food into the Food list is successful.
Purpose: If the number of items on the current Food list is less than maximum number
of Foods in an Order, and the menu quantity is greater than the desired amount,
the Food is added to the Food list, and sets the Order quantity while the number
of items on the Food list is added by 1, which will return true, otherwise return false.
*/
public boolean addToOrder (Food orderFood, int orderSize) {
if (numItems < maxPossibleFoodsInMenu && orderFood.getAvailability() >= orderSize) {
foodList[numItems] = orderFood; // Array of Object
foodList[numItems].setAvailability (orderSize);
numItems++;
return true;
} else {
return false;
}
}
/*
Parameter(s):
- orderNumber: the current Order number assigned to this Order
- date: the current Date
- maxPossibleFoodsInMenu: an integer value that sets the maximum
number of Foods in an Order
- tax: the tax in double format
Description: Deletes a food item depending on the input of the customer, foodListNum.
Returns the quantity of the food item that has to be added back to the menu.
Purpose: To allow the user to delete an existing item off their order.
*/
public int deleteAnOrder (int foodListNum) {
int index = foodListNum - 1;
if (index < numItems && index >= 0) {
int quantity = foodList [index].getAvailability(); // Array of Object
for (int i = index; i < numItems; i++) {
foodList [i] = foodList [i+1];
}
foodList [numItems] = null;
numItems--;
return quantity;
} else {
return -1;
}
}
/*
Description: This method is used to calculate the subtotal of the Order.
Purpose: For every item on the Food list, the subtotals adds the Food
price multiplied by the amount wanted.
*/
public void calculateSubtotal() {
subtotal = 0;
for (int i = 0; i < numItems; i++) {
subtotal += foodList [i].getPrice() * foodList [i].getAvailability(); // Array of Object
}
}
/*
Parameter(s):
- discountDecimal: a double value that contains the discount rate
Description: This method is used to calculate the total after applying a Coupon.
Purpose: The totalAfterCouponDiscount is the subtotal multiplied by 1 minus discount rate.
*/
public void calculateTotalAfterCouponDiscount(double discountDecimal) {
totalAfterCouponDiscount = subtotal * (1 - discountDecimal);
}
/*
Parameter(s):
- discountDecimal: a double value that contains the Customer discount rate
Description: This method is used to calculate the total if user is a premium Customer.
Purpose: The totalAfterCustomerDiscount is the subtotal multiplied by 1 minus discount rate.
*/
public void calculateTotalAfterCustomerDiscount(double discountDecimal) {
totalAfterCustomerDiscount = totalAfterCouponDiscount * (1 - discountDecimal);
}
/*
Description: This method is used to calculate the amount of tax.
Purpose: The taxAmount is the totalAfterCustomerDiscount multiplied by the tax.
*/
public void calculateTaxAmount() {
taxAmount = totalAfterCustomerDiscount * tax;
}
/*
Description: This method is used to calculate the final price.
Purpose: The finalTotal is the totalAfterCustomerDiscount added by taxAmount.
*/
public void calculateFinalTotal() {
finalTotal = totalAfterCustomerDiscount + taxAmount;
}
/*
Description: This method is used to calculate the total cost to make the entire order.
Purpose: For each Food on the Food list, the total cost is added by the cost
to make multiplied by the amount wanted.
*/
public void calculateTotalCostToMake () {
totalCostToMake = 0;
for (int i = 0; i < numItems; i++) {
totalCostToMake += foodList [i].getCostToMake() * foodList [i].getAvailability(); //Array of Object
}
}
/*
Description: This method is used to determine the arrival time of an Order.
Purpose: Returns an arrival time of the number of items on the Food list
multiplied by 4.
*/
public int determineArrivalTime () {
return numItems * 4;
}
/*
Description: This method is used to print out all Order total details.
Purpose: Called when printing the total for an Order, and returns the following Strings.
*/
DecimalFormat df = new DecimalFormat ("0.00");
public String toString () {
return "Subtotal: $" + df.format(subtotal) +
"\nDiscounts: -$" + df.format((subtotal - totalAfterCustomerDiscount)) +
"\nTax: $" + df.format(taxAmount) +
"\nTotal: $" + df.format(finalTotal);
}
}