-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathGoodsManager.java
More file actions
52 lines (39 loc) · 1.62 KB
/
Copy pathGoodsManager.java
File metadata and controls
52 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
package vendingmachine.goods;
import vendingmachine.exception.VendingMachineException;
import java.util.HashMap;
public class GoodsManager {
private final HashMap<String, Goods> goodsInfo = new HashMap<>();
private int minPrice = Integer.MAX_VALUE;
VendingMachineException vendingMachineException = new VendingMachineException();
public GoodsManager(String goodsList){
makeGoodsInformation(goodsList);
minPriceGoodsList();
}
private void makeGoodsInformation(String goodsList) {
String[] itemList = goodsList.split(";");
for (String item : itemList) {
String[] goodsInformation = item.replace("[", "").replace("]", "").split(",");
vendingMachineException.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) {
vendingMachineException.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.deleteAmount();
return money - goods.getPrice();
}
}