Skip to content

Commit 0c9187d

Browse files
Removed 16 files from 17 PR
1 parent 319a77c commit 0c9187d

File tree

12 files changed

+1165
-8
lines changed

12 files changed

+1165
-8
lines changed
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
import java.util.UUID;
5+
6+
public interface AccountOwner {
7+
8+
UUID getId();
9+
10+
String getName();
11+
12+
void addAccount(CheckingAccount account);
13+
14+
void addSavingsAccount(SavingAccount account);
15+
16+
Set<CheckingAccount> getAccounts();
17+
18+
Set<SavingAccount> getSavingsAccounts();
19+
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
/** Represents a bank ATM. */
1010
public class BankAtm {
1111

12-
private final Map<UUID, Customer> customerById = new HashMap<>();
12+
private final Map<UUID, AccountOwner> customerById = new HashMap<>();
1313
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
1414

1515
/**
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.HashSet;
4+
import java.util.Set;
5+
import java.util.UUID;
6+
7+
public class Business implements AccountOwner {
8+
9+
private final UUID id;
10+
private final String businessName;
11+
private final String taxId;
12+
private final Set<CheckingAccount> accounts = new HashSet<>();
13+
private final Set<SavingAccount> savingsAccounts = new HashSet<>();
14+
15+
public Business(UUID id, String businessName, String taxId) {
16+
this.id = id;
17+
this.businessName = businessName;
18+
this.taxId = taxId;
19+
}
20+
21+
@Override
22+
public UUID getId() {
23+
return id;
24+
}
25+
26+
@Override
27+
public String getName() {
28+
return businessName;
29+
}
30+
31+
public String getBusinessName() {
32+
return businessName;
33+
}
34+
35+
public String getTaxId() {
36+
return taxId;
37+
}
38+
39+
@Override
40+
public void addAccount(CheckingAccount account) {
41+
accounts.add(account);
42+
}
43+
44+
@Override
45+
public void addSavingsAccount(SavingAccount account) {
46+
savingsAccounts.add(account);
47+
}
48+
49+
@Override
50+
public Set<CheckingAccount> getAccounts() {
51+
return accounts;
52+
}
53+
54+
@Override
55+
public Set<SavingAccount> getSavingsAccounts() {
56+
return savingsAccounts;
57+
}
58+
59+
@Override
60+
public int hashCode() {
61+
return id.hashCode();
62+
}
63+
64+
@Override
65+
public boolean equals(Object obj) {
66+
if (obj instanceof Business other) {
67+
return id.equals(other.id);
68+
}
69+
return false;
70+
}
71+
72+
@Override
73+
public String toString() {
74+
return "Business{"
75+
+ "id="
76+
+ id
77+
+ ", businessName='"
78+
+ businessName
79+
+ '\''
80+
+ ", taxId='"
81+
+ taxId
82+
+ '\''
83+
+ '}';
84+
}
85+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import java.util.Set;
4+
5+
public class BusinessCheckingAccount extends CheckingAccount {
6+
7+
public BusinessCheckingAccount(
8+
String accountNumber, Set<AccountOwner> owners, double initialBalance) {
9+
super(accountNumber, owners, initialBalance);
10+
validateBusinessOwnership(owners);
11+
}
12+
13+
private void validateBusinessOwnership(Set<AccountOwner> owners) {
14+
boolean hasBusiness = owners.stream().anyMatch(owner -> owner instanceof Business);
15+
16+
if (!hasBusiness) {
17+
throw new IllegalArgumentException(
18+
"Business checking account must have at least one business owner");
19+
}
20+
}
21+
22+
@Override
23+
public String toString() {
24+
return "BusinessCheckingAccount{"
25+
+ "accountNumber='"
26+
+ getAccountNumber()
27+
+ '\''
28+
+ ", balance="
29+
+ getBalance()
30+
+ ", isActive="
31+
+ !isClosed()
32+
+ '}';
33+
}
34+
}

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/CheckingAccount.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
/** Represents a checking account. */
77
public class CheckingAccount {
88

9-
private final Set<Customer> owners;
9+
private final Set<AccountOwner> owners;
1010
private final String accountNumber;
1111
private double balance;
1212
private boolean isActive;
@@ -18,7 +18,7 @@ public class CheckingAccount {
1818
* @param owners The owners of the account.
1919
* @param initialBalance The initial balance of the account.
2020
*/
21-
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
21+
public CheckingAccount(String accountNumber, Set<AccountOwner> owners, double initialBalance) {
2222
this.accountNumber = accountNumber;
2323
this.owners = owners;
2424
this.balance = initialBalance;
@@ -39,7 +39,7 @@ public String getAccountNumber() {
3939
*
4040
* @return The owners of the account.
4141
*/
42-
public Set<Customer> getOwners() {
42+
public Set<AccountOwner> getOwners() {
4343
return owners;
4444
}
4545

lesson_17/bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/Customer.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,12 @@
55
import java.util.UUID;
66

77
/** Represents a customer of the bank. */
8-
public class Customer {
8+
public class Customer implements AccountOwner {
99

1010
private final UUID id;
1111
private final String name;
1212
private final Set<CheckingAccount> accounts = new HashSet<>();
13+
private final Set<SavingAccount> savingsAccounts = new HashSet<>();
1314

1415
/**
1516
* Creates a new customer.
@@ -27,6 +28,7 @@ public Customer(UUID id, String name) {
2728
*
2829
* @return The ID of the customer.
2930
*/
31+
@Override
3032
public UUID getId() {
3133
return id;
3234
}
@@ -36,6 +38,7 @@ public UUID getId() {
3638
*
3739
* @return The name of the customer.
3840
*/
41+
@Override
3942
public String getName() {
4043
return name;
4144
}
@@ -45,19 +48,41 @@ public String getName() {
4548
*
4649
* @param account The account to add.
4750
*/
51+
@Override
4852
public void addAccount(CheckingAccount account) {
4953
accounts.add(account);
5054
}
5155

5256
/**
53-
* Gets the accounts owned by the customer.
57+
* Adds a savings account to the customer.
58+
*
59+
* @param account The savings account to add.
60+
*/
61+
@Override
62+
public void addSavingsAccount(SavingAccount account) {
63+
savingsAccounts.add(account);
64+
}
65+
66+
/**
67+
* Gets the checking accounts owned by the customer.
5468
*
55-
* @return The unique set of accounts owned by the customer.
69+
* @return The unique set of checking accounts owned by the customer.
5670
*/
71+
@Override
5772
public Set<CheckingAccount> getAccounts() {
5873
return accounts;
5974
}
6075

76+
/**
77+
* Gets the savings accounts owned by the customer.
78+
*
79+
* @return The unique set of savings accounts owned by the customer.
80+
*/
81+
@Override
82+
public Set<SavingAccount> getSavingsAccounts() {
83+
return savingsAccounts;
84+
}
85+
6186
@Override
6287
public int hashCode() {
6388
return id.hashCode();
Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
4+
import java.util.Set;
5+
6+
public class SavingAccount {
7+
8+
private final Set<AccountOwner> owners;
9+
private final String accountNumber;
10+
private double balance;
11+
private boolean isActive;
12+
13+
public SavingAccount(String accountNumber, Set<AccountOwner> owners, double initialBalance) {
14+
this.accountNumber = accountNumber;
15+
this.owners = owners;
16+
this.balance = initialBalance;
17+
isActive = true;
18+
}
19+
20+
public String getAccountNumber() {
21+
return accountNumber;
22+
}
23+
24+
public Set<AccountOwner> getOwners() {
25+
return owners;
26+
}
27+
28+
public void deposit(double amount) throws IllegalStateException {
29+
if (isClosed()) {
30+
throw new IllegalStateException("Cannot deposit to a closed account");
31+
}
32+
if (amount <= 0) {
33+
throw new IllegalArgumentException("Deposit amount must be positive");
34+
}
35+
balance += amount;
36+
}
37+
38+
public void withdraw(double amount) throws InsufficientFundsException {
39+
if (isClosed()) {
40+
throw new IllegalStateException("Cannot withdraw from a closed account");
41+
}
42+
if (amount <= 0) {
43+
throw new IllegalArgumentException("Withdrawal amount must be positive");
44+
}
45+
if (balance < amount) {
46+
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
47+
}
48+
balance -= amount;
49+
}
50+
51+
public double getBalance() {
52+
return balance;
53+
}
54+
55+
public void closeAccount() throws IllegalStateException {
56+
if (balance > 0) {
57+
throw new IllegalStateException("Cannot close account with a positive balance");
58+
}
59+
isActive = false;
60+
}
61+
62+
public boolean isClosed() {
63+
return !isActive;
64+
}
65+
66+
@Override
67+
public int hashCode() {
68+
return accountNumber.hashCode();
69+
}
70+
71+
@Override
72+
public boolean equals(Object obj) {
73+
if (obj instanceof SavingAccount other) {
74+
return accountNumber.equals(other.accountNumber);
75+
}
76+
return false;
77+
}
78+
79+
@Override
80+
public String toString() {
81+
return "SavingsAccount{"
82+
+ "accountNumber='"
83+
+ accountNumber
84+
+ '\''
85+
+ ", balance="
86+
+ balance
87+
+ ", isActive="
88+
+ isActive
89+
+ '}';
90+
}
91+
}

0 commit comments

Comments
 (0)