-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathItems.java
More file actions
57 lines (45 loc) · 1.61 KB
/
Copy pathItems.java
File metadata and controls
57 lines (45 loc) · 1.61 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
package vendingmachine.domain;
import vendingmachine.utils.ItemsValidator;
import java.util.List;
import java.util.NoSuchElementException;
import java.util.OptionalLong;
import java.util.stream.Collectors;
import static vendingmachine.exception.ErrorMessage.*;
public class Items {
private final List<Item> items;
private Items(List<Item> items) {
this.items = items;
}
public static Items from(List<Item> items) {
validateUniqueName(items);
return new Items(items);
}
private static void validateUniqueName(List<Item> items) {
List<String> names = items.stream()
.map(Item::provideName)
.collect(Collectors.toList());
ItemsValidator.validateUniqueValue(names);
}
public Item buyItem(String itemName, long priceAmount) {
Item item = findItemByName(itemName);
item.buyItem(priceAmount);
return item;
}
private Item findItemByName(String name) {
return items.stream()
.filter(item -> item.provideName().equals(name))
.findFirst()
.orElseThrow(() -> new IllegalArgumentException(INVALID_ORDER_ITEM_NAME.getMessage()));
}
public long findPurchasableMinimumPrice() {
return items.stream()
.filter(Item::hasQuantity)
.mapToLong(Item::providePrice)
.min()
.orElseThrow(() -> new IllegalArgumentException(INVALID_ORDER_ITEM.getMessage()));
}
public boolean hasNoQuantity() {
return items.stream()
.allMatch(Item::hasNoQuantity);
}
}