-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathVendingMachineController.java
More file actions
57 lines (46 loc) · 2.08 KB
/
Copy pathVendingMachineController.java
File metadata and controls
57 lines (46 loc) · 2.08 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 controller;
import utils.Parser;
import utils.RepeatInput;
import vendingmachine.*;
import view.InputView;
import view.OutputView;
import java.util.Map;
public class VendingMachineController {
public void run() {
MachineAmount vendingMoney = RepeatInput.repeatWhenInvalid(this::vendingMachineMoney);
OutputView.printVendingMachineCoinAmount(new CoinCounter(), vendingMoney);
Products productsMap = RepeatInput.repeatWhenInvalid(this::productInformation);
InputAmount inputAmount = RepeatInput.repeatWhenInvalid(this::inputAmount);
int remainder = RepeatInput.repeatWhenInvalid(() -> purchaseProcess(productsMap, inputAmount));
OutputView.printRemainChanges(new CoinCounter(), vendingMoney, remainder);
}
private MachineAmount vendingMachineMoney() {
OutputView.printVendingMachineMoney();
String amount = InputView.readAmountInput();
int vendingMoney = Parser.convertToInt(amount);
return new MachineAmount(vendingMoney);
}
private Products productInformation() {
OutputView.printOrderDetails();
String input = InputView.readOrderDetails();
Map<String, Product> productMap = Parser.convertToProductMap(input);
return new Products(productMap);
}
private InputAmount inputAmount() {
OutputView.printInputAmount();
String amount = InputView.readAmountInput();
int inputAmount = Parser.convertToInt(amount);
return new InputAmount(inputAmount);
}
private int purchaseProcess(Products products, InputAmount inputAmount) {
while(inputAmount.getMoney() >= products.getMininumProduct()) {
OutputView.printPurchaseProduct(inputAmount);
String purchaseName = InputView.readPurchaseName(products);
int purchasePrice = products.findInputAmount(purchaseName);
Product purchasedProduct = products.getProductMap().get(purchaseName);
purchasedProduct.decreaseQuantity();
inputAmount.subtractMoney(purchasePrice);
}
return inputAmount.getMoney();
}
}