-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathVendingMachine.java
More file actions
63 lines (49 loc) · 1.71 KB
/
Copy pathVendingMachine.java
File metadata and controls
63 lines (49 loc) · 1.71 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
package vendingmachine;
import java.util.EnumMap;
import java.util.Map;
import vendingmachine.message.ExceptionMessage;
public class VendingMachine {
private final CoinStore coinStore;
private ProductStore productStore;
private Money holdingMoney;
public VendingMachine() {
productStore = new ProductStore();
coinStore = new CoinStore(new EnumMap<>(Coin.class));
holdingMoney = new Money(0);
}
public void initProducts(ProductStore repository) {
this.productStore = repository;
}
public void initHoldingMoney(Money money) {
coinStore.addCoinRandomly(money);
}
public boolean canPurchaseSomething() {
return productStore.canBuySomething(holdingMoney.getAmount());
}
public void initInputMoney(Money inputMoney) {
holdingMoney = inputMoney;
}
public void purchaseProduct(String productName) {
Product product = productStore.findProductByName(productName);
validatePurchase(product);
productStore.purchaseProduct(product);
holdingMoney.minus(product.getPrice());
}
private void validatePurchase(Product product) {
if (holdingMoney.isLessThen(product.getPrice())) {
throw new IllegalArgumentException(ExceptionMessage.LACK_MONEY);
}
if (productStore.getLeftProductCount(product) <= 0) {
throw new IllegalArgumentException(ExceptionMessage.LACK_QUANTITY);
}
}
public Money getHoldingMoney() {
return holdingMoney;
}
public Map<Coin, Integer> getCoinMap() {
return coinStore.getRepository();
}
public Map<Coin, Integer> getChange() {
return coinStore.getChange(holdingMoney);
}
}