Skip to content

Commit 2e32759

Browse files
authored
fix: updates homework assignments for lesson_16 and lesson_17 (#503)
* chore: removing dumb files Signed-off-by: Anthony D. Mays <[email protected]> * fix: updating homework assignments Signed-off-by: Anthony D. Mays <[email protected]> * chore: update git action --------- Signed-off-by: Anthony D. Mays <[email protected]>
1 parent ea1911e commit 2e32759

File tree

24 files changed

+768
-29
lines changed

24 files changed

+768
-29
lines changed

.github/workflows/check_lesson_17_java_pr.yaml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ on:
44
pull_request:
55
branches: [ "main" ]
66
paths:
7-
- "lesson_17/oop/**"
7+
- "lesson_17/bank/**"
88

99
jobs:
1010
build:
@@ -24,5 +24,5 @@ jobs:
2424
distribution: 'temurin'
2525

2626
- name: Build Lesson 17 with Java
27-
working-directory: ./lesson_17/oop
27+
working-directory: ./lesson_17/bank
2828
run: ./gradlew check
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
distributionBase=GRADLE_USER_HOME
22
distributionPath=wrapper/dists
3-
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
3+
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
44
networkTimeout=10000
55
zipStoreBase=GRADLE_USER_HOME
66
zipStorePath=wrapper/dists

lesson_16/objects/objects_app/build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
// Apply the application plugin to add support for building a CLI application in Java.
33
application
44
eclipse
5+
jacoco
56
id("com.diffplug.spotless") version "6.25.0"
67
id("org.springframework.boot") version "3.2.2"
78
id("com.adarshr.test-logger") version "4.0.0"
@@ -38,8 +39,29 @@ application {
3839
tasks.named<Test>("test") {
3940
// Use JUnit Platform for unit tests.
4041
useJUnitPlatform()
42+
finalizedBy(tasks.jacocoTestReport)
4143
}
4244

45+
tasks.jacocoTestReport {
46+
dependsOn(tasks.test)
47+
reports {
48+
xml.required = true
49+
}
50+
}
51+
52+
tasks.jacocoTestCoverageVerification {
53+
violationRules {
54+
rule {
55+
limit {
56+
minimum = "0.8".toBigDecimal()
57+
}
58+
}
59+
}
60+
}
61+
62+
tasks.check {
63+
dependsOn(tasks.jacocoTestCoverageVerification)
64+
}
4365

4466
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
4567

lesson_17/README.md

Lines changed: 14 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,34 +16,26 @@ Please review the following resources before lecture:
1616

1717
## Homework
1818

19-
- [ ] Complete [Library Management System](#library-management-system) assignment.
19+
- [ ] Complete [Applying SOLID principles](#applying-solid-principles-bank-atm) exercise.
2020

21-
## Library Management System
21+
## Applying SOLID Principles (Bank ATM)
2222

23-
For this assignment, you will build a library management system using object oriented principles. Pay attention to the requirements careful in order to implement your system successfully.
23+
Your task for this assignment is add enhancements to an ATM simulator. The [BankAtm][bankatm-file] is at the center of the model, allowing us to add one or more `CheckingAccount` instances and make withdrawals or deposits via cash or check. You will need to implement at least two of the following functional enhancements to the `BankAtm` class WITHOUT adding a new method. Note that you can update existing methods, however.
2424

2525
### Functional Requirements
2626

27-
* We want to define a book and keep track of its title, isbn, author(s), number of pages, and whether it is checked out or not.
28-
* We want define a patron with properties including their name and the books they have checked out.
29-
* We want to define a library that:
30-
* Allows us to add/remove a book to and from its collection of books.
31-
* Allows us to register new patrons
32-
* Allows us to check out books to patrons.
33-
* Allows us to return books from patrons.
27+
* We want to support a `SavingsAccount` that works just like the `CheckingAccount`, but doesn't allow you to write checks against the account.
28+
* We want the `BankAtm` class to support the concept of a `BusinessCheckingAccount`. A business account requires that at least one of the owning accounts is a business.
29+
* In addition to supporting checks and cash, we also want to support the concept of another monetary instrument called a `MoneyOrder`. Unlike a `Check`, a `MoneyOrder` withdraws funds from a source account immediately on creation for the purposes of this simulation..
30+
* For traceability, all of the transactions in the `BankAtm` class should logged. Create an `AuditLog` class that keeps a record of all debits and credits to any account and integrate it with the `BankAtm` class.
31+
* For the `depositFunds` method that accepts a cash amount, we'd like the ability to deposit funds in a variety of currencies. Add a parameter that accepts a currency type and a new object that encapsulates the currency converter logic for converting a cash amount to the account currency type.
3432

3533
### Technical Requirements
3634

37-
* All of your types must be created in a unique sub-package under the `com.codedifferently.lesson17` namespace.
38-
* Include tests for all of your classes and public methods.
39-
* Include appropriate `javadoc` for your classes and methods. In VS Code, you can usually start a comment with `/**` and press `Enter` to let the IDE automatically create some starter text for you.
35+
* You must integrate new features into the `BankAtm` without adding a new public method. Existing public methods may be modified without breaking existing functionality.
36+
* You must update the `BankAtm` tests and may modify or add other applicable tests.
37+
* Feel free to add the minimal number of classes, interfaces, or abstract classes needed to fulfill each requirement.
38+
* You must update existing javadocs and may add new documentation for new types and methods you introduce.
4039

41-
### Tips
42-
43-
* Read the functional requirements carefully, and take special care to identify *objects*, their *data*, and their *actions*.
44-
* It is highly recommended that you write your tests firsts, and then build what you need to make the tests work. This is essential to [Test-Driven Development][tdd-article].
45-
* How will you ensure that books are properly checked out using the library? What if someone tries to check out a book the library doesn't own (or vice-versa with returns)?
46-
* What are the public methods that you need to have to get the system working? Don't make anything public that you don't need to fulfill the requirements of the assignment. For instance, adding a getter method for a book's ISBN makes sense, but adding a method for returning the library's hours of operation is beyond the scope of this assignment.
47-
* How can you use collections to manage books and patrons? For instance, it would be wise to ensure that patrons can only be registered once based on some unique identifier.
48-
49-
[tdd-article]: https://semaphoreci.com/blog/test-driven-development
40+
[bank-folder]: ./bank/
41+
[bankatm-file]: ./bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java
File renamed without changes.
File renamed without changes.

lesson_17/oop/oop_app/build.gradle.kts renamed to lesson_17/bank/bank_app/build.gradle.kts

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ plugins {
22
// Apply the application plugin to add support for building a CLI application in Java.
33
application
44
eclipse
5+
jacoco
56
id("com.diffplug.spotless") version "6.25.0"
67
id("org.springframework.boot") version "3.2.2"
78
id("com.adarshr.test-logger") version "4.0.0"
@@ -38,8 +39,29 @@ application {
3839
tasks.named<Test>("test") {
3940
// Use JUnit Platform for unit tests.
4041
useJUnitPlatform()
42+
finalizedBy(tasks.jacocoTestReport)
4143
}
4244

45+
tasks.jacocoTestReport {
46+
dependsOn(tasks.test)
47+
reports {
48+
xml.required = true
49+
}
50+
}
51+
52+
tasks.jacocoTestCoverageVerification {
53+
violationRules {
54+
rule {
55+
limit {
56+
minimum = "0.8".toBigDecimal()
57+
}
58+
}
59+
}
60+
}
61+
62+
tasks.check {
63+
dependsOn(tasks.jacocoTestCoverageVerification)
64+
}
4365

4466
configure<com.diffplug.gradle.spotless.SpotlessExtension> {
4567

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
4+
import java.util.HashMap;
5+
import java.util.Map;
6+
import java.util.Set;
7+
import java.util.UUID;
8+
9+
/** Represents a bank ATM. */
10+
public class BankAtm {
11+
12+
private final Map<UUID, Customer> customerById = new HashMap<>();
13+
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();
14+
15+
/**
16+
* Adds a checking account to the bank.
17+
*
18+
* @param account The account to add.
19+
*/
20+
public void addAccount(CheckingAccount account) {
21+
accountByNumber.put(account.getAccountNumber(), account);
22+
account
23+
.getOwners()
24+
.forEach(
25+
owner -> {
26+
customerById.put(owner.getId(), owner);
27+
});
28+
}
29+
30+
/**
31+
* Finds all accounts owned by a customer.
32+
*
33+
* @param customerId The ID of the customer.
34+
* @return The unique set of accounts owned by the customer.
35+
*/
36+
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
37+
return customerById.containsKey(customerId)
38+
? customerById.get(customerId).getAccounts()
39+
: Set.of();
40+
}
41+
42+
/**
43+
* Deposits funds into an account.
44+
*
45+
* @param accountNumber The account number.
46+
* @param amount The amount to deposit.
47+
*/
48+
public void depositFunds(String accountNumber, double amount) {
49+
CheckingAccount account = getAccountOrThrow(accountNumber);
50+
account.deposit(amount);
51+
}
52+
53+
/**
54+
* Deposits funds into an account using a check.
55+
*
56+
* @param accountNumber The account number.
57+
* @param check The check to deposit.
58+
*/
59+
public void depositFunds(String accountNumber, Check check) {
60+
CheckingAccount account = getAccountOrThrow(accountNumber);
61+
check.depositFunds(account);
62+
}
63+
64+
/**
65+
* Withdraws funds from an account.
66+
*
67+
* @param accountNumber
68+
* @param amount
69+
*/
70+
public void withdrawFunds(String accountNumber, double amount) {
71+
CheckingAccount account = getAccountOrThrow(accountNumber);
72+
account.withdraw(amount);
73+
}
74+
75+
/**
76+
* Gets an account by its number or throws an exception if not found.
77+
*
78+
* @param accountNumber The account number.
79+
* @return The account.
80+
*/
81+
private CheckingAccount getAccountOrThrow(String accountNumber) {
82+
CheckingAccount account = accountByNumber.get(accountNumber);
83+
if (account == null || account.isClosed()) {
84+
throw new AccountNotFoundException("Account not found");
85+
}
86+
return account;
87+
}
88+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.codedifferently.lesson17.bank;
2+
3+
import com.codedifferently.lesson17.bank.exceptions.CheckVoidedException;
4+
5+
/** Represents a check. */
6+
public class Check {
7+
8+
private final String checkNumber;
9+
private final double amount;
10+
private final CheckingAccount account;
11+
private boolean isVoided = false;
12+
13+
/**
14+
* Creates a new check.
15+
*
16+
* @param checkNumber The check number.
17+
* @param amount The amount of the check.
18+
* @param account The account the check is drawn on.
19+
*/
20+
public Check(String checkNumber, double amount, CheckingAccount account) {
21+
if (amount < 0) {
22+
throw new IllegalArgumentException("Check amount must be positive");
23+
}
24+
this.checkNumber = checkNumber;
25+
this.amount = amount;
26+
this.account = account;
27+
}
28+
29+
/**
30+
* Gets the voided status of the check.
31+
*
32+
* @return True if the check is voided, and false otherwise.
33+
*/
34+
public boolean getIsVoided() {
35+
return isVoided;
36+
}
37+
38+
/** Voids the check. */
39+
public void voidCheck() {
40+
isVoided = true;
41+
}
42+
43+
/**
44+
* Deposits the check into an account.
45+
*
46+
* @param toAccount The account to deposit the check into.
47+
*/
48+
public void depositFunds(CheckingAccount toAccount) {
49+
if (isVoided) {
50+
throw new CheckVoidedException("Check is voided");
51+
}
52+
account.withdraw(amount);
53+
toAccount.deposit(amount);
54+
voidCheck();
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
return checkNumber.hashCode();
60+
}
61+
62+
@Override
63+
public boolean equals(Object obj) {
64+
if (obj instanceof Check other) {
65+
return checkNumber.equals(other.checkNumber);
66+
}
67+
return false;
68+
}
69+
70+
@Override
71+
public String toString() {
72+
return "Check{"
73+
+ "checkNumber='"
74+
+ checkNumber
75+
+ '\''
76+
+ ", amount="
77+
+ amount
78+
+ ", account="
79+
+ account.getAccountNumber()
80+
+ '}';
81+
}
82+
}

0 commit comments

Comments
 (0)