Skip to content

feat: try to add savingAccount to bankATMLesson 17 #525

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
fc92fbb
feat:mbLesson_09
Mar 25, 2025
e42056b
Merge branch 'code-differently:main' into main
Tezz03 Mar 26, 2025
d7ca3b3
Merge branch 'code-differently:main' into main
Tezz03 Mar 27, 2025
42cbca7
Merge branch 'code-differently:main' into main
Tezz03 Mar 28, 2025
c57408a
Delete lesson_09/types/types_app/src/main/java/com/codedifferently/le…
Tezz03 Mar 28, 2025
af8eef3
Delete lesson_09/types/types_app/src/main/resources/data/montezb.json
Tezz03 Mar 28, 2025
7b24560
Merge branch 'code-differently:main' into main
Tezz03 Mar 29, 2025
d3239c0
Merge branch 'code-differently:main' into main
Tezz03 Mar 31, 2025
41cb92f
Merge branch 'code-differently:main' into main
Tezz03 Apr 2, 2025
70d28f7
Merge branch 'code-differently:main' into main
Tezz03 Apr 3, 2025
1dde77b
Merge branch 'code-differently:main' into main
Tezz03 Apr 5, 2025
74fb0ca
Merge branch 'code-differently:main' into main
Tezz03 Apr 7, 2025
957c931
Merge branch 'code-differently:main' into main
Tezz03 Apr 10, 2025
636c494
Merge branch 'code-differently:main' into main
Tezz03 Apr 11, 2025
f76c59d
Merge branch 'code-differently:main' into main
Tezz03 Apr 13, 2025
1e6771f
Feat: Need more time
Apr 14, 2025
9c6bf25
fix:lesson_17
Apr 16, 2025
4ade1fc
adding file
Apr 16, 2025
133525f
fix:adding a file
Apr 16, 2025
98052f7
fix:had to run gradle spotless apply and the tests
Apr 16, 2025
ba031fa
fix:made the isCheckCreationAllowed variable final in Savings Account…
Apr 16, 2025
3f8cffa
fix: ran gradle ./gradlew :bank_app:spotlessApply to fix the formatti…
Apr 16, 2025
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,132 @@
package com.codedifferently.lesson17.bank;

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

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

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

/**
* Creates a new checking account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
// Constructor
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;
}

@Override
public String toString() {
return "BankAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,20 @@
import java.util.Map;
import java.util.Set;
import java.util.UUID;
import java.util.stream.Collectors;

/** Represents a bank ATM. */
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.
* Adds an 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,9 +34,9 @@ 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()
? customerById.get(customerId).getAccounts().stream().collect(Collectors.toSet())
: Set.of();
}

Expand All @@ -46,7 +47,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 @@ -56,8 +57,8 @@ public void depositFunds(String accountNumber, double amount) {
* @param accountNumber The account number.
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
public void depositFunds(String accountNumber, Check check) throws Exception {
BankAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
}

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

Expand All @@ -78,8 +79,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,34 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;

public class BusinessAccount extends CheckingAccount {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Prolly should be called BusinessCheckingAccount for more clarity.


/**
* Creates a new business account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public BusinessAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Need to check that one of the owners is a business, right?

super(accountNumber, owners, initialBalance);
}

/**
* Withdraws funds from the account.
*
* @param amount The amount to withdraw.
* @throws InsufficientFundsException If there are insufficient funds in the account.
*/
@Override
public void withdraw(double amount) throws InsufficientFundsException {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Don't even need to override the superclass method since it does the same thing.

if (amount > getBalance()) {
throw new InsufficientFundsException("Insufficient funds in the account.");
}
super.withdraw(amount);
}

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

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

/**
* Creates a business account.
*
* @param accountNumber The account number.
* @param owners The owners of the account.
* @param initialBalance The initial balance of the account.
*/
public class BusinessCheckingAccount extends CheckingAccount {
public BusinessCheckingAccount(
String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);

boolean hasBusinessOwner = owners.stream().anyMatch(customer -> customer.isBusiness());
if (!hasBusinessOwner) {
throw new InvalidBusinessAccountException("At least one owner must be a business.");
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;

/** Represents a check. */
public class Check {

private final String checkNumber;
Expand Down Expand Up @@ -44,8 +43,9 @@ public void voidCheck() {
* Deposits the check into an account.
*
* @param toAccount The account to deposit the check into.
* @throws Exception
*/
public void depositFunds(CheckingAccount toAccount) {
public void depositFunds(BankAccount toAccount) throws Exception {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
}
Expand Down
Loading
Loading