-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOrderManager.java
199 lines (183 loc) · 5.46 KB
/
OrderManager.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
/**
* @author Bradley Estacio and Patrick Inosanto
* 12/6/19
*
* Class that manages both one time and repeated orders -
* Uses OneTimeOrders and RepeatedOrders
* Has one array list.
* The array list is where all the OneTimeOrders/RepeatedOrders are stored.
* Also has a method that sorts all the orders by date.
*
* Uses of OrderManager:
* Adds orders
* Deletes order - @param orderID
* List orders for a particular customer ID in increasing date and sorted order - @param customerID
* Calculate and report for each month on inventory needed for each product ID (based on current orders)
*/
import java.util.*;
public class OrderManager
{
private ArrayList<OneTimeOrders> orders = new ArrayList<OneTimeOrders>();
private int removeCounter = 1;
public OrderManager(String order)
{
String[] orderSplit = order.split(","); //splits string taken into file for ease of sorting
if(orderSplit[0].equals("O"))
{
OneTimeOrders newOrder = new OneTimeOrders(orderSplit[1], orderSplit[2], orderSplit[3], Integer.parseInt(orderSplit[4]));
orders.add(newOrder);
}
else if(orderSplit[0].equals("R"))
{
RepeatedOrders newOrder = new RepeatedOrders(orderSplit[1], orderSplit[2], orderSplit[3], Integer.parseInt(orderSplit[4]), Integer.parseInt(orderSplit[5]), orderSplit[6]);
orders.add(newOrder);
}
}
//adds orders
public void addOrder(String nextOrder)
{
String[] orderSplit = nextOrder.split(","); //splits string taken into file for ease of sorting
if(orderSplit[0].equals("O"))
{
OneTimeOrders newOrder = new OneTimeOrders(orderSplit[1], orderSplit[2], orderSplit[3], Integer.parseInt(orderSplit[4]));
orders.add(newOrder);
}
else if(orderSplit[0].equals("R"))
{
RepeatedOrders newOrder = new RepeatedOrders(orderSplit[1], orderSplit[2], orderSplit[3], Integer.parseInt(orderSplit[4]), Integer.parseInt(orderSplit[5]), orderSplit[6]);
orders.add(newOrder);
}
}
//deletes orders - removeCounter++
public void deleteOrder(int orderID) throws OrderManagerException
{
if(orderID - removeCounter > orders.size() || orderID < 0) //if statement makes sure orderID is valid
{
throw new OrderManagerException("orderID does not exist.");
}
else
{
orders.remove(orderID - removeCounter);
this.removeCounter++;
}
}
//lists orders for specific customer ID
//must be increasing date sorted order
public void listOrder(String customerID) throws OrderManagerException
{
boolean validCustomerID = false;
//makes sure that customerID exists
for(int i = 0; i < orders.size(); i++)
{
if(orders.get(i).getCustomerID().equals(customerID))
{
validCustomerID = true;
}
}
if(validCustomerID)
{
Collections.sort(orders);
for(int j = 0; j < orders.size(); j++)
{
if(orders.get(j).getCustomerID().equals(customerID))
{
System.out.println(orders.get(j).toString());
}
}
}
else
{
throw new OrderManagerException("Sorry. This Customer ID does not exist.");
}
}
//Just prints toString of OneTimeOrders or RepeatedOrders
//All in one single line if sorted beforehand
public String toString()
{
String totalOrders = "";
for(int i = 0; i < orders.size(); i++)
{
totalOrders += orders.get(i).toString() + "\n";
}
return totalOrders;
}
/**
* Inventory report is sorted either by year, month, and productID
* @see OfficeSupplyUI - inventoryReport
* @see OneTimeOrders compareTo
* Note: .getMonth() return 0-11 so reports will print out
* months in numbers 1-12 representing each respective month
* Note: when reporting inventory with repeated orders, it is stated
* that it is one, and the end date of the repeated order
*/
public void inventoryReport()
{
//initializing variables
int previousMonth = 0;
int previousYear = 0;
int productIDAmount = 0;
int thisMonth, thisYear;
boolean sameMonth, sameYear, sameProductID;
String previousProductID = "";
String thisProductID;
Collections.sort(orders);
//everything should now be sorted by date, month, and product ID
for(int i = 0; i < orders.size(); i++)
{
thisMonth = orders.get(i).getDate().getMonth() + 1;
thisYear = orders.get(i).getDate().getYear();
thisProductID = orders.get(i).getProductID();
//checks if orders.get(i) is instance of RepeatedOrders so they can add properly
if(orders.get(i) instanceof RepeatedOrders)
{
RepeatedOrders tempOrder = (RepeatedOrders) orders.get(i); //temporarily casted to get amount for repeated order
productIDAmount += tempOrder.getAmount() + Math.abs((tempOrder.getAmount() * ((tempOrder.getEndDate().getDate() - tempOrder.getDate().getDate()) / tempOrder.getPeriod())));
}
else
{
productIDAmount += orders.get(i).getAmount();
}
//checks to make it can combine year/month/productIDs
if(previousMonth != thisMonth)
{
sameMonth = false;
previousMonth = thisMonth;
}
else
{
sameMonth = true;
}
if(previousYear != thisYear)
{
sameYear = false;
previousYear = thisYear;
}
else
{
sameYear = true;
}
if(!previousProductID.equals(thisProductID))
{
sameProductID = false;
previousProductID = thisProductID;
}
else
{
sameProductID = true;
}
if(!sameYear)
{
System.out.println("Year: " + previousYear);
}
if(!sameMonth)
{
System.out.println("\tMonth: " + previousMonth);
}
if(!sameProductID)
{
System.out.println("\t\tNeed " + productIDAmount + " of " + previousProductID);
productIDAmount = 0;
}
}
}
}