Skip to content

fix: updates homework assignments for lesson_16 and lesson_17 #503

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

Merged
merged 3 commits into from
Oct 30, 2024
Merged
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
4 changes: 2 additions & 2 deletions .github/workflows/check_lesson_17_java_pr.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ on:
pull_request:
branches: [ "main" ]
paths:
- "lesson_17/oop/**"
- "lesson_17/bank/**"

jobs:
build:
Expand All @@ -24,5 +24,5 @@ jobs:
distribution: 'temurin'

- name: Build Lesson 17 with Java
working-directory: ./lesson_17/oop
working-directory: ./lesson_17/bank
run: ./gradlew check
2 changes: 1 addition & 1 deletion lesson_16/objects/gradle/wrapper/gradle-wrapper.properties
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
distributionBase=GRADLE_USER_HOME
distributionPath=wrapper/dists
distributionUrl=https\://services.gradle.org/distributions/gradle-8.0.2-bin.zip
distributionUrl=https\://services.gradle.org/distributions/gradle-8.10.2-bin.zip
networkTimeout=10000
zipStoreBase=GRADLE_USER_HOME
zipStorePath=wrapper/dists
22 changes: 22 additions & 0 deletions lesson_16/objects/objects_app/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
eclipse
jacoco
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.2.2"
id("com.adarshr.test-logger") version "4.0.0"
Expand Down Expand Up @@ -38,8 +39,29 @@ application {
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
xml.required = true
}
}

tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}

tasks.check {
dependsOn(tasks.jacocoTestCoverageVerification)
}

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

Expand Down
36 changes: 14 additions & 22 deletions lesson_17/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,34 +16,26 @@ Please review the following resources before lecture:

## Homework

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

## Library Management System
## Applying SOLID Principles (Bank ATM)

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.
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.

### Functional Requirements

* 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.
* We want define a patron with properties including their name and the books they have checked out.
* We want to define a library that:
* Allows us to add/remove a book to and from its collection of books.
* Allows us to register new patrons
* Allows us to check out books to patrons.
* Allows us to return books from patrons.
* We want to support a `SavingsAccount` that works just like the `CheckingAccount`, but doesn't allow you to write checks against the account.
* 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.
* 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..
* 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.
* 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.

### Technical Requirements

* All of your types must be created in a unique sub-package under the `com.codedifferently.lesson17` namespace.
* Include tests for all of your classes and public methods.
* 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.
* You must integrate new features into the `BankAtm` without adding a new public method. Existing public methods may be modified without breaking existing functionality.
* You must update the `BankAtm` tests and may modify or add other applicable tests.
* Feel free to add the minimal number of classes, interfaces, or abstract classes needed to fulfill each requirement.
* You must update existing javadocs and may add new documentation for new types and methods you introduce.

### Tips

* Read the functional requirements carefully, and take special care to identify *objects*, their *data*, and their *actions*.
* 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].
* 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)?
* 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.
* 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.

[tdd-article]: https://semaphoreci.com/blog/test-driven-development
[bank-folder]: ./bank/
[bankatm-file]: ./bank/bank_app/src/main/java/com/codedifferently/lesson17/bank/BankAtm.java
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ plugins {
// Apply the application plugin to add support for building a CLI application in Java.
application
eclipse
jacoco
id("com.diffplug.spotless") version "6.25.0"
id("org.springframework.boot") version "3.2.2"
id("com.adarshr.test-logger") version "4.0.0"
Expand Down Expand Up @@ -38,8 +39,29 @@ application {
tasks.named<Test>("test") {
// Use JUnit Platform for unit tests.
useJUnitPlatform()
finalizedBy(tasks.jacocoTestReport)
}

tasks.jacocoTestReport {
dependsOn(tasks.test)
reports {
xml.required = true
}
}

tasks.jacocoTestCoverageVerification {
violationRules {
rule {
limit {
minimum = "0.8".toBigDecimal()
}
}
}
}

tasks.check {
dependsOn(tasks.jacocoTestCoverageVerification)
}

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

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

import com.codedifferently.lesson17.bank.exceptions.AccountNotFoundException;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
import java.util.UUID;

/** Represents a bank ATM. */
public class BankAtm {

private final Map<UUID, Customer> customerById = new HashMap<>();
private final Map<String, CheckingAccount> accountByNumber = new HashMap<>();

/**
* Adds a checking account to the bank.
*
* @param account The account to add.
*/
public void addAccount(CheckingAccount account) {
accountByNumber.put(account.getAccountNumber(), account);
account
.getOwners()
.forEach(
owner -> {
customerById.put(owner.getId(), owner);
});
}

/**
* Finds all accounts owned by a customer.
*
* @param customerId The ID of the customer.
* @return The unique set of accounts owned by the customer.
*/
public Set<CheckingAccount> findAccountsByCustomerId(UUID customerId) {
return customerById.containsKey(customerId)
? customerById.get(customerId).getAccounts()
: Set.of();
}

/**
* Deposits funds into an account.
*
* @param accountNumber The account number.
* @param amount The amount to deposit.
*/
public void depositFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
account.deposit(amount);
}

/**
* Deposits funds into an account using a check.
*
* @param accountNumber The account number.
* @param check The check to deposit.
*/
public void depositFunds(String accountNumber, Check check) {
CheckingAccount account = getAccountOrThrow(accountNumber);
check.depositFunds(account);
}

/**
* Withdraws funds from an account.
*
* @param accountNumber
* @param amount
*/
public void withdrawFunds(String accountNumber, double amount) {
CheckingAccount account = getAccountOrThrow(accountNumber);
account.withdraw(amount);
}

/**
* Gets an account by its number or throws an exception if not found.
*
* @param accountNumber The account number.
* @return The account.
*/
private CheckingAccount getAccountOrThrow(String accountNumber) {
CheckingAccount account = accountByNumber.get(accountNumber);
if (account == null || account.isClosed()) {
throw new AccountNotFoundException("Account not found");
}
return account;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
package com.codedifferently.lesson17.bank;

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

/** Represents a check. */
public class Check {

private final String checkNumber;
private final double amount;
private final CheckingAccount account;
private boolean isVoided = false;

/**
* Creates a new check.
*
* @param checkNumber The check number.
* @param amount The amount of the check.
* @param account The account the check is drawn on.
*/
public Check(String checkNumber, double amount, CheckingAccount account) {
if (amount < 0) {
throw new IllegalArgumentException("Check amount must be positive");
}
this.checkNumber = checkNumber;
this.amount = amount;
this.account = account;
}

/**
* Gets the voided status of the check.
*
* @return True if the check is voided, and false otherwise.
*/
public boolean getIsVoided() {
return isVoided;
}

/** Voids the check. */
public void voidCheck() {
isVoided = true;
}

/**
* Deposits the check into an account.
*
* @param toAccount The account to deposit the check into.
*/
public void depositFunds(CheckingAccount toAccount) {
if (isVoided) {
throw new CheckVoidedException("Check is voided");
}
account.withdraw(amount);
toAccount.deposit(amount);
voidCheck();
}

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

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

@Override
public String toString() {
return "Check{"
+ "checkNumber='"
+ checkNumber
+ '\''
+ ", amount="
+ amount
+ ", account="
+ account.getAccountNumber()
+ '}';
}
}
Loading
Loading