-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathCoinCounter.java
More file actions
30 lines (27 loc) · 1.1 KB
/
Copy pathCoinCounter.java
File metadata and controls
30 lines (27 loc) · 1.1 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
package vendingmachine;
import java.util.EnumMap;
import java.util.Map;
public class CoinCounter {
private final Map<Coin, Integer> coinCounterMap = new EnumMap<>(Coin.class);
private final Map<Coin, Integer> remainCounterMap = new EnumMap<>(Coin.class);
public Map<Coin, Integer> getCounter(MachineAmount amount) {
int vendingMoney = amount.getMoney();
for (Coin coin : Coin.values()){
int coinNumbers = vendingMoney / coin.getAmount();
coinCounterMap.put(coin, coinNumbers);
vendingMoney %= coin.getAmount();
}
return coinCounterMap;
}
public Map<Coin, Integer> getRemainCounter(MachineAmount amount, int remainder) {
int vendingMoney = amount.getMoney();
if (remainder >= vendingMoney) {
for (Coin remainderCoin : Coin.values()) {
int remainCoinNumbers = vendingMoney / remainderCoin.getAmount();
remainCounterMap.put(remainderCoin, remainCoinNumbers);
vendingMoney %= remainderCoin.getAmount();
}
}
return remainCounterMap;
}
}