Skip to content

Commit 5943cb0

Browse files
committed
Adjust ATM task solution to make ATM & ATM2 look the same
1 parent 4062da8 commit 5943cb0

File tree

2 files changed

+12
-8
lines changed

2 files changed

+12
-8
lines changed

src/main/java/by/andd3dfx/common/ATM.java

+11-8
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,11 @@ public ATM(Map<Integer, Integer> state) {
3434
public Map<Integer, Integer> withdraw(int amount) {
3535
// Try to make withdraw using banknote of highest nominal,
3636
// in case of fail - try to start from next nominal
37-
for (int startingBanknoteIndex = 0; startingBanknoteIndex < nominals.size(); startingBanknoteIndex++) {
38-
var result = withdraw(amount, startingBanknoteIndex);
39-
40-
if (result != null) {
41-
return result;
37+
for (int i = 0; i < nominals.size(); i++) {
38+
try {
39+
return withdraw(amount, i);
40+
} catch (IllegalStateException ex) {
41+
// do nothing
4242
}
4343
}
4444

@@ -66,13 +66,16 @@ private Map<Integer, Integer> withdraw(int amount, int startingBanknoteIndex) {
6666
}
6767

6868
if (amount > 0) {
69-
return null;
69+
throw new IllegalStateException("Could not perform withdraw!");
7070
}
7171

72+
mutateAtm(result);
73+
return result;
74+
}
75+
76+
private void mutateAtm(Map<Integer, Integer> result) {
7277
for (var nominal : result.keySet()) {
7378
state.put(nominal, state.get(nominal) - result.get(nominal));
7479
}
75-
76-
return result;
7780
}
7881
}

src/main/java/by/andd3dfx/common/ATM2.java

+1
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ public Map<Integer, Integer> withdraw(int amount) {
4141
// do nothing
4242
}
4343
}
44+
4445
throw new IllegalStateException("Could not perform withdraw!");
4546
}
4647

0 commit comments

Comments
 (0)