-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathController.java
More file actions
76 lines (63 loc) · 2.97 KB
/
Copy pathController.java
File metadata and controls
76 lines (63 loc) · 2.97 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
package vendingmachine.controller;
import java.util.List;
import java.util.Map;
import java.util.function.Supplier;
import vendingmachine.domain.Coin;
import vendingmachine.domain.InputAmount;
import vendingmachine.domain.Product;
import vendingmachine.domain.VendingMachine;
import vendingmachine.domain.VendingMachineAmount;
import vendingmachine.utils.Converter;
import vendingmachine.view.InputView;
import vendingmachine.view.OutputView;
public class Controller {
public void run() {
VendingMachine vendingMachine = new VendingMachine();
VendingMachineAmount vendingMachineAmount = repeatReadForInvalid(this::createVendingMachineAmount);
addCoinsToVendingMachine(vendingMachineAmount, vendingMachine);
addProductsToVendingMachine(vendingMachine);
InputAmount inputAmount = repeatReadForInvalid(this::createInputAmount);
repeatPurchaseProduct(vendingMachine, inputAmount);
Map<Coin, Integer> changes = vendingMachine.changes(inputAmount);
OutputView.printChanges(changes);
}
private void addCoinsToVendingMachine(VendingMachineAmount vendingMachineAmount, VendingMachine vendingMachine) {
vendingMachine.addCoins(vendingMachineAmount);
OutputView.printCoinsOfVendingMachine(vendingMachine.getCoins());
}
private void addProductsToVendingMachine(VendingMachine vendingMachine) {
List<String> inputProducts = repeatReadForInvalid(InputView::readProducts);
for (String inputProduct : inputProducts) {
String[] split = inputProduct.split(",");
vendingMachine.addProduct(repeatReadForInvalid(() -> this.createProduct(split)));
}
}
private InputAmount createInputAmount() {
return new InputAmount(InputView.readInputAmount());
}
private void repeatPurchaseProduct(VendingMachine vendingMachine, InputAmount inputAmount) {
while (canPurchase(vendingMachine, inputAmount)) {
OutputView.printRemainingInputAmount(inputAmount.getAmount());
String productNameToPurchase = repeatReadForInvalid(InputView::readProductNameToPurchase);
vendingMachine.purchase(productNameToPurchase, inputAmount);
}
}
private boolean canPurchase(VendingMachine vendingMachine, InputAmount inputAmount) {
return !vendingMachine.isAllPriceGreaterThan(inputAmount.getAmount())
&& !vendingMachine.isAllProductSoldOut();
}
private Product createProduct(String[] split) {
return new Product(split[0], Converter.convertToInt(split[1]), Converter.convertToInt(split[2]));
}
private VendingMachineAmount createVendingMachineAmount() {
return new VendingMachineAmount(InputView.readVendingMachineAmount());
}
private <T> T repeatReadForInvalid(Supplier<T> reader) {
try {
return reader.get();
} catch (IllegalArgumentException e) {
OutputView.printErrorMessage(e);
return repeatReadForInvalid(reader);
}
}
}