Skip to content
Open
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
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea/
out/
target/
17 changes: 17 additions & 0 deletions backlog/pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>

<groupId>com.backlog</groupId>
<artifactId>backlog</artifactId>
<version>1.0-SNAPSHOT</version>

<properties>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</properties>

</project>
84 changes: 84 additions & 0 deletions backlog/src/main/java/Contract.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import java.time.LocalDate;

/**
* Represents a car lease agreement for a customer.
* <p>
* 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.
* <p>
* Instances are immutable once created.
*/
public class Contract {
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;
this.startDate = startDate;
this.carPrice = carPrice;
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;
}
}
33 changes: 33 additions & 0 deletions backlog/src/main/java/OneYearContractRentalGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* Generates the 12 monthly rentals for a 1-year lease contract.
* <p>
* 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<Rental> generateRentals(Contract contract) {

List<Rental> rentals = new ArrayList<>(12);

double capitalAmount = contract.getCarPrice() / 12;
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.isAfter(LocalDate.now())));
}
return rentals;
}
}
55 changes: 55 additions & 0 deletions backlog/src/main/java/Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
import java.time.LocalDate;

/**
* Represents a single monthly rental installment on a lease {@link Contract}.
* <p>
* 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 {
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;
this.interestAmount = interestAmount;
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);
}
}
12 changes: 12 additions & 0 deletions backlog/src/main/java/RentalCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import java.util.List;

/**
* Generates the monthly rentals for a lease contract.
* <p>
* 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<Rental> generateRentals(Contract contract);

}
110 changes: 110 additions & 0 deletions backlog/src/main/java/RentalSummary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
import java.security.InvalidParameterException;
import java.time.LocalDate;
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.
* <p>
* 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 {
final List<Rental> rentals;
final LocalDate endDate;
final Optional<Rental> 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.getStartDate().plusYears(contract.getContractLengthYears());
if (contract.getContractLengthYears() == 1) {
OneYearContractRentalGenerator oneYearContractRentalGenerator = new OneYearContractRentalGenerator();

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As you said in your question, yep the three year and one year contract rental generator could be a single class which takes in the time period of the contract, meaning it could be used for any number of years.

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();
}

/**
* 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<Rental> 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<Rental> 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;
}
}
Loading