Skip to content

Feat: Adds for lesson 17 method implementation/ new funtionality for … #547

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 13 commits into from
Closed
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
Expand Up @@ -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<CheckingAccount> findAccountsByCustomerId(UUID customerId, UUID businessId) {
return customerById.containsKey(customerId)
? customerById.get(customerId).getAccounts()
: Set.of();
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a check. */
public class Check {
Expand All @@ -21,6 +22,9 @@ public Check(String checkNumber, double amount, CheckingAccount account) {
if (amount < 0) {
throw new IllegalArgumentException("Check amount must be positive");
}
if (account instanceof SavingsAccount) {
throw new UnsupportedOperationException("Cannot issue checks from a savings account.");
}
this.checkNumber = checkNumber;
this.amount = amount;
this.account = account;
Expand All @@ -47,7 +51,7 @@ public void voidCheck() {
*/
public void depositFunds(CheckingAccount toAccount) {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
throw new CheckVoidedException("MoneyOrder is voided");
}
account.withdraw(amount);
toAccount.deposit(amount);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public class CheckingAccount {
private final Set<Customer> owners;
private final String accountNumber;
private double balance;
private boolean isActive;
public boolean isActive;

/**
* Creates a new checking account.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@
/** Represents a customer of the bank. */
public class Customer {

public static <T> boolean isBusiness(T t) {
throw new UnsupportedOperationException("Not supported yet.");
}

private final UUID id;
private final String name;
private final Set<CheckingAccount> accounts = new HashSet<>();
Expand All @@ -16,8 +20,9 @@ public class Customer {
*
* @param id The ID of the customer.
* @param name The name of the customer.
* @param b
*/
public Customer(UUID id, String name) {
public Customer(UUID id, String name, boolean b) {
this.id = id;
this.name = name;
}
Expand Down Expand Up @@ -58,6 +63,10 @@ public Set<CheckingAccount> getAccounts() {
return accounts;
}

public boolean isBusiness() {
return false;
}

@Override
public int hashCode() {
return id.hashCode();
Expand All @@ -75,4 +84,8 @@ public boolean equals(Object obj) {
public String toString() {
return "Customer{" + "id=" + id + ", name='" + name + '\'' + '}';
}

public static String getEmail() {
return Customer.getEmail();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package com.codedifferently.lesson17.bank.exceptions;

import java.util.ArrayList;
import java.util.List;

public class AuditLog {
private final List<String> logEntries = new ArrayList<>();
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package com.codedifferently.lesson17.bank.exceptions;

import com.codedifferently.lesson17.bank.CheckingAccount;
import com.codedifferently.lesson17.bank.Customer;
import java.util.Set;

public class BusinessAccount {
public class BusinessCheckingAccount extends CheckingAccount {
public BusinessCheckingAccount(String accountNumber, Set<Customer> owners, double balance) {
super(accountNumber, owners, balance);
if (!hasBusinessOwner(owners)) {
throw new IllegalArgumentException("requires at least one business owner.");
}
}

private boolean hasBusinessOwner(Set<Customer> owners) {
return owners.stream()
.anyMatch(
owner -> {
return ((Customer) owners).getEmail().contains("business");
});
}

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

public class IllegalArgumentException extends RuntimeException {
public IllegalArgumentException() {}

public IllegalArgumentException(String message) {
super(message);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
package com.codedifferently.lesson17.bank.exceptions;

import com.codedifferently.lesson17.bank.Check;

public class MoneyOrder extends Check {

public MoneyOrder(
String checkNumber,
double amount,
com.codedifferently.lesson17.bank.CheckingAccount account) {
super(checkNumber, amount, account);
if (amount < 0) {
throw new IllegalArgumentException("MoneyOrder amount must be positive");
}
account.withdraw(amount);
}

/**
* Deposits the check into an account.
*
* @param toAccount The account to deposit the check into.
*/
public void depositFunds(CheckingAccount toAccount) {
if (getIsVoided()) {
throw new UnsupportedOperationException("MoneyOrder is voided");
}
toAccount.withdraw(getAmount());
((CheckingAccount) getAccount()).withdraw(getAmount());
((CheckingAccount) toAccount).deposit(getAmount());
voidCheck();
}

public class CheckingAccount {
private String accountNumber;

public CheckingAccount(String accountNumber) {
this.accountNumber = accountNumber;
}

private void withdraw(double amount) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'withdraw'");
}

private void deposit(double amount) {
// TODO Auto-generated method stub
throw new UnsupportedOperationException("Unimplemented method 'deposit'");
}

public String getAccountNumber() {
return this.accountNumber;
}
}

@Override
public String toString() {
return "MoneyOrder{"
+ "checkNumber='"
+ getCheckNumber()
+ '\''
+ ", amount="
+ getAmount()
+ ", account="
+ ((CheckingAccount) getAccount()).getAccountNumber()
+ '}';
}

private double getAmount() {
throw new UnsupportedOperationException("Not supported yet.");
}

private Object getAccount() {
throw new UnsupportedOperationException("Not supported yet.");
}

private String getCheckNumber() {
throw new UnsupportedOperationException("Not supported yet.");
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package com.codedifferently.lesson17.bank.exceptions;

import com.codedifferently.lesson17.bank.CheckingAccount;
import com.codedifferently.lesson17.bank.Customer;
import java.util.Set;

public class SavingsAccount extends CheckingAccount {

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

// Define the NullCheck class that blocks a check from being withdrawn
public class NullCheck {

private boolean isVoided;
private String checkNumber;
private double amount;
private String account;

public void blockCheckWithdrawal(
double amount, String account, String checkNumber, boolean isVoided) {
this.isVoided = isVoided;
this.checkNumber = checkNumber;
this.amount = amount;
this.account = account;

// Example logic for blocking check withdrawal
if (isVoided) {
// Simulate a system that logs or handles the voided check
System.out.println("Check " + checkNumber + " for account " + account + " is voided.");
} else {
// Process the withdrawal logic (not implemented here)
System.out.println("Withdrawal blocked for check " + checkNumber);
}

if (this.amount > 0) {}
}

// Getters for properties
public boolean getisVoided() {
return isVoided;
}

public String getCheckNumber() {
return checkNumber;
}

public double getAmount() {
return amount;
}

public String getAccount() {
return account;
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@

Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,20 @@ class BankAtmTest {
private BankAtm classUnderTest;
private CheckingAccount account1;
private CheckingAccount account2;
private CheckingAccount account3;
private Customer customer1;
private Customer customer2;
private Customer customer3;

@BeforeEach
void setUp() {
classUnderTest = new BankAtm();
customer1 = new Customer(UUID.randomUUID(), "John Doe");
customer2 = new Customer(UUID.randomUUID(), "Jane Smith");
customer1 = new Customer(UUID.randomUUID(), "John Doe", false);
customer2 = new Customer(UUID.randomUUID(), "Jane Smith", false);
customer3 = new Customer(UUID.randomUUID(), "Jane Smith's Business", true);
account1 = new CheckingAccount("123456789", Set.of(customer1), 100.0);
account2 = new CheckingAccount("987654321", Set.of(customer1, customer2), 200.0);
account3 = new CheckingAccount("123456777", Set.of(customer2), 40000);
customer1.addAccount(account1);
customer1.addAccount(account2);
customer2.addAccount(account2);
Expand All @@ -35,22 +39,24 @@ void setUp() {
@Test
void testAddAccount() {
// Arrange
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson");
Customer customer3 = new Customer(UUID.randomUUID(), "Alice Johnson", false);
CheckingAccount account3 = new CheckingAccount("555555555", Set.of(customer3), 300.0);
customer3.addAccount(account3);

// Act
classUnderTest.addAccount(account3);

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

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

// Assert
assertThat(accounts).containsOnly(account1, account2);
Expand Down Expand Up @@ -86,7 +92,7 @@ void testDepositFunds_DoesntDepositCheckTwice() {

assertThatExceptionOfType(CheckVoidedException.class)
.isThrownBy(() -> classUnderTest.depositFunds("987654321", check))
.withMessage("Check is voided");
.withMessage("MoneyOrder is voided");
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ class CheckingAccountTest {
@BeforeEach
void setUp() {
owners = new HashSet<>();
owners.add(new Customer(UUID.randomUUID(), "John Doe"));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith"));
owners.add(new Customer(UUID.randomUUID(), "John Doe", false));
owners.add(new Customer(UUID.randomUUID(), "Jane Smith", false));
classUnderTest = new CheckingAccount("123456789", owners, 100.0);
}

Expand Down
Loading
Loading