From 147bdfb436f28c3fce554cc6c99220cb660c15de Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sat, 1 Aug 2026 17:30:01 +0100 Subject: [PATCH 1/5] add gitignore --- .gitignore | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 .gitignore diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..4ce2af9e --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +.idea/ +out/ +target/ \ No newline at end of file From 80611a58b10963e9340e9699eddcfe6e48c03f23 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sat, 1 Aug 2026 23:01:06 +0100 Subject: [PATCH 2/5] set up project config --- backlog/pom.xml | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) create mode 100644 backlog/pom.xml diff --git a/backlog/pom.xml b/backlog/pom.xml new file mode 100644 index 00000000..c1e90003 --- /dev/null +++ b/backlog/pom.xml @@ -0,0 +1,17 @@ + + + 4.0.0 + + com.backlog + backlog + 1.0-SNAPSHOT + + + 17 + 17 + UTF-8 + + + \ No newline at end of file From c9af7e9b0b614fb1bf68547d1490e1d671e371a2 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sat, 1 Aug 2026 23:02:32 +0100 Subject: [PATCH 3/5] initialize Rental generator skeletopn --- backlog/src/main/java/Contract.java | 66 +++++++++++++++++++ .../java/OneYearContractRentalGenerator.java | 22 +++++++ backlog/src/main/java/Rental.java | 15 +++++ backlog/src/main/java/RentalCalculator.java | 6 ++ backlog/src/main/java/RentalSummary.java | 26 ++++++++ .../src/main/java/RentalSummaryService.java | 28 ++++++++ .../ThreeYearContractRentalGenerator.java | 20 ++++++ 7 files changed, 183 insertions(+) create mode 100644 backlog/src/main/java/Contract.java create mode 100644 backlog/src/main/java/OneYearContractRentalGenerator.java create mode 100644 backlog/src/main/java/Rental.java create mode 100644 backlog/src/main/java/RentalCalculator.java create mode 100644 backlog/src/main/java/RentalSummary.java create mode 100644 backlog/src/main/java/RentalSummaryService.java create mode 100644 backlog/src/main/java/ThreeYearContractRentalGenerator.java diff --git a/backlog/src/main/java/Contract.java b/backlog/src/main/java/Contract.java new file mode 100644 index 00000000..f27c6133 --- /dev/null +++ b/backlog/src/main/java/Contract.java @@ -0,0 +1,66 @@ +import java.security.InvalidParameterException; +import java.time.LocalDate; + +public class Contract { + String customerName; + Integer customerAge; + LocalDate startDate; + double carPrice; + Integer contractLengthYears; + + public Contract(String customerName, Integer customerAge, LocalDate startDate, Integer contractLengthYears, double carPrice) { + if (!validContractLength(contractLengthYears)) { + throw new InvalidParameterException("Contract must be 1 or 3 years"); + } + this.customerName = customerName; + this.customerAge = customerAge; + this.startDate = startDate; + this.carPrice = carPrice; + this.contractLengthYears = contractLengthYears; + } + + public boolean validContractLength(int contractLengthYears) { + return contractLengthYears == 1 || contractLengthYears == 3; + + } + + public String getCustomerName() { + return customerName; + } + + public void setCustomerName(String customerName) { + this.customerName = customerName; + } + + public Integer getCustomerAge() { + return customerAge; + } + + public void setCustomerAge(Integer customerAge) { + this.customerAge = customerAge; + } + + public LocalDate getStartDate() { + return startDate; + } + + public void setStartDate(LocalDate startDate) { + this.startDate = startDate; + } + + public double getCarPrice() { + return carPrice; + } + + public void setCarPrice(double carPrice) { + this.carPrice = carPrice; + } + + public Integer getContractLengthYears() { + return contractLengthYears; + } + + public void setContractLengthYears(Integer contractLengthYears) { + this.contractLengthYears = contractLengthYears; + } +} diff --git a/backlog/src/main/java/OneYearContractRentalGenerator.java b/backlog/src/main/java/OneYearContractRentalGenerator.java new file mode 100644 index 00000000..098eddf1 --- /dev/null +++ b/backlog/src/main/java/OneYearContractRentalGenerator.java @@ -0,0 +1,22 @@ +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class OneYearContractRentalGenerator implements RentalCalculator { + @Override + public List generateRentals(Contract contract) { + + List rentals = new ArrayList<>(12); + + double capitalAmount = contract.getCarPrice() / 12; + double interestAmount = capitalAmount / 100 * 2; + for (int month = 0; month < rentals.size(); month++) { + LocalDate dueDate = contract.getStartDate().plusMonths(1); + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, false)); + } + return rentals; + } +} +// rentals.forEach((rental -> { +// rental = new Rental(contract.ge) +// })); \ No newline at end of file diff --git a/backlog/src/main/java/Rental.java b/backlog/src/main/java/Rental.java new file mode 100644 index 00000000..4307cb13 --- /dev/null +++ b/backlog/src/main/java/Rental.java @@ -0,0 +1,15 @@ +import java.time.LocalDate; + +public class Rental { + LocalDate dueDate; + double capitalAmount; + double interestAmount; + boolean paid; + + public Rental(LocalDate dueDate, double capitalAmount, double interestAmount, boolean paid) { + this.dueDate = dueDate; + this.capitalAmount = capitalAmount; + this.interestAmount = interestAmount; + this.paid = paid; + } +} diff --git a/backlog/src/main/java/RentalCalculator.java b/backlog/src/main/java/RentalCalculator.java new file mode 100644 index 00000000..df19bc03 --- /dev/null +++ b/backlog/src/main/java/RentalCalculator.java @@ -0,0 +1,6 @@ +import java.util.List; + +public interface RentalCalculator { + List generateRentals(Contract contract); + +} diff --git a/backlog/src/main/java/RentalSummary.java b/backlog/src/main/java/RentalSummary.java new file mode 100644 index 00000000..46ac3b18 --- /dev/null +++ b/backlog/src/main/java/RentalSummary.java @@ -0,0 +1,26 @@ +import java.util.List; + +public class RentalSummary { + List rentals; + Contract contract; + String customerName; + String customerAge; + String startDate; + String endDate; + + public RentalSummary(Contract contract) { + this.contract = contract; + } + +} + +//The system should log the rental summary to the console, printed in a human-readable format. +//The summary information should contain: +//The customer’s name and age +//The dates of the contract i.e. start and end date +//All rentals, sorted by due date +//The next due rental +//The total amount of capital and interest on the contract +//The number of remaining outstanding rentals +//If the contract is complete, i.e. the current date is after the final rental due due, then no summary should be printed. +// Instead the console should print a message to say the contract is completed. \ No newline at end of file diff --git a/backlog/src/main/java/RentalSummaryService.java b/backlog/src/main/java/RentalSummaryService.java new file mode 100644 index 00000000..69f00a03 --- /dev/null +++ b/backlog/src/main/java/RentalSummaryService.java @@ -0,0 +1,28 @@ +import java.time.LocalDate; +import java.util.Optional; + +public class RentalSummaryService { + public static void main(String[] args) { + + Contract oneYearContract = new Contract("John Smith", 25, LocalDate.of(2025, 8, 12), 1, 10000); + Contract twoYearContract = new Contract("Michael Jones", 56, LocalDate.of(2025, 12, 12), 2, 20000); + Contract threeYearContract = new Contract("Jane Doe", 77, LocalDate.of(2024, 4, 1), 3, 50000); + Contract completedContract = new Contract("Rosie Parker", 47, LocalDate.of(2021, 7, 10), 3, 40000); + + RentalSummaryService rentalSummaryService = new RentalSummaryService(); + rentalSummaryService.printRentalSummary(oneYearContract); + rentalSummaryService.printRentalSummary(twoYearContract); // Should throw a suitable error + rentalSummaryService.printRentalSummary(threeYearContract); + rentalSummaryService.printRentalSummary(completedContract); // Should print a message to say the contract is complete + } + + private Optional generateRentalSummary(Contract contract) { + return Optional.of(new RentalSummary(contract)); + } + + void printRentalSummary(Contract contract) { + RentalSummaryService rentalSummaryService = new RentalSummaryService(); + System.out.println(rentalSummaryService.generateRentalSummary(contract)); + } + +} diff --git a/backlog/src/main/java/ThreeYearContractRentalGenerator.java b/backlog/src/main/java/ThreeYearContractRentalGenerator.java new file mode 100644 index 00000000..d10f052e --- /dev/null +++ b/backlog/src/main/java/ThreeYearContractRentalGenerator.java @@ -0,0 +1,20 @@ +import java.time.LocalDate; +import java.util.ArrayList; +import java.util.List; + +public class ThreeYearContractRentalGenerator implements RentalCalculator { + + @Override + public List generateRentals(Contract contract) { + + List rentals = new ArrayList<>(36); + + double capitalAmount = contract.getCarPrice() / 36; + double interestAmount = capitalAmount / 100 * 3; + for (int month = 0; month < rentals.size(); month++) { + LocalDate dueDate = contract.getStartDate().plusMonths(1); + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, false)); + } + return rentals; + } +} From a6e8f56d76c207407884a3892860e500e8f63b96 Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 2 Aug 2026 12:20:51 +0100 Subject: [PATCH 4/5] complete task --- backlog/src/main/java/Contract.java | 27 -------- .../java/OneYearContractRentalGenerator.java | 9 +-- backlog/src/main/java/Rental.java | 12 ++++ backlog/src/main/java/RentalSummary.java | 66 ++++++++++++++----- .../src/main/java/RentalSummaryService.java | 27 ++++++-- .../ThreeYearContractRentalGenerator.java | 10 +-- 6 files changed, 92 insertions(+), 59 deletions(-) diff --git a/backlog/src/main/java/Contract.java b/backlog/src/main/java/Contract.java index f27c6133..3eaf8f20 100644 --- a/backlog/src/main/java/Contract.java +++ b/backlog/src/main/java/Contract.java @@ -1,4 +1,3 @@ -import java.security.InvalidParameterException; import java.time.LocalDate; public class Contract { @@ -9,9 +8,6 @@ public class Contract { Integer contractLengthYears; public Contract(String customerName, Integer customerAge, LocalDate startDate, Integer contractLengthYears, double carPrice) { - if (!validContractLength(contractLengthYears)) { - throw new InvalidParameterException("Contract must be 1 or 3 years"); - } this.customerName = customerName; this.customerAge = customerAge; this.startDate = startDate; @@ -19,48 +15,25 @@ public Contract(String customerName, Integer customerAge, LocalDate startDate, I this.contractLengthYears = contractLengthYears; } - public boolean validContractLength(int contractLengthYears) { - return contractLengthYears == 1 || contractLengthYears == 3; - - } public String getCustomerName() { return customerName; } - public void setCustomerName(String customerName) { - this.customerName = customerName; - } - public Integer getCustomerAge() { return customerAge; } - public void setCustomerAge(Integer customerAge) { - this.customerAge = customerAge; - } - public LocalDate getStartDate() { return startDate; } - public void setStartDate(LocalDate startDate) { - this.startDate = startDate; - } public double getCarPrice() { return carPrice; } - public void setCarPrice(double carPrice) { - this.carPrice = carPrice; - } - public Integer getContractLengthYears() { return contractLengthYears; } - - public void setContractLengthYears(Integer contractLengthYears) { - this.contractLengthYears = contractLengthYears; - } } diff --git a/backlog/src/main/java/OneYearContractRentalGenerator.java b/backlog/src/main/java/OneYearContractRentalGenerator.java index 098eddf1..fadd338f 100644 --- a/backlog/src/main/java/OneYearContractRentalGenerator.java +++ b/backlog/src/main/java/OneYearContractRentalGenerator.java @@ -10,13 +10,10 @@ public List generateRentals(Contract contract) { double capitalAmount = contract.getCarPrice() / 12; double interestAmount = capitalAmount / 100 * 2; - for (int month = 0; month < rentals.size(); month++) { - LocalDate dueDate = contract.getStartDate().plusMonths(1); - rentals.add(new Rental(dueDate, capitalAmount, interestAmount, false)); + for (int month = 1; month <= 12; month++) { + LocalDate dueDate = contract.getStartDate().plusMonths(month); + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, dueDate.isBefore(LocalDate.now().plusDays(1)))); } return rentals; } } -// rentals.forEach((rental -> { -// rental = new Rental(contract.ge) -// })); \ No newline at end of file diff --git a/backlog/src/main/java/Rental.java b/backlog/src/main/java/Rental.java index 4307cb13..b51f2753 100644 --- a/backlog/src/main/java/Rental.java +++ b/backlog/src/main/java/Rental.java @@ -12,4 +12,16 @@ public Rental(LocalDate dueDate, double capitalAmount, double interestAmount, bo this.interestAmount = interestAmount; this.paid = paid; } + + public boolean isPaid() { + return paid; + } + + @Override + public String toString() { + return String.format("Due date: %1$tA, %1td %1$tB %1$tY %n" + + "Capital amount: %f%n" + + "Interest amount: %f%n" + + "Paid: %B%n", dueDate, capitalAmount, interestAmount, paid); + } } diff --git a/backlog/src/main/java/RentalSummary.java b/backlog/src/main/java/RentalSummary.java index 46ac3b18..cce45c97 100644 --- a/backlog/src/main/java/RentalSummary.java +++ b/backlog/src/main/java/RentalSummary.java @@ -1,26 +1,56 @@ +import java.security.InvalidParameterException; +import java.time.LocalDate; import java.util.List; +import java.util.Optional; public class RentalSummary { List rentals; - Contract contract; - String customerName; - String customerAge; - String startDate; - String endDate; + LocalDate endDate; + Optional nextRental; + double totalCapital; + double totalInterest; + long numberOfRemainingRentals; + public RentalSummary(Contract contract) { - this.contract = contract; + this.endDate = contract.startDate.plusYears(contract.getContractLengthYears()); + if (contract.getContractLengthYears() == 1) { + OneYearContractRentalGenerator oneYearContractRentalGenerator = new OneYearContractRentalGenerator(); + this.rentals = oneYearContractRentalGenerator.generateRentals(contract); + } else if (contract.getContractLengthYears() == 3) { + ThreeYearContractRentalGenerator threeYearContractRentalGenerator = new ThreeYearContractRentalGenerator(); + this.rentals = threeYearContractRentalGenerator.generateRentals(contract); + } else { + throw new InvalidParameterException("Contract length isn't 1 or 3"); + } + this.nextRental = rentals.stream() + .filter(rental -> !rental.isPaid()).findFirst(); + this.totalCapital = contract.getCarPrice(); + this.totalInterest = contract.getCarPrice() / 100 * 2; + this.numberOfRemainingRentals = rentals.stream() + .filter(rental -> !rental.isPaid()).count(); + } + + public List getRentals() { + return rentals; + } + public LocalDate getEndDate() { + return endDate; + } + + public Optional getNextRental() { + return nextRental; } -} - -//The system should log the rental summary to the console, printed in a human-readable format. -//The summary information should contain: -//The customer’s name and age -//The dates of the contract i.e. start and end date -//All rentals, sorted by due date -//The next due rental -//The total amount of capital and interest on the contract -//The number of remaining outstanding rentals -//If the contract is complete, i.e. the current date is after the final rental due due, then no summary should be printed. -// Instead the console should print a message to say the contract is completed. \ No newline at end of file + public double getTotalCapital() { + return totalCapital; + } + + public double getTotalInterest() { + return totalInterest; + } + + public long getNumberOfRemainingRentals() { + return numberOfRemainingRentals; + } +} \ No newline at end of file diff --git a/backlog/src/main/java/RentalSummaryService.java b/backlog/src/main/java/RentalSummaryService.java index 69f00a03..88d7c4ba 100644 --- a/backlog/src/main/java/RentalSummaryService.java +++ b/backlog/src/main/java/RentalSummaryService.java @@ -17,12 +17,31 @@ public static void main(String[] args) { } private Optional generateRentalSummary(Contract contract) { - return Optional.of(new RentalSummary(contract)); + return Optional.ofNullable(new RentalSummary(contract)); } void printRentalSummary(Contract contract) { - RentalSummaryService rentalSummaryService = new RentalSummaryService(); - System.out.println(rentalSummaryService.generateRentalSummary(contract)); + try { + RentalSummaryService rentalSummaryService = new RentalSummaryService(); + RentalSummary rentalSummary = (rentalSummaryService.generateRentalSummary(contract).orElseThrow()); + System.out.printf("%n--------------------------------------------%n"); + if (rentalSummary.getEndDate().isBefore(LocalDate.now())) { + System.out.println("Contract is completed"); + } else { + System.out.printf("Rental summary for %s, %d%n", contract.getCustomerName(), contract.getCustomerAge()); + System.out.printf("Contract: %1$tA, %1td %1$tB %1$tY - %2$tA, %1td %2$tB %2$tY%n", contract.getStartDate(), rentalSummary.getEndDate()); + System.out.printf("All rentals:%n"); + rentalSummary.getRentals().forEach(rental -> System.out.println(rental.toString())); + rentalSummary.getNextRental().ifPresentOrElse( + nextRental -> System.out.printf("Next rental %s%n", nextRental), + () -> System.out.println("No upcoming rentals — contract complete.") + ); + System.out.printf("Total amount of capital %f%n", rentalSummary.getTotalCapital()); + System.out.printf("Total interest %f%n", rentalSummary.getTotalInterest()); + System.out.printf("Remaining rentals %d%n", rentalSummary.getNumberOfRemainingRentals()); + } + } catch (Exception e){ + System.err.println(e); + } } - } diff --git a/backlog/src/main/java/ThreeYearContractRentalGenerator.java b/backlog/src/main/java/ThreeYearContractRentalGenerator.java index d10f052e..ae208615 100644 --- a/backlog/src/main/java/ThreeYearContractRentalGenerator.java +++ b/backlog/src/main/java/ThreeYearContractRentalGenerator.java @@ -9,11 +9,13 @@ public List generateRentals(Contract contract) { List rentals = new ArrayList<>(36); + double capitalAmount = contract.getCarPrice() / 36; - double interestAmount = capitalAmount / 100 * 3; - for (int month = 0; month < rentals.size(); month++) { - LocalDate dueDate = contract.getStartDate().plusMonths(1); - rentals.add(new Rental(dueDate, capitalAmount, interestAmount, false)); + double interestAmount = capitalAmount / 100 * 2; + for (int month = 1; month <= 36; month++) { + LocalDate dueDate = contract.getStartDate().plusMonths(month); + + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, dueDate.isBefore(LocalDate.now().plusDays(1)))); } return rentals; } From 9ad50ee76557b79fc4df4d898d25ac3387c441fd Mon Sep 17 00:00:00 2001 From: Droid-An Date: Sun, 2 Aug 2026 12:53:30 +0100 Subject: [PATCH 5/5] add javadoc --- backlog/src/main/java/Contract.java | 61 +++++++++++++--- .../java/OneYearContractRentalGenerator.java | 18 ++++- backlog/src/main/java/Rental.java | 38 ++++++++-- backlog/src/main/java/RentalCalculator.java | 6 ++ backlog/src/main/java/RentalSummary.java | 70 ++++++++++++++++--- .../src/main/java/RentalSummaryService.java | 53 +++++++++++++- .../ThreeYearContractRentalGenerator.java | 17 ++++- 7 files changed, 236 insertions(+), 27 deletions(-) diff --git a/backlog/src/main/java/Contract.java b/backlog/src/main/java/Contract.java index 3eaf8f20..8d064343 100644 --- a/backlog/src/main/java/Contract.java +++ b/backlog/src/main/java/Contract.java @@ -1,12 +1,34 @@ import java.time.LocalDate; +/** + * Represents a car lease agreement for a customer. + *

+ * A contract records the customer's details, the vehicle price, the start + * date, and the length of the lease in years. {@code Contract} itself does + * not validate the contract length; that validation is performed by + * {@link RentalSummary}, since only a subset of contract types (those with + * a supported {@link RentalCalculator} implementation) require a specific + * length. This keeps {@code Contract} usable for any future contract type + * without over-constraining it here. + *

+ * Instances are immutable once created. + */ public class Contract { - String customerName; - Integer customerAge; - LocalDate startDate; - double carPrice; - Integer contractLengthYears; + private final String customerName; + private final Integer customerAge; + private final LocalDate startDate; + private final double carPrice; + private final Integer contractLengthYears; + /** + * Creates a new contract. + * + * @param customerName the customer's name + * @param customerAge the customer's age + * @param startDate the date the contract begins + * @param contractLengthYears the length of the contract in years + * @param carPrice the price of the leased vehicle + */ public Contract(String customerName, Integer customerAge, LocalDate startDate, Integer contractLengthYears, double carPrice) { this.customerName = customerName; this.customerAge = customerAge; @@ -15,25 +37,48 @@ public Contract(String customerName, Integer customerAge, LocalDate startDate, I this.contractLengthYears = contractLengthYears; } - + /** + * Returns the customer's name. + * + * @return customer name + */ public String getCustomerName() { return customerName; } + /** + * Returns the customer's age. + * + * @return customer age + */ public Integer getCustomerAge() { return customerAge; } + /** + * Returns the date this contract begins. + * + * @return contract start date + */ public LocalDate getStartDate() { return startDate; } - + /** + * Returns the price of the leased vehicle. + * + * @return vehicle price + */ public double getCarPrice() { return carPrice; } + /** + * Returns the length of this contract, in years. + * + * @return contract length in years + */ public Integer getContractLengthYears() { return contractLengthYears; } -} +} \ No newline at end of file diff --git a/backlog/src/main/java/OneYearContractRentalGenerator.java b/backlog/src/main/java/OneYearContractRentalGenerator.java index fadd338f..4cf6a982 100644 --- a/backlog/src/main/java/OneYearContractRentalGenerator.java +++ b/backlog/src/main/java/OneYearContractRentalGenerator.java @@ -2,17 +2,31 @@ import java.util.ArrayList; import java.util.List; +/** + * Generates the 12 monthly rentals for a 1-year lease contract. + *

+ * Monthly capital is the vehicle price divided by 12; monthly interest is + * 2% of the vehicle price divided by 12. + */ public class OneYearContractRentalGenerator implements RentalCalculator { + + /** + * Generates the 12 monthly rentals for the given contract, starting + * one month after the contract's start date. + * + * @param contract the 1-year contract to generate rentals for + * @return list of 12 rentals, one per month + */ @Override public List generateRentals(Contract contract) { List rentals = new ArrayList<>(12); double capitalAmount = contract.getCarPrice() / 12; - double interestAmount = capitalAmount / 100 * 2; + double interestAmount = (contract.getCarPrice() / 100 * 2) / 12; for (int month = 1; month <= 12; month++) { LocalDate dueDate = contract.getStartDate().plusMonths(month); - rentals.add(new Rental(dueDate, capitalAmount, interestAmount, dueDate.isBefore(LocalDate.now().plusDays(1)))); + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, !dueDate.isAfter(LocalDate.now()))); } return rentals; } diff --git a/backlog/src/main/java/Rental.java b/backlog/src/main/java/Rental.java index b51f2753..5e1955dc 100644 --- a/backlog/src/main/java/Rental.java +++ b/backlog/src/main/java/Rental.java @@ -1,11 +1,27 @@ import java.time.LocalDate; +/** + * Represents a single monthly rental installment on a lease {@link Contract}. + *

+ * Each rental is made up of a capital portion (the value of the vehicle + * being paid off) and an interest portion (the cost of leasing), due on a + * specific date. A rental is considered paid once its due date has passed. + */ public class Rental { - LocalDate dueDate; - double capitalAmount; - double interestAmount; + final LocalDate dueDate; + final double capitalAmount; + final double interestAmount; boolean paid; + /** + * Creates a rental installment. + * + * @param dueDate the date this rental is due + * @param capitalAmount the capital portion of this rental + * @param interestAmount the interest portion of this rental + * @param paid whether this rental has already been paid, + * i.e. whether its due date is on or before today + */ public Rental(LocalDate dueDate, double capitalAmount, double interestAmount, boolean paid) { this.dueDate = dueDate; this.capitalAmount = capitalAmount; @@ -13,15 +29,27 @@ public Rental(LocalDate dueDate, double capitalAmount, double interestAmount, bo this.paid = paid; } + /** + * Returns whether this rental has already been paid. + * + * @return {@code true} if this rental's due date is on or before today, + * {@code false} otherwise + */ public boolean isPaid() { return paid; } + /** + * Returns a human-readable, multi-line representation of this rental, + * including its due date, capital and interest amounts, and paid status. + * + * @return formatted string describing this rental + */ @Override public String toString() { return String.format("Due date: %1$tA, %1td %1$tB %1$tY %n" + "Capital amount: %f%n" + "Interest amount: %f%n" + - "Paid: %B%n", dueDate, capitalAmount, interestAmount, paid); + "Paid: %b%n", dueDate, capitalAmount, interestAmount, paid); } -} +} \ No newline at end of file diff --git a/backlog/src/main/java/RentalCalculator.java b/backlog/src/main/java/RentalCalculator.java index df19bc03..5c680d0e 100644 --- a/backlog/src/main/java/RentalCalculator.java +++ b/backlog/src/main/java/RentalCalculator.java @@ -1,5 +1,11 @@ import java.util.List; +/** + * Generates the monthly rentals for a lease contract. + *

+ * Monthly capital is the vehicle price divided by amount of months in a contract length; monthly interest is + * 2% of the vehicle price divided by amount of months in a contract length. + */ public interface RentalCalculator { List generateRentals(Contract contract); diff --git a/backlog/src/main/java/RentalSummary.java b/backlog/src/main/java/RentalSummary.java index cce45c97..aeee8703 100644 --- a/backlog/src/main/java/RentalSummary.java +++ b/backlog/src/main/java/RentalSummary.java @@ -3,17 +3,35 @@ import java.util.List; import java.util.Optional; +/** + * Represents a computed summary of a car rental {@link Contract}, including + * the full schedule of {@link Rental} installments, the next unpaid rental, + * and aggregate totals for capital and interest. + *

+ * The rental schedule is generated based on the contract's length: a 1-year + * contract uses {@link OneYearContractRentalGenerator}, a 3-year contract + * uses {@link ThreeYearContractRentalGenerator}. Any other contract length + * is not supported and results in an exception. + */ public class RentalSummary { - List rentals; - LocalDate endDate; - Optional nextRental; - double totalCapital; - double totalInterest; - long numberOfRemainingRentals; - + final List rentals; + final LocalDate endDate; + final Optional nextRental; + final double totalCapital; + final double totalInterest; + final long numberOfRemainingRentals; + /** + * Builds a rental summary for the given contract, generating the full + * rental schedule and computing totals and the next outstanding rental. + * + * @param contract the contract to summarize; must have a supported + * contract length (1 or 3 years) + * @throws InvalidParameterException if the contract length is neither + * 1 nor 3 years + */ public RentalSummary(Contract contract) { - this.endDate = contract.startDate.plusYears(contract.getContractLengthYears()); + this.endDate = contract.getStartDate().plusYears(contract.getContractLengthYears()); if (contract.getContractLengthYears() == 1) { OneYearContractRentalGenerator oneYearContractRentalGenerator = new OneYearContractRentalGenerator(); this.rentals = oneYearContractRentalGenerator.generateRentals(contract); @@ -31,25 +49,61 @@ public RentalSummary(Contract contract) { .filter(rental -> !rental.isPaid()).count(); } + /** + * Returns the full list of rentals generated for this contract, covering + * the entire contract duration. + * + * @return list of all rentals for the contract + */ public List getRentals() { return rentals; } + + /** + * Returns the date the contract ends, calculated as the contract's + * start date plus its length in years. + * + * @return the contract end date + */ public LocalDate getEndDate() { return endDate; } + /** + * Returns the next rental that has not yet been paid, if any. + * + * @return an {@link Optional} containing the next unpaid rental, or + * empty if all rentals have been paid + */ public Optional getNextRental() { return nextRental; } + /** + * Returns the total capital amount for the contract, based on the + * car's price. + * + * @return total capital amount + */ public double getTotalCapital() { return totalCapital; } + /** + * Returns the total interest amount for the contract, calculated as + * 2% of the car's price. + * + * @return total interest amount + */ public double getTotalInterest() { return totalInterest; } + /** + * Returns the number of rentals that have not yet been paid. + * + * @return count of remaining (unpaid) rentals + */ public long getNumberOfRemainingRentals() { return numberOfRemainingRentals; } diff --git a/backlog/src/main/java/RentalSummaryService.java b/backlog/src/main/java/RentalSummaryService.java index 88d7c4ba..019f7fc0 100644 --- a/backlog/src/main/java/RentalSummaryService.java +++ b/backlog/src/main/java/RentalSummaryService.java @@ -1,7 +1,27 @@ import java.time.LocalDate; import java.util.Optional; +/** + * Generates and prints rental summaries for car lease {@link Contract}s. + *

+ * A rental summary includes the customer's details, the contract's start + * and end dates, the full list of rentals, the next outstanding rental, + * total capital and interest, and the number of remaining unpaid rentals. + * If a contract has already run its full term, a completion message is + * printed instead of the summary. + * + * @see RentalSummary + * @see Contract + */ public class RentalSummaryService { + + /** + * Demonstrates generating and printing rental summaries for a range of + * contracts, including a contract with an unsupported length and a + * contract that has already completed. + * + * @param args not used + */ public static void main(String[] args) { Contract oneYearContract = new Contract("John Smith", 25, LocalDate.of(2025, 8, 12), 1, 10000); @@ -16,10 +36,39 @@ public static void main(String[] args) { rentalSummaryService.printRentalSummary(completedContract); // Should print a message to say the contract is complete } + /** + * Builds a {@link RentalSummary} for the given contract. + * + * @param contract the contract to summarize + * @return an {@link Optional} containing the generated summary; always + * present, since {@link RentalSummary}'s constructor either + * succeeds or throws + */ private Optional generateRentalSummary(Contract contract) { - return Optional.ofNullable(new RentalSummary(contract)); + return Optional.of(new RentalSummary(contract)); } + /** + * Prints a rental summary for the given contract to the console. + *

+ * The printed summary contains: + *

    + *
  • the customer's name and age
  • + *
  • the contract's start and end dates
  • + *
  • all rentals for the contract
  • + *
  • the next outstanding rental, if any
  • + *
  • the total capital and interest for the contract
  • + *
  • the number of remaining unpaid rentals
  • + *
+ * If the contract's end date has already passed, a completion message + * is printed instead of the full summary. + *

+ * Any error encountered while generating the summary (for example, an + * unsupported contract length) is caught and logged to the error + * console rather than propagating to the caller. + * + * @param contract the contract to print a summary for + */ void printRentalSummary(Contract contract) { try { RentalSummaryService rentalSummaryService = new RentalSummaryService(); @@ -44,4 +93,4 @@ void printRentalSummary(Contract contract) { System.err.println(e); } } -} +} \ No newline at end of file diff --git a/backlog/src/main/java/ThreeYearContractRentalGenerator.java b/backlog/src/main/java/ThreeYearContractRentalGenerator.java index ae208615..05ab9e8e 100644 --- a/backlog/src/main/java/ThreeYearContractRentalGenerator.java +++ b/backlog/src/main/java/ThreeYearContractRentalGenerator.java @@ -2,8 +2,21 @@ import java.util.ArrayList; import java.util.List; +/** + * Generates the 36 monthly rentals for a 3-year lease contract. + *

+ * Monthly capital is the vehicle price divided by 36; monthly interest is + * 2% of the vehicle price divided by 36. + */ public class ThreeYearContractRentalGenerator implements RentalCalculator { + /** + * Generates the 36 monthly rentals for the given contract, starting + * one month after the contract's start date. + * + * @param contract the 3-year contract to generate rentals for + * @return list of 36 rentals, one per month + */ @Override public List generateRentals(Contract contract) { @@ -11,11 +24,11 @@ public List generateRentals(Contract contract) { double capitalAmount = contract.getCarPrice() / 36; - double interestAmount = capitalAmount / 100 * 2; + double interestAmount = (contract.getCarPrice() / 100 * 2) / 36; for (int month = 1; month <= 36; month++) { LocalDate dueDate = contract.getStartDate().plusMonths(month); - rentals.add(new Rental(dueDate, capitalAmount, interestAmount, dueDate.isBefore(LocalDate.now().plusDays(1)))); + rentals.add(new Rental(dueDate, capitalAmount, interestAmount, !dueDate.isAfter(LocalDate.now()))); } return rentals; }