-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathAtmUsingRecursion.java
49 lines (41 loc) · 1.65 KB
/
AtmUsingRecursion.java
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
package by.andd3dfx.common.atm;
import java.util.HashMap;
import java.util.Map;
/**
* <pre>
* Есть банкомат (ATM), который заряжают купюрами.
* Надо реализовать метод withdraw() для выдачи заданной суммы amount имеющимися в банкомате купюрами.
* Метод withdraw() - мутирующий, т.е. меняет состояние банкомата после вызова (кол-во купюр может уменьшиться).
* </pre>
*
* @see <a href="https://youtu.be/0-BL-NO9-B8">Video solution</a>
*/
public class AtmUsingRecursion extends AbstractAtm {
public AtmUsingRecursion(Map<Integer, Integer> state) {
super(state);
}
@Override
protected Map<Integer, Integer> withdraw(int amount, int nominalIndex) {
if (amount == 0) {
return Map.of();
}
if (nominalIndex >= nominals.size()) {
throw new IllegalStateException("Could not perform withdraw!");
}
var nominal = nominals.get(nominalIndex);
if (nominal > amount || state.get(nominal) == 0) {
return withdraw(amount, nominalIndex + 1);
}
var result = new HashMap<Integer, Integer>();
int count = amount / nominal;
count = Math.min(count, state.get(nominal));
result.put(nominal, count);
amount -= nominal * count;
if (amount > 0) {
result.putAll(withdraw(amount, nominalIndex + 1));
return result;
}
mutateAtm(result);
return result;
}
}