Skip to content

Commit e8dbe17

Browse files
committed
feat: added Audit log and Savings Account class with test By Yemi
1 parent d46c282 commit e8dbe17

File tree

6 files changed

+323
-115
lines changed

6 files changed

+323
-115
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
6+
/**
7+
* Creates a new saving account.
8+
*
9+
* @param accountNumber The account number as log transaction key.
10+
* @param value The actual debit/credit transaction.
11+
* @param accountNumberByValueLog The audit log object.
12+
*/
13+
public class AuditLog {
14+
15+
// Method to add a value to an existing ArrayList or create a new one if accountNumber doesn't exist
16+
public void addToMap(HashMap<String, ArrayList<Double>> accountNumberByValueLog, String accountNumber, Double value) {
17+
accountNumberByValueLog.computeIfAbsent(accountNumber, k -> new ArrayList<>()).add(value);
18+
}
19+
}
Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
import org.slf4j.Logger;
6+
import org.slf4j.LoggerFactory;
7+
8+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
9+
10+
/** Represents a checking account. */
11+
public class BankAccount {
12+
13+
private final Set<Customer> owners;
14+
private final String accountNumber;
15+
private double balance;
16+
private boolean isActive;
17+
private static final Logger logger = LoggerFactory.getLogger(SavingAccount.class);
18+
19+
20+
/**
21+
* Creates a new checking account.
22+
*
23+
* @param accountNumber The account number.
24+
* @param owners The owners of the account.
25+
* @param initialBalance The initial balance of the account.
26+
*/
27+
public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
28+
this.accountNumber = accountNumber;
29+
this.owners = owners;
30+
this.balance = initialBalance;
31+
isActive = true;
32+
}
33+
34+
/**
35+
* Gets the account number.
36+
*
37+
* @return The account number.
38+
*/
39+
public String getAccountNumber() {
40+
return accountNumber;
41+
}
42+
43+
/**
44+
* Gets the owners of the account.
45+
*
46+
* @return The owners of the account.
47+
*/
48+
public Set<Customer> getOwners() {
49+
return owners;
50+
}
51+
52+
/**
53+
* Deposits funds into the account.
54+
*
55+
* @param amount The amount to deposit.
56+
*/
57+
public void deposit(double amount) throws IllegalStateException {
58+
if (isClosed()) {
59+
logger.info("Cannot deposit to a closed account");
60+
throw new IllegalStateException("Cannot deposit to a closed account");
61+
}
62+
if (amount <= 0) {
63+
logger.info("Deposit amount must be positive");
64+
throw new IllegalArgumentException("Deposit amount must be positive");
65+
}
66+
balance += amount;
67+
}
68+
69+
/**
70+
* Withdraws funds from the account.
71+
*
72+
* @param amount
73+
* @throws InsufficientFundsException
74+
*/
75+
public void withdraw(double amount) throws InsufficientFundsException {
76+
if (isClosed()) {
77+
logger.info("Cannot withdraw from a closed account");
78+
throw new IllegalStateException("Cannot withdraw from a closed account");
79+
}
80+
if (amount <= 0) {
81+
logger.info("Withdrawal amount must be positive");
82+
throw new IllegalStateException("Withdrawal amount must be positive");
83+
}
84+
if (balance < amount) {
85+
logger.info("Account does not have enough funds for withdrawal");
86+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
87+
}
88+
balance -= amount;
89+
}
90+
91+
/**
92+
* Gets the balance of the account.
93+
*
94+
* @return The balance of the account.
95+
*/
96+
public double getBalance() {
97+
return balance;
98+
}
99+
100+
/** Closes the account. */
101+
public void closeAccount() throws IllegalStateException {
102+
if (balance > 0) {
103+
logger.info("Cannot close account with a positive balance");
104+
throw new IllegalStateException("Cannot close account with a positive balance");
105+
}
106+
isActive = false;
107+
}
108+
109+
/**
110+
* Checks if the account is closed.
111+
*
112+
* @return True if the account is closed, otherwise false.
113+
*/
114+
public boolean isClosed() {
115+
return !isActive;
116+
}
117+
118+
@Override
119+
public int hashCode() {
120+
return accountNumber.hashCode();
121+
}
122+
123+
@Override
124+
public boolean equals(Object obj) {
125+
if (obj instanceof BankAccount other) {
126+
return accountNumber.equals(other.accountNumber);
127+
}
128+
return false;
129+
}
130+
131+
@Override
132+
public String toString() {
133+
return "CheckingAccount{"
134+
+ "accountNumber='"
135+
+ accountNumber
136+
+ '\''
137+
+ ", balance="
138+
+ balance
139+
+ ", isActive="
140+
+ isActive
141+
+ '}';
142+
}
143+
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
package com.codedifferently.lesson17.bank;
22

33
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
4+
5+
import java.util.ArrayList;
46
import java.util.HashMap;
57
import java.util.Map;
68
import java.util.Set;
@@ -11,6 +13,8 @@ public class BankAtm {
1113

1214
private final Map<UUID, Customer> customerById = new HashMap<>();
1315
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
16+
private final AuditLog auditLog = new AuditLog();
17+
private final HashMap<String, ArrayList<Double>> accountNumberByValue = new HashMap<>();
1418

1519
/**
1620
* Adds a checking account to the bank.
@@ -48,6 +52,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
4852
public void depositFunds(String accountNumber, double amount) {
4953
CheckingAccount account = getAccountOrThrow(accountNumber);
5054
account.deposit(amount);
55+
auditLog.addToMap(accountNumberByValue, accountNumber, amount);
5156
}
5257

5358
/**
@@ -70,6 +75,7 @@ public void depositFunds(String accountNumber, Check check) {
7075
public void withdrawFunds(String accountNumber, double amount) {
7176
CheckingAccount account = getAccountOrThrow(accountNumber);
7277
account.withdraw(amount);
78+
auditLog.addToMap(accountNumberByValue, accountNumber, amount);
7379
}
7480

7581
/**
Lines changed: 5 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,11 @@
11
package com.codedifferently.lesson17.bank;
22

3-
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
43
import java.util.Set;
54

65
/** Represents a checking account. */
7-
public class CheckingAccount {
6+
public class CheckingAccount extends BankAccount{
87

9-
private final Set<Customer> owners;
10-
private final String accountNumber;
11-
private double balance;
12-
private boolean isActive;
8+
//private boolean isActive;
139

1410
/**
1511
* Creates a new checking account.
@@ -18,114 +14,8 @@ public class CheckingAccount {
1814
* @param owners The owners of the account.
1915
* @param initialBalance The initial balance of the account.
2016
*/
21-
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
22-
this.accountNumber = accountNumber;
23-
this.owners = owners;
24-
this.balance = initialBalance;
25-
isActive = true;
26-
}
27-
28-
/**
29-
* Gets the account number.
30-
*
31-
* @return The account number.
32-
*/
33-
public String getAccountNumber() {
34-
return accountNumber;
35-
}
36-
37-
/**
38-
* Gets the owners of the account.
39-
*
40-
* @return The owners of the account.
41-
*/
42-
public Set<Customer> getOwners() {
43-
return owners;
44-
}
45-
46-
/**
47-
* Deposits funds into the account.
48-
*
49-
* @param amount The amount to deposit.
50-
*/
51-
public void deposit(double amount) throws IllegalStateException {
52-
if (isClosed()) {
53-
throw new IllegalStateException("Cannot deposit to a closed account");
54-
}
55-
if (amount <= 0) {
56-
throw new IllegalArgumentException("Deposit amount must be positive");
57-
}
58-
balance += amount;
59-
}
60-
61-
/**
62-
* Withdraws funds from the account.
63-
*
64-
* @param amount
65-
* @throws InsufficientFundsException
66-
*/
67-
public void withdraw(double amount) throws InsufficientFundsException {
68-
if (isClosed()) {
69-
throw new IllegalStateException("Cannot withdraw from a closed account");
70-
}
71-
if (amount <= 0) {
72-
throw new IllegalStateException("Withdrawal amount must be positive");
73-
}
74-
if (balance < amount) {
75-
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
76-
}
77-
balance -= amount;
78-
}
79-
80-
/**
81-
* Gets the balance of the account.
82-
*
83-
* @return The balance of the account.
84-
*/
85-
public double getBalance() {
86-
return balance;
87-
}
88-
89-
/** Closes the account. */
90-
public void closeAccount() throws IllegalStateException {
91-
if (balance > 0) {
92-
throw new IllegalStateException("Cannot close account with a positive balance");
93-
}
94-
isActive = false;
95-
}
96-
97-
/**
98-
* Checks if the account is closed.
99-
*
100-
* @return True if the account is closed, otherwise false.
101-
*/
102-
public boolean isClosed() {
103-
return !isActive;
104-
}
105-
106-
@Override
107-
public int hashCode() {
108-
return accountNumber.hashCode();
109-
}
110-
111-
@Override
112-
public boolean equals(Object obj) {
113-
if (obj instanceof CheckingAccount other) {
114-
return accountNumber.equals(other.accountNumber);
115-
}
116-
return false;
117-
}
118-
119-
@Override
120-
public String toString() {
121-
return "CheckingAccount{"
122-
+ "accountNumber='"
123-
+ accountNumber
124-
+ '\''
125-
+ ", balance="
126-
+ balance
127-
+ ", isActive="
128-
+ isActive
129-
+ '}';
17+
public CheckingAccount(String accountNumber, Set<Customer> owners, double balance) {
18+
super(accountNumber, owners, balance);
19+
//isActive = true;
13020
}
13121
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
4+
import java.util.Set;
5+
6+
import org.slf4j.Logger;
7+
import org.slf4j.LoggerFactory;
8+
9+
/** Represents a saving account. */
10+
public class SavingAccount extends BankAccount{
11+
//private boolean isActive;
12+
private boolean isCheckCreationAllowed;
13+
private static final Logger logger = LoggerFactory.getLogger(SavingAccount.class);
14+
15+
/**
16+
* Creates a new saving account.
17+
*
18+
* @param accountNumber The account number.
19+
* @param owners The owners of the account.
20+
* @param initialBalance The initial balance of the account.
21+
*/
22+
public SavingAccount(String accountNumber, Set<Customer> owners, double balance) {
23+
super(accountNumber, owners, balance);
24+
logger.info("Saving Account constructor accessed...");
25+
isCheckCreationAllowed = false;
26+
}
27+
28+
public boolean isCheckCreationAllowed() {
29+
return isCheckCreationAllowed;
30+
}
31+
32+
}

0 commit comments

Comments
 (0)