-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDashboard.java
252 lines (213 loc) · 11.1 KB
/
Dashboard.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
import java.util.ArrayList;
import java.util.Collections;
/**
* Project 5 - Dashboard.java
*
* Class that encompasses functionality for customers to view store and seller
* information, and for sellers to view statistics for each of their stores
* Uses four methods, two for sellers and two for customers.
* To create a full dashboard, call the two customer/seller methods, and display
* the resulting data. Each Dashboard will return an arraylist with three
* collumns: Name, Total
* quantity, and Total revenue. Sort index is which collumn to sort by.
*
* @author Shafer Anthony Hofmann, Qihang Gan, Shreyas Viswanathan, Nathan Pasic
* Miller, Oliver Long
*
* @version December 6, 2023
*/
public class Dashboard {
private Database database = new Database();
// Seller's method to view all customers
// Return line format: email,totalQuantity,totalSpent
public ArrayList<String> sellerGetCustomersDashboard(int sortIndex, boolean sortAscending) throws SellerException {
// Get all customer Users
ArrayList<String> allCustomers = database.getMatchedEntries("users.csv", 3, "CUSTOMER");
// return array
ArrayList<String> customerStatisticsStrings = new ArrayList<>();
// for each customer, find their purchases
for (String customer : allCustomers) {
String[] customerDataList = customer.split(",");
String customerID = customerDataList[0];
// get all purchases this customer has made
ArrayList<String> customerPurchases = database.getMatchedEntries("purchaseHistories.csv", 0, customerID);
// Accumulate all this customer's purchase data into total purchases and total
// spending
double totalQuantity = 0;
double totalSpent = 0;
for (String purchase : customerPurchases) {
String[] splitPurchase = purchase.split(",");
double purchaseQuantity = Double.parseDouble(splitPurchase[6]);
totalQuantity += purchaseQuantity;
// price times quantity
double purchaseCost = Double.parseDouble(splitPurchase[7]);
totalSpent += purchaseCost;
}
String customerEmail = customerDataList[1];
// Add customer's stats to the list
// Format: email, totalQuantity, totalSpent
String statsString = String.format("%s,%.2f,%.2f", customerEmail, totalQuantity, totalSpent);
customerStatisticsStrings.add(statsString);
}
// Sorts the processed data
// See here for allowed values of sortType
if (customerStatisticsStrings.isEmpty()) {
throw new SellerException("No customers have purchased products from any of your stores yet!");
} else {
return sortResults(sortIndex, sortAscending, customerStatisticsStrings);
}
}
// Seller's method to view all products
// Return line format: productName,TotalSales,Total Revenue
public ArrayList<String> sellerGetProductsDashboard(int sortIndex, boolean sortAscending) throws SellerException {
// Get all products:Seller ID,Store ID,Product ID,Store Name,Product
// Name,Available Quantity,Price,Description
ArrayList<String> allProducts = database.getDatabaseContents("products.csv");
// return array
ArrayList<String> productStatisticsStrings = new ArrayList<>();
// for each product, accumulate the total number of sales
for (String product : allProducts) {
String[] productDataList = product.split(",");
// product id is the 3rd collumn of the products csv
String productID = productDataList[2];
// get all purchases this customer has made, productID on 3rd index of
// purchasHistories
// Customer ID,Seller ID,Store ID,Product ID,Store Name,Product Name,Purchase
// Quantity,Price
ArrayList<String> productPurchases = database.getMatchedEntries("purchaseHistories.csv", 3, productID);
// Accumulate all this customer's purchase data into total purchases and total
// spending
double totalSold = 0;
double totalRevenue = 0;
for (String purchase : productPurchases) {
String[] splitPurchase = purchase.split(",");
double purchaseQuantity = Double.parseDouble(splitPurchase[6]);
totalSold += purchaseQuantity;
// price times quantity
double saleRevenue = Double.parseDouble(splitPurchase[7]);
totalRevenue += saleRevenue;
}
// Product's name is stored in the 4th collumn
String productName = productDataList[4];
// Add customer's stats to the list
// Format: email, totalQuantity, totalSpent
String statsString = String.format("%s,%.2f,%.2f", productName, totalSold, totalRevenue);
productStatisticsStrings.add(statsString);
}
if (productStatisticsStrings.isEmpty()) {
throw new SellerException("No products have been purchased from any of your stores yet!");
} else {
return sortResults(sortIndex, sortAscending, productStatisticsStrings);
}
}
// Customer's method to view all stores and their products sold
// Return line format: storeName, Products Sold, Total Revenue
public ArrayList<String> customerGetStoresDashboard(int sortIndex, boolean sortAscending) throws CustomerException {
// Get all stores:Store ID,Seller ID,Store Name,Number of Products
ArrayList<String> allStores = database.getDatabaseContents("stores.csv");
// return array
ArrayList<String> storeStatisticsStrings = new ArrayList<>();
// for each product, accumulate the total number of sales
for (String store : allStores) {
String[] storeDataList = store.split(",");
// store id is the 1st collumn of the stores csv
String storeID = storeDataList[0];
// get all purchases made at this store
// Customer ID,Seller ID,Store ID,Product ID,Store Name,Product Name,Purchase
// Quantity,Price
ArrayList<String> storeSales = database.getMatchedEntries("purchaseHistories.csv", 2, storeID);
// Accumulate all this customer's purchase data into total purchases and total
// spending
double totalProductsSold = 0;
double totalRevenue = 0;
for (String purchase : storeSales) {
String[] splitPurchase = purchase.split(",");
double purchaseQuantity = Double.parseDouble(splitPurchase[6]);
totalProductsSold += purchaseQuantity;
// price times quantity
double saleRevenue = Double.parseDouble(splitPurchase[7]);
totalRevenue += saleRevenue;
}
// Store name is stored in the 3rd collumn
String storeName = storeDataList[2];
// Add customer's stats to the list
// Format: email, totalQuantity, totalSpent
String statsString = String.format("%s,%.2f,%.2f", storeName, totalProductsSold, totalRevenue);
storeStatisticsStrings.add(statsString);
}
if (storeStatisticsStrings.isEmpty()) {
throw new CustomerException("No products have been purchased from any of the stores yet!");
} else {
return sortResults(sortIndex, sortAscending, storeStatisticsStrings);
}
}
// Personal Dashboard requires CustomerID
// Returns all stores including summed information about purchases made by the
// customer with customer ID at that store
// Return line format: storeName, Products Bought, Total Spent
public ArrayList<String> customerGetPersonalPurchasesDashboard(int sortIndex, boolean sortAscending,
String customerID) throws CustomerException {
// Get all stores:Store ID,Seller ID,Store Name,Number of Products
ArrayList<String> allStores = database.getDatabaseContents("stores.csv");
// return array
ArrayList<String> storeStatisticsStrings = new ArrayList<>();
// for each product, accumulate the total number of sales
for (String store : allStores) {
String[] storeDataList = store.split(",");
// store id is the 1st collumn of the stores csv
String storeID = storeDataList[0];
// get all purchases made at this store
// Customer ID,Seller ID,Store ID,Product ID,Store Name,Product Name,Purchase
// Quantity,Price
ArrayList<String> storeSales = database.getMatchedEntries("purchaseHistories.csv", 2, storeID);
// Accumulate all this customer's purchase data into total purchases and total
// spending
double totalProductsBought = 0;
double totalSpent = 0;
for (String purchase : storeSales) {
String[] splitPurchase = purchase.split(",");
// If purchase wasn't made by this customer, continue
if (!splitPurchase[0].equals(customerID)) {
continue;
}
double purchaseQuantity = Double.parseDouble(splitPurchase[6]);
totalProductsBought += purchaseQuantity;
// price times quantity
double saleRevenue = Double.parseDouble(splitPurchase[7]);
totalSpent += saleRevenue;
}
// Store name is stored in the 3th collumn
String storeName = storeDataList[2];
// Add customer's stats to the list
// Format: email, totalQuantity, totalSpent
String statsString = String.format("%s,%.2f,%.2f", storeName, totalProductsBought, totalSpent);
storeStatisticsStrings.add(statsString);
}
if (storeStatisticsStrings.isEmpty()) {
throw new CustomerException("You haven\'t purchased items from any of the stores yet!");
} else {
return sortResults(sortIndex, sortAscending, storeStatisticsStrings);
}
}
// Assuming strings are in format: String, Double, Double
public ArrayList<String> sortResults(int sortIndex, boolean sortAscending, ArrayList<String> arrayList) {
switch (sortIndex) {
case 0:
Collections.sort(arrayList,
(a, b) -> a.split(",")[0].compareTo(b.split(",")[0]));
// Alphabetically sorting works reverse to numerical sorting
sortAscending = !sortAscending;
break;
case 1, 2:
// Using a lambda function to compare only the price collumn after casting to
// double
Collections.sort(arrayList,
(a, b) -> Double.compare(Double.parseDouble(a.split(",")[sortIndex]),
Double.parseDouble(b.split(",")[sortIndex])));
break;
}
if (!sortAscending)
Collections.reverse(arrayList);
return arrayList;
}
}