-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathInventoryTracker.java
More file actions
352 lines (305 loc) · 13 KB
/
Copy pathInventoryTracker.java
File metadata and controls
352 lines (305 loc) · 13 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
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import java.util.Scanner;
import java.util.ArrayList;
import java.util.HashMap;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
// Tracks inventory for a single user with analytics and food tracking
// Each user gets their own instance upon login
public class InventoryTracker {
private ArrayList<Asset> assets;
// Represents a single inventory item with timestamps and metadata
static class Asset {
String id;
String name;
String category;
int quantity;
String status;
LocalDateTime dateAdded;
String foodSubcategory;
String expirationDate;
Asset(String id, String name, String category, int quantity) {
this.id = id;
this.name = name;
this.category = category;
this.quantity = quantity;
this.status = "active";
this.dateAdded = LocalDateTime.now();
this.foodSubcategory = null;
this.expirationDate = null;
}
// Custom toString for nice formatting when printing
public String toString() {
StringBuilder result = new StringBuilder();
result.append(String.format("[%s] %s | Category: %s | Qty: %d | Status: %s | Added: %s",
id, name, category, quantity, status,
dateAdded.format(DateTimeFormatter.ofPattern("MMM dd, yyyy HH:mm"))));
if (foodSubcategory != null) {
result.append(String.format(" | Food Type: %s", foodSubcategory));
}
if (expirationDate != null) {
result.append(String.format(" | Expires: %s", expirationDate));
}
return result.toString();
}
}
public InventoryTracker() {
assets = new ArrayList<>();
}
// Add asset to current user's inventory with food-specific options
// If asset already exists with same name and category, increment quantity instead
public void addAsset(Scanner scanner) {
System.out.print(" Asset ID: ");
String id = scanner.nextLine().trim();
System.out.print(" Asset Name: ");
String name = scanner.nextLine().trim();
System.out.print(" Category (hardware/software/food/other): ");
String category = scanner.nextLine().trim();
System.out.print(" Quantity: ");
int qty = Integer.parseInt(scanner.nextLine().trim());
// Check if asset with same name and category already exists
for (Asset existingAsset : assets) {
if (existingAsset.name.equalsIgnoreCase(name) &&
existingAsset.category.equalsIgnoreCase(category)) {
// Asset exists, increment quantity
existingAsset.quantity += qty;
System.out.println(" [SUCCESS] Quantity updated! Total: " + existingAsset.quantity);
return;
}
}
// Asset doesn't exist, create new one
Asset asset = new Asset(id, name, category, qty);
// If food, ask for subcategory and expiration
if (category.equalsIgnoreCase("food")) {
System.out.print(" Food Type (packaged/produce): ");
asset.foodSubcategory = scanner.nextLine().trim();
System.out.print(" Expiration Date (MM/DD/YYYY): ");
asset.expirationDate = scanner.nextLine().trim();
}
assets.add(asset);
System.out.println(" [SUCCESS] Asset added!");
}
// Display all assets with nice formatting
public void viewAssets() {
if (assets.isEmpty()) {
System.out.println(" No assets in inventory.");
return;
}
System.out.println("\n === Your Assets ===");
for (Asset asset : assets) {
System.out.println(" " + asset);
}
}
// Find asset by ID and update quantity
public void updateAssetQty(Scanner scanner) {
if (assets.isEmpty()) {
System.out.println(" No assets to update.");
return;
}
System.out.print(" Asset ID to update: ");
String id = scanner.nextLine().trim();
// Linear search through ArrayList to find matching ID
for (Asset asset : assets) {
if (asset.id.equals(id)) {
System.out.print(" New quantity: ");
int newQty = Integer.parseInt(scanner.nextLine().trim());
asset.quantity = newQty;
System.out.println(" [SUCCESS] Asset updated!");
return;
}
}
System.out.println(" [ERROR] Asset not found!");
}
// Edit asset details by ID - allows changing name, category, and food metadata
public void editAsset(Scanner scanner) {
if (assets.isEmpty()) {
System.out.println(" No assets to edit.");
return;
}
System.out.print(" Asset ID to edit: ");
String id = scanner.nextLine().trim();
// Find asset by ID
for (Asset asset : assets) {
if (asset.id.equals(id)) {
System.out.println(" Current Asset: " + asset);
System.out.println("\n What would you like to edit?");
System.out.println(" 1. Name");
System.out.println(" 2. Category");
System.out.println(" 3. Food Type (if food)");
System.out.println(" 4. Expiration Date (if food)");
System.out.println(" 5. Back");
System.out.print(" Choose: ");
String choice = scanner.nextLine().trim();
switch (choice) {
case "1":
System.out.print(" New name: ");
asset.name = scanner.nextLine().trim();
System.out.println(" [SUCCESS] Name updated!");
break;
case "2":
System.out.print(" New category: ");
String newCategory = scanner.nextLine().trim();
asset.category = newCategory;
if (!newCategory.equalsIgnoreCase("food")) {
asset.foodSubcategory = null;
asset.expirationDate = null;
}
System.out.println(" [SUCCESS] Category updated!");
break;
case "3":
if (asset.category.equalsIgnoreCase("food")) {
System.out.print(" New food type (packaged/produce): ");
asset.foodSubcategory = scanner.nextLine().trim();
System.out.println(" [SUCCESS] Food type updated!");
} else {
System.out.println(" [ERROR] This asset is not food!");
}
break;
case "4":
if (asset.category.equalsIgnoreCase("food")) {
System.out.print(" New expiration date (MM/DD/YYYY): ");
asset.expirationDate = scanner.nextLine().trim();
System.out.println(" [SUCCESS] Expiration date updated!");
} else {
System.out.println(" [ERROR] This asset is not food!");
}
break;
case "5":
break;
default:
System.out.println(" [ERROR] Invalid option!");
}
return;
}
}
System.out.println(" [ERROR] Asset not found!");
}
// Bubble sort implementation for custom sorting
private void bubbleSort(ArrayList<Asset> list, int sortType) {
int n = list.size();
for (int i = 0; i < n - 1; i++) {
for (int j = 0; j < n - i - 1; j++) {
Asset current = list.get(j);
Asset next = list.get(j + 1);
boolean shouldSwap = false;
if (sortType == 1) {
// Sort by name (A-Z)
shouldSwap = current.name.compareToIgnoreCase(next.name) > 0;
} else if (sortType == 2) {
// Sort by quantity (High to Low)
shouldSwap = current.quantity < next.quantity;
} else if (sortType == 3) {
// Sort by category
shouldSwap = current.category.compareToIgnoreCase(next.category) > 0;
} else if (sortType == 4) {
// Sort by date (Newest First)
shouldSwap = current.dateAdded.isBefore(next.dateAdded);
}
if (shouldSwap) {
Asset temp = list.get(j);
list.set(j, list.get(j + 1));
list.set(j + 1, temp);
}
}
}
}
// Sort assets by different parameters and display
public void sortAndView(Scanner scanner) {
if (assets.isEmpty()) {
System.out.println(" No assets to sort.");
return;
}
System.out.println("\n === Sort Options ===");
System.out.println(" 1. By Name (A-Z)");
System.out.println(" 2. By Quantity (High to Low)");
System.out.println(" 3. By Category");
System.out.println(" 4. By Date Added (Newest First)");
System.out.println(" 5. Back");
System.out.print(" Choose: ");
String choice = scanner.nextLine().trim();
// Create copy of assets list to sort (don't modify original order)
ArrayList<Asset> sortedAssets = new ArrayList<>(assets);
switch (choice) {
case "1":
bubbleSort(sortedAssets, 1);
System.out.println("\n === Assets (Sorted by Name) ===");
break;
case "2":
bubbleSort(sortedAssets, 2);
System.out.println("\n === Assets (Sorted by Quantity) ===");
break;
case "3":
bubbleSort(sortedAssets, 3);
System.out.println("\n === Assets (Sorted by Category) ===");
break;
case "4":
bubbleSort(sortedAssets, 4);
System.out.println("\n === Assets (Sorted by Date Added - Newest First) ===");
break;
case "5":
return;
default:
System.out.println(" [ERROR] Invalid option!");
return;
}
// Display sorted assets
for (Asset asset : sortedAssets) {
System.out.println(" " + asset);
}
}
// Show comprehensive stats including category breakdown and purchase frequency
public void showStats() {
int totalAssets = assets.size();
int totalItems = 0;
HashMap<String, Integer> categoryCount = new HashMap<>();
// Count items by category
for (Asset asset : assets) {
totalItems += asset.quantity;
String cat = asset.category.toLowerCase();
categoryCount.put(cat, categoryCount.getOrDefault(cat, 0) + 1);
}
System.out.println("\n === Inventory Stats ===");
System.out.println(" Total Asset Types: " + totalAssets);
System.out.println(" Total Items: " + totalItems);
System.out.println("\n === Items by Category ===");
String mostBought = null;
int maxCount = 0;
for (String category : categoryCount.keySet()) {
int count = categoryCount.get(category);
System.out.println(" " + category + ": " + count + " items");
if (count > maxCount) {
maxCount = count;
mostBought = category;
}
}
if (mostBought != null) {
System.out.println("\n === Most Purchased Category ===");
System.out.println(" " + mostBought + " (" + maxCount + " items)");
}
// Food-specific analytics
int foodCount = 0;
int packagedCount = 0;
int produceCount = 0;
for (Asset asset : assets) {
if (asset.category.equalsIgnoreCase("food")) {
foodCount++;
if (asset.foodSubcategory != null) {
if (asset.foodSubcategory.equalsIgnoreCase("packaged")) {
packagedCount++;
} else if (asset.foodSubcategory.equalsIgnoreCase("produce")) {
produceCount++;
}
}
}
}
if (foodCount > 0) {
System.out.println("\n === Food Analytics ===");
System.out.println(" Total Food Items: " + foodCount);
if (packagedCount > 0) {
System.out.println(" Packaged Food: " + packagedCount);
}
if (produceCount > 0) {
System.out.println(" Produce: " + produceCount);
}
}
}
}