Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 52 additions & 7 deletions src/main/java/hse/java/lectures/lecture3/tasks/atm/Atm.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@

public class Atm {
public enum Denomination {
D50(50),
D100(100),
D500(500),
D5000(5000),
D1000(1000),
D5000(5000);
D500(500),
D100(100),
D50(50);

private final int value;

Expand All @@ -32,14 +32,59 @@ public static Denomination fromInt(int value) {
public Atm() {
}

public void deposit(Map<Denomination, Integer> banknotes){}
public void deposit(Map<Denomination, Integer> banknotes) {
if (banknotes == null || banknotes.isEmpty()) {
throw new InvalidDepositException("Deposit has to be non-empty");
}

for (Map.Entry<Denomination, Integer> entry : banknotes.entrySet()) {
if (entry.getValue() <= 0) {
throw new InvalidDepositException("Denomination has to be positive value");
}

this.banknotes.merge(entry.getKey(), entry.getValue(), Integer::sum);
}
}

public Map<Denomination, Integer> withdraw(int amount) {
return Map.of();
if (amount <= 0) {
throw new InvalidAmountException("Amount has to be positive value");
} else if (amount > getBalance()) {
throw new InsufficientFundsException("Not enough funds in ATM");
}

Map<Denomination, Integer> change = new EnumMap<>(Denomination.class);

for (Map.Entry<Denomination, Integer> entry : banknotes.entrySet()) {
if (amount == 0) break;

Denomination denomination = entry.getKey();
Integer denominationAmount = entry.getValue();

int times = Integer.min(amount / denomination.value, denominationAmount);

amount -= denomination.value * times;
change.put(denomination, times);
}

if (amount > 0) {
throw new CannotDispenseException("Cannot dispense");
}

for (Map.Entry<Denomination, Integer> entry : change.entrySet()) {
int times = banknotes.get(entry.getKey());
banknotes.put(entry.getKey(), times - entry.getValue());
}

return change;
}

public int getBalance() {
return 0;
int sum = 0;
for (Map.Entry<Denomination, Integer> entry : banknotes.entrySet()) {
sum += entry.getKey().value * entry.getValue();
}
return sum;
}

}
Loading