Skip to content

Commit ff9156d

Browse files
committed
chore: adds more git comments
1 parent 15802e9 commit ff9156d

File tree

7 files changed

+87
-0
lines changed

7 files changed

+87
-0
lines changed

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

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,20 @@
33
import com.codedifferently.lesson17.bank.exceptions.InsufficientFundsException;
44
import java.util.Set;
55

6+
/** Abstract base class for all types of accounts (Checking, Savings, etc.). */
67
public abstract class Account {
78
private final String accountNumber;
89
private final Set<Customer> owners;
910
private double balance;
1011
private boolean isActive;
1112

13+
/**
14+
* Creates a new account with initial balance and owners.
15+
*
16+
* @param accountNumber unique account identifier
17+
* @param owners set of customers owning this account
18+
* @param initialBalance starting balance
19+
*/
1220
public Account(String accountNumber, Set<Customer> owners, double initialBalance) {
1321
this.accountNumber = accountNumber;
1422
this.owners = owners;
@@ -32,23 +40,39 @@ public boolean isClosed() {
3240
return !isActive;
3341
}
3442

43+
/**
44+
* Deposits a positive amount into the account.
45+
*
46+
* @param amount amount to deposit
47+
*/
3548
public void deposit(double amount) {
3649
if (amount < 0) throw new IllegalArgumentException("Deposit must be positive");
3750
balance += amount;
3851
}
3952

53+
/**
54+
* Withdraws a positive amount from the account, ensuring sufficient funds.
55+
*
56+
* @param amount amount to withdraw
57+
*/
4058
public void withdraw(double amount) {
4159
if (amount < 0) throw new IllegalStateException("Withdrawal amount must be positive");
4260
if (amount > balance)
4361
throw new InsufficientFundsException("Account does not have enough funds for withdrawal");
4462
balance -= amount;
4563
}
4664

65+
/** Closes the account if the balance is zero. */
4766
public void closeAccount() {
4867
if (balance != 0) throw new IllegalStateException("Cannot close account with non-zero balance");
4968
isActive = false;
5069
}
5170

71+
/**
72+
* Determines if this account can write checks.
73+
*
74+
* @return true for CheckingAccount, false for SavingsAccount
75+
*/
5276
public abstract boolean canWriteChecks();
5377

5478
@Override

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ public Set<Account> findAccountsByCustomerId(UUID customerId) {
4848
public void depositFunds(String accountNumber, double amount) {
4949
Account account = getAccountOrThrow(accountNumber);
5050
account.deposit(amount);
51+
// Log deposit transaction
5152
auditLog.logTransaction(
5253
account.getOwners().iterator().next().getId(),
5354
"Deposited $" + amount + " to " + accountNumber);
@@ -61,12 +62,16 @@ public void depositFunds(String accountNumber, double amount) {
6162
*/
6263
public void depositFunds(String accountNumber, Check check) {
6364
Account account = getAccountOrThrow(accountNumber);
65+
// Ensure only CheckingAccounts can accept checks
66+
6467
if (!(account instanceof CheckingAccount)) {
6568
throw new IllegalArgumentException("Can only deposit checks into checking accounts");
6669
}
6770

6871
check.depositFunds((CheckingAccount) account);
6972

73+
// Log check deposit transaction
74+
7075
auditLog.logTransaction(
7176
account.getOwners().iterator().next().getId(),
7277
"Deposited check of " + check + " to account " + accountNumber);
@@ -81,6 +86,8 @@ public void depositFunds(String accountNumber, Check check) {
8186
public void withdrawFunds(String accountNumber, double amount) {
8287
Account account = getAccountOrThrow(accountNumber);
8388
account.withdraw(amount);
89+
// Log withdrawal transaction
90+
8491
auditLog.logTransaction(
8592
account.getOwners().iterator().next().getId(),
8693
"Withdrew $" + amount + " from " + accountNumber);
@@ -95,12 +102,16 @@ public void withdrawFunds(String accountNumber, double amount) {
95102
private Account getAccountOrThrow(String accountNumber) {
96103
Account account = accountByNumber.get(accountNumber);
97104
if (account == null || account.isClosed()) {
105+
// Throw exception if account not found or closed
106+
98107
throw new AccountNotFoundException("Account not found");
99108
}
100109
return account;
101110
}
102111

103112
public AuditLog getAuditLog() {
113+
// Getter for audit log, used in tests or external monitoring
114+
104115
return auditLog;
105116
}
106117
}

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,29 @@
22

33
import java.util.Set;
44

5+
/**
6+
* Represents a checking account, which is a type of Account. Unlike SavingsAccount, CheckingAccount
7+
* allows writing checks.
8+
*/
59
public class CheckingAccount extends Account {
610

11+
/**
12+
* Creates a new checking account with given account number, owners, and initial balance.
13+
*
14+
* @param accountNumber the unique account number
15+
* @param owners set of Customer objects who own the account
16+
* @param initialBalance starting balance
17+
*/
718
public CheckingAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
819
super(accountNumber, owners, initialBalance);
920
}
1021

22+
/**
23+
* Indicates whether this account can write checks. For checking accounts, this always returns
24+
* true.
25+
*
26+
* @return true because checks are allowed
27+
*/
1128
@Override
1229
public boolean canWriteChecks() {
1330
return true;

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,15 @@
44

55
public class SavingsAccount extends Account {
66

7+
/**
8+
* Represents a savings account that doesn't allow writing checks. Works like a CheckingAccount
9+
* otherwise.
10+
*/
711
public SavingsAccount(String accountNumber, Set<Customer> owners, double initialBalance) {
812
super(accountNumber, owners, initialBalance);
913
}
1014

15+
/** Savings accounts cannot write checks, so always return false. */
1116
@Override
1217
public boolean canWriteChecks() {
1318
return false;

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CheckingAccountTest.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,38 @@ void getOwners() {
3737

3838
@Test
3939
void deposit() {
40+
// Act
4041
classUnderTest.deposit(50.0);
42+
// Assert
4143
assertEquals(150.0, classUnderTest.getBalance());
4244
}
4345

4446
@Test
4547
void deposit_withNegativeAmount() {
48+
// Act & Assert
4649
assertThatExceptionOfType(IllegalArgumentException.class)
4750
.isThrownBy(() -> classUnderTest.deposit(-50.0));
4851
}
4952

5053
@Test
5154
void withdraw() {
55+
// Act
5256
classUnderTest.withdraw(50.0);
57+
// Assert
5358
assertEquals(50.0, classUnderTest.getBalance());
5459
}
5560

5661
@Test
5762
void withdraw_withNegativeAmount() {
63+
// Act & Assert
5864
assertThatExceptionOfType(IllegalStateException.class)
5965
.isThrownBy(() -> classUnderTest.withdraw(-50.0))
6066
.withMessage("Withdrawal amount must be positive");
6167
}
6268

6369
@Test
6470
void withdraw_withInsufficientBalance() {
71+
// Act & Assert
6572
assertThatExceptionOfType(InsufficientFundsException.class)
6673
.isThrownBy(() -> classUnderTest.withdraw(150.0))
6774
.withMessage("Account does not have enough funds for withdrawal");
@@ -74,33 +81,43 @@ void getBalance() {
7481

7582
@Test
7683
void closeAccount_withPositiveBalance() {
84+
// Act & Assert
7785
assertThatExceptionOfType(IllegalStateException.class)
7886
.isThrownBy(() -> classUnderTest.closeAccount());
7987
}
8088

8189
@Test
8290
void isClosed() {
91+
// Arrange
8392
assertFalse(classUnderTest.isClosed());
8493
classUnderTest.withdraw(100);
94+
// Act
8595
classUnderTest.closeAccount();
96+
// Assert
8697
assertTrue(classUnderTest.isClosed());
8798
}
8899

89100
@Test
90101
void equals() {
102+
// Arrange
91103
CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0);
104+
// Act & Assert
92105
assertEquals(classUnderTest, otherAccount);
93106
}
94107

95108
@Test
96109
void hashCodeTest() {
110+
// Arrange
97111
CheckingAccount otherAccount = new CheckingAccount("123456789", owners, 200.0);
112+
// Act & Assert
98113
assertEquals(classUnderTest.hashCode(), otherAccount.hashCode());
99114
}
100115

101116
@Test
102117
void toStringTest() {
118+
// Arrange
103119
String expected = "CheckingAccount{accountNumber='123456789', balance=100.0, isActive=true}";
120+
// Act & Assert
104121
assertEquals(expected, classUnderTest.toString());
105122
}
106123
}

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/CustomerTest.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,13 +30,17 @@ void testGetIdAndName() {
3030

3131
@Test
3232
void testGetAccounts() {
33+
// Act
3334
Set<Account> accounts = customer.getAccounts();
35+
// Assert
3436
assertThat(accounts).containsExactlyInAnyOrder(account1, account2);
3537
}
3638

3739
@Test
3840
void testEqualsAndHashCode() {
41+
// Arrange
3942
Customer sameCustomer = new Customer(customer.getId(), "Jane Doe");
43+
// Act & Assert
4044
assertThat(customer).isEqualTo(sameCustomer);
4145
assertThat(customer.hashCode()).isEqualTo(sameCustomer.hashCode());
4246
}

lesson_17/bank/bank_app/src/test/java/com/codedifferently/lesson17/bank/SavingsAccountTest.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,24 +35,30 @@ void getOwners() {
3535

3636
@Test
3737
void depositFundsIncreasesBalance() {
38+
// Act
3839
classUnderTest.deposit(50.0);
40+
// Assert
3941
assertThat(classUnderTest.getBalance()).isEqualTo(150.0);
4042
}
4143

4244
@Test
4345
void depositNegativeAmountThrowsException() {
46+
// Act & Assert
4447
assertThatExceptionOfType(IllegalArgumentException.class)
4548
.isThrownBy(() -> classUnderTest.deposit(-50.0));
4649
}
4750

4851
@Test
4952
void withdrawFundsDecreasesBalance() {
53+
// Act
5054
classUnderTest.withdraw(50.0);
55+
// Assert
5156
assertThat(classUnderTest.getBalance()).isEqualTo(50.0);
5257
}
5358

5459
@Test
5560
void withdrawTooMuchThrowsException() {
61+
// Act & Assert
5662
assertThatExceptionOfType(InsufficientFundsException.class)
5763
.isThrownBy(() -> classUnderTest.withdraw(150.0));
5864
}
@@ -69,13 +75,16 @@ void isClosedInitiallyFalse() {
6975

7076
@Test
7177
void closeAccountWithZeroBalanceWorks() {
78+
// Act
7279
classUnderTest.withdraw(100.0);
7380
classUnderTest.closeAccount();
81+
// Assert
7482
assertThat(classUnderTest.isClosed()).isTrue();
7583
}
7684

7785
@Test
7886
void closingAccountWithPositiveBalanceThrowsException() {
87+
// Act & Assert
7988
assertThatExceptionOfType(IllegalStateException.class)
8089
.isThrownBy(() -> classUnderTest.closeAccount());
8190
}

0 commit comments

Comments
 (0)