|
| 1 | +package com.codedifferently.lesson17.bank; |
| 2 | + |
| 3 | +import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException; |
| 4 | +import java.util.HashMap; |
| 5 | +import java.util.Map; |
| 6 | +import java.util.Set; |
| 7 | +import java.util.UUID; |
| 8 | + |
| 9 | +/** Represents a bank ATM. */ |
| 10 | +public class BankAtm { |
| 11 | + |
| 12 | + private final Map<UUID, Customer> customerById = new HashMap<>(); |
| 13 | + private final Map<String, CheckingAccount> accountByNumber = new HashMap<>(); |
| 14 | + |
| 15 | + /** |
| 16 | + * Adds a checking account to the bank. |
| 17 | + * |
| 18 | + * @param account The account to add. |
| 19 | + */ |
| 20 | + public void addAccount(CheckingAccount account) { |
| 21 | + accountByNumber.put(account.getAccountNumber(), account); |
| 22 | + account |
| 23 | + .getOwners() |
| 24 | + .forEach( |
| 25 | + owner -> { |
| 26 | + customerById.put(owner.getId(), owner); |
| 27 | + }); |
| 28 | + } |
| 29 | + |
| 30 | + /** |
| 31 | + * Finds all accounts owned by a customer. |
| 32 | + * |
| 33 | + * @param customerId The ID of the customer. |
| 34 | + * @return The unique set of accounts owned by the customer. |
| 35 | + */ |
| 36 | + public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) { |
| 37 | + return customerById.containsKey(customerId) |
| 38 | + ? customerById.get(customerId).getAccounts() |
| 39 | + : Set.of(); |
| 40 | + } |
| 41 | + |
| 42 | + /** |
| 43 | + * Deposits funds into an account. |
| 44 | + * |
| 45 | + * @param accountNumber The account number. |
| 46 | + * @param amount The amount to deposit. |
| 47 | + */ |
| 48 | + public void depositFunds(String accountNumber, double amount) { |
| 49 | + CheckingAccount account = getAccountOrThrow(accountNumber); |
| 50 | + account.deposit(amount); |
| 51 | + } |
| 52 | + |
| 53 | + /** |
| 54 | + * Deposits funds into an account using a check. |
| 55 | + * |
| 56 | + * @param accountNumber The account number. |
| 57 | + * @param check The check to deposit. |
| 58 | + */ |
| 59 | + public void depositFunds(String accountNumber, Check check) { |
| 60 | + CheckingAccount account = getAccountOrThrow(accountNumber); |
| 61 | + check.depositFunds(account); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Withdraws funds from an account. |
| 66 | + * |
| 67 | + * @param accountNumber |
| 68 | + * @param amount |
| 69 | + */ |
| 70 | + public void withdrawFunds(String accountNumber, double amount) { |
| 71 | + CheckingAccount account = getAccountOrThrow(accountNumber); |
| 72 | + account.withdraw(amount); |
| 73 | + } |
| 74 | + |
| 75 | + /** |
| 76 | + * Gets an account by its number or throws an exception if not found. |
| 77 | + * |
| 78 | + * @param accountNumber The account number. |
| 79 | + * @return The account. |
| 80 | + */ |
| 81 | + private CheckingAccount getAccountOrThrow(String accountNumber) { |
| 82 | + CheckingAccount account = accountByNumber.get(accountNumber); |
| 83 | + if (account == null || account.isClosed()) { |
| 84 | + throw new AccountNotFoundException("Account not found"); |
| 85 | + } |
| 86 | + return account; |
| 87 | + } |
| 88 | +} |
0 commit comments