-
Notifications
You must be signed in to change notification settings - Fork 350
Expand file tree
/
Copy pathGoodsManager.java
More file actions
54 lines (39 loc) · 1.62 KB
/
Copy pathGoodsManager.java
File metadata and controls
54 lines (39 loc) · 1.62 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
package vendingmachine.goods;
import vendingmachine.exception.VendingMachineValidator;
import java.util.HashMap;
public class GoodsManager {
VendingMachineValidator vendingMachineValidator = new VendingMachineValidator();
private final HashMap<String, Goods> goodsInfo = new HashMap<>();
private int minPrice = Integer.MAX_VALUE;
public GoodsManager(String goodsList){
makeGoodsInformation(goodsList);
minPriceGoodsList();
}
private void makeGoodsInformation(String goodsList) {
String[] itemList = goodsList.split(";");
for (String item : itemList) {
String[] goodsInformation = item.replaceAll("[\\[\\]]","").split(",");
vendingMachineValidator.validGoodsMoneyType(goodsInformation[1]);
Goods goods = new Goods(goodsInformation);
goodsInfo.put(goodsInformation[0],goods);
}
}
private void minPriceGoodsList() {
for (Goods g : goodsInfo.values())
if (g.getPrice() < minPrice) minPrice = g.getPrice();
}
public boolean validPurchase(String purchaseGoods, int money) {
vendingMachineValidator.validGoodsName(purchaseGoods,goodsInfo);
Goods goods = goodsInfo.get(purchaseGoods);
return goods.getPrice() <= money && goods.getAmount() != 0;
}
public boolean validMinPurchase(int money) {
return money >= minPrice;
}
public int purchaseGoods(String purchaseGoods, int money) {
Goods goods = goodsInfo.get(purchaseGoods);
if (goods.getAmount() != 0)
goods.reduceAmount();
return money - goods.getPrice();
}
}