|
1 | 1 | package com.codedifferently.lesson17.bank;
|
2 | 2 |
|
3 |
| -import java.util.HashSet; |
4 | 3 | import java.util.Set;
|
5 | 4 |
|
6 |
| -public abstract class BankAccount { |
| 5 | +import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; |
| 6 | + |
| 7 | +public class BankAccount { |
7 | 8 | protected String accountNumber;
|
8 | 9 | protected Set<Customer> owners;
|
9 | 10 | protected double balance;
|
10 |
| - protected boolean closed; |
| 11 | + protected boolean isActive; |
| 12 | + private static final double LARGE_TRANSACTION_THRESHOLD = 5000.0; |
11 | 13 |
|
12 |
| - public BankAccount(String accountNumber, Customer owner) { |
| 14 | + public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) { |
13 | 15 | this.accountNumber = accountNumber;
|
14 |
| - this.owners = new HashSet<>(Set.of(owner)); |
15 |
| - this.balance = 0.0; |
16 |
| - this.closed = false; |
17 |
| - owner.addAccount(this); |
| 16 | + this.owners = owners; |
| 17 | + this.balance = initialBalance; |
| 18 | + isActive = true; |
18 | 19 | }
|
19 | 20 |
|
| 21 | + /** |
| 22 | + * Gets the account number. |
| 23 | + * |
| 24 | + * @return The account number. |
| 25 | + */ |
20 | 26 | public String getAccountNumber() {
|
21 | 27 | return accountNumber;
|
22 | 28 | }
|
23 | 29 |
|
| 30 | + /** |
| 31 | + * Gets the owners of the account. |
| 32 | + * |
| 33 | + * @return The owners of the account. |
| 34 | + */ |
24 | 35 | public Set<Customer> getOwners() {
|
25 | 36 | return owners;
|
26 | 37 | }
|
27 | 38 |
|
| 39 | + /** |
| 40 | + * Deposits funds into the account. |
| 41 | + * |
| 42 | + * @param amount The amount to deposit. |
| 43 | + */ |
| 44 | + public void deposit(double amount) throws IllegalStateException { |
| 45 | + if (isClosed()) { |
| 46 | + throw new IllegalStateException("Cannot deposit to a closed account"); |
| 47 | + } |
| 48 | + if (amount <= 0) { |
| 49 | + throw new IllegalArgumentException("Deposit amount must be positive"); |
| 50 | + } |
| 51 | + if (amount > LARGE_TRANSACTION_THRESHOLD) { |
| 52 | + System.out.println("Warning: Large transaction detected for deposit."); |
| 53 | + } |
| 54 | + balance += amount; |
| 55 | + } |
| 56 | + |
| 57 | + |
| 58 | + // Withdraws funds from the account. |
| 59 | + |
| 60 | + public void withdraw(double amount) throws InsufficientFundsException { |
| 61 | + if (isClosed()) { |
| 62 | + throw new IllegalStateException("Cannot withdraw from a closed account"); |
| 63 | + } |
| 64 | + if (amount <= 0) { |
| 65 | + throw new IllegalStateException("Withdrawal amount must be positive"); |
| 66 | + } |
| 67 | + if (amount > LARGE_TRANSACTION_THRESHOLD) { |
| 68 | + System.out.println("Warning: Large transaction detected for withdrawal."); |
| 69 | + } |
| 70 | + if (balance < amount) { |
| 71 | + throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); |
| 72 | + } |
| 73 | + balance -= amount; |
| 74 | + } |
| 75 | + /** |
| 76 | + * Checks if the account is active. |
| 77 | + * |
| 78 | + * @return True if the account is active, otherwise false. |
| 79 | + */ |
| 80 | + |
| 81 | + /** |
| 82 | + * Gets the balance of the account. |
| 83 | + * |
| 84 | + * @return The balance of the account. |
| 85 | + */ |
28 | 86 | public double getBalance() {
|
29 | 87 | return balance;
|
30 | 88 | }
|
31 | 89 |
|
| 90 | + /** Closes the account. */ |
| 91 | + public void closeAccount() throws IllegalStateException { |
| 92 | + if (balance > 0) { |
| 93 | + throw new IllegalStateException("Cannot close account with a positive balance"); |
| 94 | + } |
| 95 | + isActive = false; |
| 96 | + } |
| 97 | + |
| 98 | + /** |
| 99 | + * Checks if the account is closed. |
| 100 | + * |
| 101 | + * @return True if the account is closed, otherwise false. |
| 102 | + */ |
32 | 103 | public boolean isClosed() {
|
33 |
| - return closed; |
| 104 | + return !isActive; |
34 | 105 | }
|
35 | 106 |
|
36 |
| - public void deposit(double amount) { |
37 |
| - balance += amount; |
| 107 | + @Override |
| 108 | + public int hashCode() { |
| 109 | + return accountNumber.hashCode(); |
38 | 110 | }
|
39 | 111 |
|
40 |
| - public void withdraw(double amount) { |
41 |
| - balance -= amount; |
| 112 | + @Override |
| 113 | + public boolean equals(Object obj) { |
| 114 | + if (obj instanceof BankAccount other) { |
| 115 | + return accountNumber.equals(other.accountNumber); |
| 116 | + } |
| 117 | + return false; |
42 | 118 | }
|
43 | 119 |
|
44 |
| - public void closeAccount() { |
45 |
| - closed = true; |
| 120 | + @Override |
| 121 | + public String toString() { |
| 122 | + return "BnnkAccount{" |
| 123 | + + "accountNumber='" |
| 124 | + + accountNumber |
| 125 | + + '\'' |
| 126 | + + ", balance=" |
| 127 | + + balance |
| 128 | + + ", isActive=" |
| 129 | + + isActive |
| 130 | + + '}'; |
46 | 131 | }
|
47 | 132 | }
|
| 133 | + |
0 commit comments