-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathShelf.java
More file actions
46 lines (37 loc) · 1.3 KB
/
Copy pathShelf.java
File metadata and controls
46 lines (37 loc) · 1.3 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
package vendingmachine.domain;
import vendingmachine.util.ErrorMessage;
import java.util.List;
import java.util.stream.IntStream;
import static vendingmachine.util.ErrorMessage.*;
public class Shelf {
private final List<Product> products;
public Shelf(List<Product> products) {
this.products = products;
}
public void productExist(String productName) {
products.stream().filter(o -> o.getName().equals(productName))
.findFirst().orElseThrow(() -> new IllegalArgumentException(PRODUCT_NOT_EXIST.getMessage()));
}
public int getMinPrice() {
return products.stream()
.mapToInt(Product::getPrice)
.min()
.orElseThrow(IllegalArgumentException::new);
}
public int consumeProduct(String productName) {
for (Product o : products) {
if (o.getName().equals(productName)) {
if (o.getCount() == 0) {
throw new IllegalArgumentException(PRODUCT_SOLD_OUT.getMessage());
}
return o.sell();
}
}
return 0;
}
public boolean allProductSoldOut() {
int totalCount = products.stream().mapToInt(o -> o.getCount()).sum();
if (totalCount == 0) return true;
return false;
}
}