-
Notifications
You must be signed in to change notification settings - Fork 24
feat: implement bank account system with audit logging and support fo… #528
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
bc81ec0
13eee25
c3f6c6b
fe5afad
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.ArrayList; | ||
import java.util.HashMap; | ||
|
||
/** | ||
* Creates a new saving account. | ||
* | ||
* @param accountNumber The account number as log transaction key. | ||
* @param value The actual debit/credit transaction. | ||
* @param accountNumberByValueLog The audit log object. | ||
*/ | ||
public class AuditLog { | ||
|
||
// Method to add a value to an existing ArrayList or create a new one if accountNumber doesn't | ||
// exist | ||
public void addToMap( | ||
HashMap<String, ArrayList<Double>> accountNumberByValueLog, | ||
String accountNumber, | ||
Double value) { | ||
accountNumberByValueLog.computeIfAbsent(accountNumber, k -> new ArrayList<>()).add(value); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.HashSet; | ||
import java.util.Set; | ||
|
||
public abstract class BankAccount { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Instead of building your own |
||
protected String accountNumber; | ||
protected Set<Customer> owners; | ||
protected double balance; | ||
protected boolean closed; | ||
|
||
public BankAccount(String accountNumber, Customer owner) { | ||
this.accountNumber = accountNumber; | ||
this.owners = new HashSet<>(Set.of(owner)); | ||
this.balance = 0.0; | ||
this.closed = false; | ||
owner.addAccount(this); | ||
} | ||
|
||
public String getAccountNumber() { | ||
return accountNumber; | ||
} | ||
|
||
public Set<Customer> getOwners() { | ||
return owners; | ||
} | ||
|
||
public double getBalance() { | ||
return balance; | ||
} | ||
|
||
public boolean isClosed() { | ||
return closed; | ||
} | ||
|
||
public void deposit(double amount) { | ||
balance += amount; | ||
} | ||
|
||
public void withdraw(double amount) { | ||
balance -= amount; | ||
} | ||
|
||
public void closeAccount() { | ||
closed = true; | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,14 @@ | ||
package com.codedifferently.lesson17.bank; | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
import java.util.Set; | ||
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException; | ||
|
||
import main.java.com.codedifferently.lesson17.bank.BankAccount; | ||
|
||
public class CheckingAccount extends BankAccount { | ||
|
||
/** Represents a checking account. */ | ||
public class CheckingAccount { | ||
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance);{ | ||
|
||
private final Set<Customer> owners; | ||
private final String accountNumber; | ||
|
@@ -128,4 +132,19 @@ public String toString() { | |
+ isActive | ||
+ '}'; | ||
} | ||
@Override | ||
public void withdraw(double amount) throws InsufficientFundsException { | ||
if (isClosed()) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This should all be in the superclass since these are good checks for all accounts, no matter the type. |
||
throw new IllegalStateException("Cannot withdraw from a closed account"); | ||
} | ||
if (amount <= 0) { | ||
throw new IllegalStateException("Withdrawal amount must be positive"); | ||
} | ||
if (balance < amount) { | ||
throw new InsufficientFundsException("Account does not have enough funds for withdrawal"); | ||
} | ||
balance -= amount; | ||
} | ||
|
||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,18 @@ public class Customer { | |
private final String name; | ||
private final Set<CheckingAccount> accounts = new HashSet<>(); | ||
|
||
private boolean isBusiness; | ||
|
||
public Customer(String name, boolean isBusiness) { | ||
this.name = name; | ||
this.id = UUID.randomUUID(); | ||
this.isBusiness = isBusiness; | ||
} | ||
|
||
public boolean isBusiness() { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice. |
||
return isBusiness; | ||
} | ||
|
||
/** | ||
* Creates a new customer. | ||
* | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package com.codedifferently.lesson17.bank; | ||
|
||
import java.util.Set; | ||
|
||
/** A savings account does not support checks. */ | ||
public class SavingsAccount extends BankAccount { | ||
|
||
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) { | ||
super(accountNumber, owners, initialBalance); | ||
} | ||
|
||
// No check support = no extra method needed! | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is weird. Maybe do some research on how to build a logger object. No too difficult to make a simple one.