Skip to content

feat: adds savings account and businesscheckingaccount class made modification to other class necessary #566

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

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
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
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
import java.util.Set;

/**
* @author vscode
*/
public class BankAccount {

protected final Set<Customer> owners;
protected final String accountNumber;
protected double balance;
protected boolean isActive;

public BankAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
this.balance = initialBalance;
isActive = true;
}

/**
* Gets the account number.
*
* @return The account number.
*/
public String getAccountNumber() {
return accountNumber;
}

/**
* Gets the owners of the account.
*
* @return The owners of the account.
*/
public Set<Customer> getOwners() {
return owners;
}

/**
* Deposits funds into the account.
*
* @param amount The amount to deposit.
*/
public void deposit(double amount) throws IllegalStateException {
if (isClosed()) {
throw new IllegalStateException("Cannot deposit to a closed account");
}
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive");
}
balance += amount;
}

/**
* Withdraws funds from the account.
*
* @param amount
* @throws InsufficientFundsException
*/
public void withdraw(double amount) throws InsufficientFundsException {
if (isClosed()) {
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;
}

/**
* Gets the balance of the account.
*
* @return The balance of the account.
*/
public double getBalance() {
return balance;
}

/** Closes the account. */
public void closeAccount() throws IllegalStateException {
if (balance > 0) {
throw new IllegalStateException("Cannot close account with a positive balance");
}
isActive = false;
}

/**
* Checks if the account is closed.
*
* @return True if the account is closed, otherwise false.
*/
public boolean isClosed() {
return !isActive;
}

@Override
public int hashCode() {
return accountNumber.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof BankAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
}

public String toString() {
return null;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,14 +10,14 @@
public class BankAtm {

private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
private final Map<String, BankAccount> accountByNumber = new HashMap<>();

/**
* Adds a checking account to the bank.
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
public void addAccount(BankAccount account) {
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
Expand All @@ -33,7 +33,7 @@ public void addAccount(CheckingAccount account) {
* @param customerId The ID of the customer.
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
public Set<BankAccount> findAccountsByCustomerId(UUID customerId) {
return customerById.containsKey(customerId)
? customerById.get(customerId).getAccounts()
: Set.of();
Expand All @@ -46,7 +46,7 @@ public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
* @param amount The amount to deposit.
*/
public void depositFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.deposit(amount);
}

Expand All @@ -57,7 +57,7 @@ public void depositFunds(String accountNumber, double amount) {
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
}

Expand All @@ -68,7 +68,7 @@ public void depositFunds(String accountNumber, Check check) {
* @param amount
*/
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
BankAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
}

Expand All @@ -78,8 +78,8 @@ public void withdrawFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @return The account.
*/
private CheckingAccount getAccountOrThrow(String accountNumber) {
CheckingAccount account = accountByNumber.get(accountNumber);
private BankAccount getAccountOrThrow(String accountNumber) {
BankAccount account = accountByNumber.get(accountNumber);
if (account == null || account.isClosed()) {
throw new AccountNotFoundException("Account not found");
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
package com.codedifferently.lesson17.bank;

public class BusinessCheckingAccount {}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ public void voidCheck() {
/**
* Deposits the check into an account.
*
* @param toAccount The account to deposit the check into.
* @param account2 The account to deposit the check into.
*/
public void depositFunds(CheckingAccount toAccount) {
public void depositFunds(BankAccount account2) {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
}
account.withdraw(amount);
toAccount.deposit(amount);
account2.deposit(amount);
voidCheck();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
package com.codedifferently.lesson17.bank;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
import java.util.Set;

/** Represents a checking account. */
public class CheckingAccount {

private final Set<Customer> owners;
private final String accountNumber;
private double balance;
private boolean isActive;
public class CheckingAccount extends BankAccount {

/**
* Creates a new checking account.
Expand All @@ -19,101 +13,7 @@ public class CheckingAccount {
* @param initialBalance The initial balance of the account.
*/
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
this.accountNumber = accountNumber;
this.owners = owners;
this.balance = initialBalance;
isActive = true;
}

/**
* Gets the account number.
*
* @return The account number.
*/
public String getAccountNumber() {
return accountNumber;
}

/**
* Gets the owners of the account.
*
* @return The owners of the account.
*/
public Set<Customer> getOwners() {
return owners;
}

/**
* Deposits funds into the account.
*
* @param amount The amount to deposit.
*/
public void deposit(double amount) throws IllegalStateException {
if (isClosed()) {
throw new IllegalStateException("Cannot deposit to a closed account");
}
if (amount <= 0) {
throw new IllegalArgumentException("Deposit amount must be positive");
}
balance += amount;
}

/**
* Withdraws funds from the account.
*
* @param amount
* @throws InsufficientFundsException
*/
public void withdraw(double amount) throws InsufficientFundsException {
if (isClosed()) {
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;
}

/**
* Gets the balance of the account.
*
* @return The balance of the account.
*/
public double getBalance() {
return balance;
}

/** Closes the account. */
public void closeAccount() throws IllegalStateException {
if (balance > 0) {
throw new IllegalStateException("Cannot close account with a positive balance");
}
isActive = false;
}

/**
* Checks if the account is closed.
*
* @return True if the account is closed, otherwise false.
*/
public boolean isClosed() {
return !isActive;
}

@Override
public int hashCode() {
return accountNumber.hashCode();
}

@Override
public boolean equals(Object obj) {
if (obj instanceof CheckingAccount other) {
return accountNumber.equals(other.accountNumber);
}
return false;
super(accountNumber, owners, initialBalance);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class Customer {

private final UUID id;
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();
private final Set<BankAccount> accounts = new HashSet<>();

/**
* Creates a new customer.
Expand Down Expand Up @@ -45,7 +45,7 @@ public String getName() {
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
public void addAccount(BankAccount account) {
accounts.add(account);
}

Expand All @@ -54,7 +54,7 @@ public void addAccount(CheckingAccount account) {
*
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> getAccounts() {
public Set<BankAccount> getAccounts() {
return accounts;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

public class SavingsAccount extends BankAccount {

public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}

@Override
public String toString() {
return "SavingsAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -43,14 +43,14 @@ void testAddAccount() {
classUnderTest.addAccount(account3);

// Assert
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer3.getId());
assertThat(accounts).containsOnly(account3);
}

@Test
void testFindAccountsByCustomerId() {
// Act
Set<CheckingAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());
Set<BankAccount> accounts = classUnderTest.findAccountsByCustomerId(customer1.getId());

// Assert
assertThat(accounts).containsOnly(account1, account2);
Expand Down
Loading