-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathWarehouse.java
More file actions
114 lines (97 loc) · 3.67 KB
/
Warehouse.java
File metadata and controls
114 lines (97 loc) · 3.67 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
package com.example;
import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.*;
public class Warehouse {
static HashMap<String, Warehouse> warehouses = new HashMap<>();
HashMap<UUID, Product> products;
List<Shippable> shippables;
List<Perishable> perishables;
HashMap<Optional<Product>, HashMap<LocalDateTime, BigDecimal>> changedProducts;
private Warehouse() {
this.products = new HashMap<>();
this.shippables = new ArrayList<>();
this.perishables = new ArrayList<>();
this.changedProducts = new HashMap<>();
}
public List<Product> getProducts(){
return List.copyOf(products.values());
}
public List<Shippable> shippableProducts() {
return shippables;
}
public void clearProducts(){
products.clear();
Category.categories.clear();
shippables.clear();
perishables.clear();
}
public boolean isEmpty(){
return products.isEmpty();
}
public void addProduct(Product product){
if (product == null)
throw new IllegalArgumentException("Product cannot be null.");
if (getProductById(product.uuid).isPresent())
throw new IllegalArgumentException("Product with that id already exists, use updateProduct for updates.");
products.put(product.uuid, product);
if(product instanceof Shippable)
shippables.add((Shippable) product);
if(product instanceof Perishable)
perishables.add((Perishable) product);
}
public void remove(UUID id){
products.remove(id);
}
public Optional<Product> getProductById(UUID id){
if (products.isEmpty())
return Optional.empty();
return (Optional.ofNullable(products.get(id)));
}
public static synchronized Warehouse getInstance(String string) {
if (!warehouses.containsKey(string)) {
warehouses.put(string, new Warehouse());
}
return warehouses.get(string);
}
public static synchronized Warehouse getInstance() {
if(!warehouses.containsKey("default"))
warehouses.put("default", new Warehouse());
return warehouses.get("default");
}
public Map<Category, List<Product>> getProductsGroupedByCategories() {
if(products == null || products.isEmpty()){
return Collections.emptyMap();
}
HashMap<Category, List<Product>> result = new HashMap<>();
var tempMap = Category.categories.entrySet();
var productsList = new ArrayList<>(products.values());
for(var category : tempMap){
List<Product> tempList = new ArrayList<>();
productsList.stream()
.filter(element -> element.category.equals(category.getValue()))
.forEach(tempList::add);
result.put(category.getValue(), tempList);
}
return result;
}
public void updateProductPrice(UUID id, BigDecimal newPrice){
var product = getProductById(id);
if(product.isPresent()) {
var tempMap = new HashMap<LocalDateTime, BigDecimal>();
tempMap.put(LocalDateTime.now(), product.get().price);
changedProducts.put(product, tempMap);
product.get().setPrice(newPrice);
}
else
throw new NoSuchElementException("Product not found with id:" + id);
}
public Map<Optional<Product>, HashMap<LocalDateTime, BigDecimal>> getChangedProducts(){
return changedProducts;
}
public List<Perishable> expiredProducts() {
return perishables.stream()
.filter(Perishable::isExpired)
.toList();
}
}