-
Notifications
You must be signed in to change notification settings - Fork 349
Expand file tree
/
Copy pathMachine.java
More file actions
58 lines (46 loc) · 1.63 KB
/
Copy pathMachine.java
File metadata and controls
58 lines (46 loc) · 1.63 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
package vendingmachine.domain;
import camp.nextstep.edu.missionutils.Randoms;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
public class Machine {
private LinkedHashMap<Coin, Integer> coins;
public Machine() {
this.coins = generateCoinMap();
}
public LinkedHashMap<Coin, Integer> getCoins() {
return coins;
}
private LinkedHashMap<Coin, Integer> generateCoinMap() {
LinkedHashMap<Coin, Integer> coinMap = new LinkedHashMap<>();
coinMap.put(Coin.COIN_500, 0);
coinMap.put(Coin.COIN_100, 0);
coinMap.put(Coin.COIN_50, 0);
coinMap.put(Coin.COIN_10, 0);
return coinMap;
}
public void generateCoin(int moneyInput) {
List<Integer> coinUnit = getCoinUnit();
generateRandomCoin(moneyInput, coinUnit);
}
private void generateRandomCoin(int moneyInput, List<Integer> coinUnit) {
while(moneyInput > 0) {
int pickRandomNum = Randoms.pickNumberInList(coinUnit);
Coin randomCoin = Coin.valueOf(pickRandomNum);
if (isInputRemainingMoney(moneyInput, pickRandomNum)) continue;
moneyInput -= pickRandomNum;
coins.put(randomCoin, coins.get(randomCoin) + 1);
}
}
private static boolean isInputRemainingMoney(int moneyInput, int pickRandomNum) {
return moneyInput - pickRandomNum < 0;
}
private static List<Integer> getCoinUnit() {
List<Integer> coinUnit = new ArrayList<>();
coinUnit.add(500);
coinUnit.add(100);
coinUnit.add(50);
coinUnit.add(10);
return coinUnit;
}
}