-
Notifications
You must be signed in to change notification settings - Fork 351
Expand file tree
/
Copy pathCoinStore.java
More file actions
51 lines (43 loc) · 1.64 KB
/
Copy pathCoinStore.java
File metadata and controls
51 lines (43 loc) · 1.64 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
package vendingmachine;
import java.util.Collections;
import java.util.EnumMap;
import java.util.Map;
public class CoinStore {
private final Map<Coin, Integer> repository;
public CoinStore(Map<Coin, Integer> repository) {
this.repository = repository;
}
public void addCoinRandomly(Money money) {
while (money.isMoreOrEqualThen(Coin.COIN_10.getAmount())) {
Coin pickedCoin = Coin.getRandomCoin();
if (money.isLessThen(pickedCoin.getAmount())) {
continue;
}
money.minus(pickedCoin.getAmount());
addCoin(pickedCoin);
}
}
private void addCoin(Coin coin) {
repository.put(coin, repository.getOrDefault(coin, 0) + 1);
}
public Map<Coin, Integer> getChange(Money holdingMoney) {
Map<Coin, Integer> change = new EnumMap<>(Coin.class);
Coin.getCoinOrderedList()
.forEach((coin) -> handleChange(change, coin, holdingMoney));
return change;
}
private void handleChange(Map<Coin, Integer> change, Coin coin, Money holdingMoney) {
if (repository.get(coin) == null || repository.get(coin) <= 0) {
return;
}
if (holdingMoney.getAmount() >= coin.getAmount()) {
int quantity = Math.min(holdingMoney.getAmount() / coin.getAmount(), repository.get(coin));
change.put(coin, quantity);
repository.put(coin, repository.get(coin) - quantity);
holdingMoney.minus(coin.getAmount() * quantity);
}
}
public Map<Coin, Integer> getRepository() {
return Collections.unmodifiableMap(repository);
}
}