-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOnlineOrder.java
152 lines (127 loc) · 4.47 KB
/
OnlineOrder.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
import java.util.ArrayList;
public class OnlineOrder {
//private instance variables are created
private Member member;
private double costOrder;
private int numItems;
private ArrayList<GroceryItem> basketItems;
//constructor for OnlineOrder
public OnlineOrder(Member member,ArrayList<GroceryItem> basketItems ) throws BadOrder{
setMember(member);
setBasketItems(basketItems);
setNumItems(basketItems);
}
public Member getMember() {
/**
* Gets the member associated with the order.
*
* @return member - The member associated with the order.
*/
return member;
}
public void setMember(Member member) {
/**
* Sets the member associated with the order.
*
* @param member The new member associated with the order.
*/
this.member = member;
}
public double getCostOrder() {
/**
* Gets the total cost of the order.
*
* @return The total cost of the order.
*/
return totalCostBasket(basketItems);
}
public int getNumItems() {
/**
* Gets the number of items in the order.
*
* @return The number of items in the order.
*/
return numItems;
}
public void setNumItems(ArrayList<GroceryItem> basketItems) {
/**
* Sets the number of items in the order.
*
* @param numItems The new number of items in the order.
*/
this.numItems = basketItems.size();
}
public ArrayList<GroceryItem> getBasketItems() {
/**
* Gets the list of grocery items in the order's basket.
*
* @return The list of grocery items in the order's basket.
*/
return basketItems;
}
public void setBasketItems(ArrayList<GroceryItem> basketItems) throws BadOrder {
/**
* Sets the list of grocery items in the order's basket.
*
* @param basketItems - The new list of grocery items in the order's basket.
*/
for(GroceryItem item: basketItems){
if(item.getInventory()==0){
//exception is thrown if an item is not in stock
throw new BadOrder(item.getItemName()+" is not in stock!");
}
this.basketItems = basketItems;
}
}
public boolean checkInventory(){
/**
* This method is used to check that all items in an order are in the inventory
* @param None
* @return inStock - boolean containing the inventory status
*/
boolean inStock = true;
for(GroceryItem item: basketItems){
//inStock remains true if the item has over 0 units in stock
if(item.getInventory()>0){
inStock = true;
}
//false is returned if an item is not in stock
else{
return false;
}
}
return inStock;
}
public double totalCostBasket(ArrayList<GroceryItem> basketItems){
/**
* This method is used to calculate the cost of the order
* @param basketItems - array list containing the order
* @return costOrder - double containing the cost of the order
*/
costOrder = 0;
//for each loop that loops through the order and the price of each item is added to costOrder
for (GroceryItem item: basketItems){
costOrder+=(item.getPrice());
}
return costOrder;
}
@Override
public String toString(){
/**
* This method is used to create a String representation of the OnlineOrder object
* @param None
* @return s - the string representation of the OnlineOrder object
*/
String s = "The name of the member buying this order is " + member.getName()+ ". The cost of this order is: $"+ totalCostBasket(basketItems)+ ". The number of items in this order are: "+getNumItems();
return s;
}
public double compareTo(OnlineOrder otherOrder){
/**
* This method is used to compare the cost of two orders
* @param otherOrder - the other order
* @return the difference between the cost of the two orders
*/
//totalCostBasket is used to calculate the cost of both orders
return totalCostBasket(basketItems)- (otherOrder.totalCostBasket(otherOrder.getBasketItems()));
}
}