Skip to content

Feat: Chigazo Lesson 17 SavingsAccount & MoneyOrder; #548

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 46 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
887b8f0
Merge pull request #1 from code-differently/main
A1-4U2T1NN Sep 26, 2024
05ad627
Merge branch 'code-differently:main' into main
A1-4U2T1NN Sep 27, 2024
5715b6a
Merge branch 'code-differently:main' into main
A1-4U2T1NN Sep 30, 2024
6c909b0
Merge branch 'code-differently:main' into main
A1-4U2T1NN Sep 30, 2024
4c1a3f2
Merge branch 'code-differently:main' into main
A1-4U2T1NN Sep 30, 2024
de19403
Merge branch 'code-differently:main' into main
A1-4U2T1NN Sep 30, 2024
56aa83d
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 1, 2024
8529105
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 2, 2024
4f76813
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 3, 2024
48bf962
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 5, 2024
1da88b9
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 7, 2024
3068765
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 9, 2024
712efd6
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 11, 2024
5db7413
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 11, 2024
5096f8e
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 12, 2024
09341aa
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 14, 2024
a8f634e
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 15, 2024
b643a4c
feat: created chigazograham.json file
Oct 16, 2024
cae2152
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 16, 2024
68ccb5f
fix: deleted lesson_09 content from main;
Oct 16, 2024
7d4f86f
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 16, 2024
473eb98
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 17, 2024
1d19106
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 17, 2024
bba5af5
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 18, 2024
a51c852
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 19, 2024
8a39fcc
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 21, 2024
ac98745
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 22, 2024
eade00f
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 23, 2024
bf252ad
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 23, 2024
955b86f
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 25, 2024
b6f63bd
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 28, 2024
22db65b
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 28, 2024
d5eff9e
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 28, 2024
2a09493
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 29, 2024
33e9bbc
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 30, 2024
f6f3267
Merge branch 'code-differently:main' into main
A1-4U2T1NN Oct 30, 2024
be048f2
Merge branch 'code-differently:main' into main
A1-4U2T1NN Nov 1, 2024
49eaa8c
Merge branch 'code-differently:main' into main
A1-4U2T1NN Nov 1, 2024
11135a9
Merge branch 'code-differently:main' into main
A1-4U2T1NN Nov 2, 2024
e2b48dd
feat: created abstract class 'BankAccount' that uses all base methods…
Nov 4, 2024
1494a09
test: added general test for 'SavingsAccount' class;
Nov 4, 2024
1eab727
Merge branch 'code-differently:main' into lesson_17
A1-4U2T1NN Nov 6, 2024
379f71e
feat: created SavingsAccount and corresponding test; Created abstract…
Nov 6, 2024
9f1b0be
Merge branch 'lesson_17' of https://github.com/A1-4U2T1NN/code-differ…
Nov 6, 2024
8152ceb
ffix: ran ./gradlew :bank_app:spotlessApply to correct syntax;
Nov 6, 2024
3a7943b
fix: removed caccidental 'gi' from depositFunds
Nov 6, 2024
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,103 @@
package com.codedifferently.lesson17.bank;

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

public abstract class BankAccount {
protected final Set<Customer> owners;
protected final String accountNumber;
protected double balance;
protected 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.
*/
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;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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 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,88 +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;
super(accountNumber, owners, initialBalance);
}

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

public class MoneyOrder {
private final BankAccount account;
private final double amount;

public MoneyOrder(BankAccount account, double amount) {
this.account = account;
this.amount = amount;

account.withdraw(amount);
}

public String getAmount() {
return "$" + amount + " Taken from: " + account;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package com.codedifferently.lesson17.bank;

import java.util.Set;

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

/**
* 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.
*/
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
super(accountNumber, owners, initialBalance);
}

public void writeCheck(double amount) {
throw new UnsupportedOperationException("Cannot write checks against savings accounts.");
}

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

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

@Override
public String toString() {
return "SavingsAccount{"
+ "accountNumber='"
+ accountNumber
+ '\''
+ ", balance="
+ balance
+ ", isActive="
+ isActive
+ '}';
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package com.codedifferently.lesson17.bank;

import static org.junit.jupiter.api.Assertions.assertEquals;

import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

public class MoneyOrderTest {

private BankAccount account;

@BeforeEach
public void setup() {
// Arrange
account = new CheckingAccount("12345678", null, 999.0);
}

@Test
public void testMoneyOrderCreation() {
// Arrange
double expectedAccountAmmount = 499.00;

// Act
MoneyOrder moneyOrder = new MoneyOrder(account, 500.0);

// Assert
assertEquals(expectedAccountAmmount, account.balance);
}

@Test
public void testMoneyOrderGetAmmount() {
// Act
MoneyOrder moneyOrder = new MoneyOrder(account, 500.0);

// Arrange
String expectedMessage = "$" + 500.0 + " Taken from: " + account;

// Assert
assertEquals(expectedMessage, moneyOrder.getAmount());
}
}
Loading