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 @@
out/
.idea/
*.iml
28 changes: 28 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?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>org.codeyourfuture</groupId>
<artifactId>module-java</artifactId>
<version>1.0-SNAPSHOT</version>

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

<dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>5.14.0</version>
<scope>test</scope>
</dependency>
</dependencies>

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

/**
* Represents a car rental contract for a customer.
*/
public class Contract {

private String customerName;
private int customerAge;
private LocalDate startDate;
private int contractLengthYears;
private double carPrice;

/**
* Creates a new rental contract.
*
* @param customerName the name of the customer
* @param customerAge the age of the customer
* @param startDate the start date of the contract
* @param contractLengthYears the length of the contract in years
* @param carPrice the price of the car

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

When variables are primitive types or quite common reference types it is important to be specific about what they represent. In particular something which often causes confusion is units (such as units of time or currency). Can you think of something which might be useful to add in the documentation to make it clearer what this double variable represents?

*/
public Contract(
String customerName,
int customerAge,
LocalDate startDate,
int contractLengthYears,
double carPrice
) {
this.customerName = customerName;
this.customerAge = customerAge;
this.startDate = startDate;
this.contractLengthYears = contractLengthYears;
this.carPrice = carPrice;
}

public String getCustomerName() {
return customerName;
}

public int getCustomerAge() {
return customerAge;
}

public LocalDate getStartDate() {
return startDate;
}

public int getContractLengthYears() {
return contractLengthYears;
}

public double getCarPrice() {
return carPrice;
}
}
70 changes: 70 additions & 0 deletions src/main/java/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
import java.time.LocalDate;

/**
* Runs examples of the rental summary generator.
*/
public class Main {

/**
* Creates different contracts and prints their rental summaries.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
RentalSummaryService rentalSummaryService = new RentalSummaryService();

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

System.out.println("ONE-YEAR CONTRACT");
rentalSummaryService.printRentalSummary(oneYearContract);


System.out.println();
System.out.println("TWO-YEAR CONTRACT");

try {
rentalSummaryService.printRentalSummary(twoYearContract);
} catch (IllegalArgumentException exception) {
System.out.println("Error: " + exception.getMessage());
}

System.out.println();
System.out.println("THREE-YEAR CONTRACT");
rentalSummaryService.printRentalSummary(threeYearContract);

System.out.println();
System.out.println("COMPLETED CONTRACT");
rentalSummaryService.printRentalSummary((completedContract));

}
}
45 changes: 45 additions & 0 deletions src/main/java/OneYearContractRentalGenerator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;

/**
* Generates monthly rental payments for a one-year contract.
*/
public class OneYearContractRentalGenerator implements RentalCalculator {

private static final int NUMBER_OF_RENTALS = 12;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

nice

private static final double INTEREST_RATE = 0.02;

/**
* Generates twelve monthly rental payments for the given contract.
*
* @param contract the contract used to calculate the rental payments
* @return the generated monthly rental payments
*/
@Override
public List<Rental> generateRentals(Contract contract) {
List<Rental> rentals = new ArrayList<>();

double monthlyCapitalAmount = contract.getCarPrice() / NUMBER_OF_RENTALS;

double monthlyInterestAmount = (contract.getCarPrice() * INTEREST_RATE) / NUMBER_OF_RENTALS;

LocalDate currentDate = LocalDate.now();

for (int monthNumber = 1; monthNumber <= NUMBER_OF_RENTALS; monthNumber++) {
LocalDate dueDate = contract.getStartDate().plusMonths(monthNumber);
boolean paid = !dueDate.isAfter(currentDate);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

To me dueDate.isBefore(currentDate) is clearer that !dueDate.isAfter(currentDate) as it's one less logical step to think through (small readability point though!). Was there any particular reason you did it this way? And how did you decide to handle the case where the current date is equal to the due date for one of the rentals?


Rental rental = new Rental(
dueDate,
monthlyCapitalAmount,
monthlyInterestAmount,
paid
);

rentals.add(rental);
}

return rentals;
}
}
48 changes: 48 additions & 0 deletions src/main/java/Rental.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.time.LocalDate;

/**
* Represents a single monthly payment in a rental contract.
*/
public class Rental {

private LocalDate dueDate;
private double capitalAmount;
private double interestAmount;
private boolean paid;

/**
* Creates a monthly rental payment.
*
* @param dueDate the date the payment is due
* @param capitalAmount the capital part of the payment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

* @param interestAmount the interest part of the payment
* @param paid whether the payment has been paid
*/
public Rental(
LocalDate dueDate,
double capitalAmount,
double interestAmount,
boolean paid
) {
this.dueDate = dueDate;
this.capitalAmount = capitalAmount;
this.interestAmount = interestAmount;
this.paid = paid;
}

public LocalDate getDueDate() {
return dueDate;
}

public double getCapitalAmount() {
return capitalAmount;
}

public double getInterestAmount() {
return interestAmount;
}

public boolean isPaid() {
return paid;
}
}
15 changes: 15 additions & 0 deletions src/main/java/RentalCalculator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import java.util.List;

/**
* Defines how monthly rental payments are generated for a contract.
*/
public interface RentalCalculator {

/**
* Generates the monthly payments for the given contract.
*
* @param contract the contract used to calculate the payments
* @return the generated monthly rental payments
*/
List<Rental> generateRentals(Contract contract);
}
91 changes: 91 additions & 0 deletions src/main/java/RentalSummary.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
import java.time.LocalDate;
import java.util.List;
import java.util.Optional;

/**
* Represents a summary of a customer's rental contract and payments.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

What if the customer has more than one contract, or has had a contract in the past? Is this just for a certain contract or for all time for that customer?

*/
public class RentalSummary {

private String customerName;
private int customerAge;
private LocalDate contractStartDate;
private LocalDate contractEndDate;
private List<Rental> rentals;
private Optional<Rental> nextDueRental;
private double totalCapitalAmount;
private double totalInterestAmount;
private long outstandingRentalCount;

/**
* Creates a rental summary.
*
* @param customerName the name of the customer
* @param customerAge the age of the customer
* @param contractStartDate the start date of the contract
* @param contractEndDate the end date of the contract
* @param rentals the monthly rental payments
* @param nextDueRental the next unpaid rental, if one exists
* @param totalCapitalAmount the total capital amount
* @param totalInterestAmount the total interest amount
* @param outstandingRentalCount the number of unpaid rentals
*/
public RentalSummary(
String customerName,
int customerAge,
LocalDate contractStartDate,
LocalDate contractEndDate,
List<Rental> rentals,
Optional<Rental> nextDueRental,
double totalCapitalAmount,
double totalInterestAmount,
long outstandingRentalCount
) {
this.customerName = customerName;
this.customerAge = customerAge;
this.contractStartDate = contractStartDate;
this.contractEndDate = contractEndDate;
this.rentals = rentals;
this.nextDueRental = nextDueRental;
this.totalCapitalAmount = totalCapitalAmount;
this.totalInterestAmount = totalInterestAmount;
this.outstandingRentalCount = outstandingRentalCount;

}

public String getCustomerName() {
return customerName;
}

public int getCustomerAge() {
return customerAge;
}

public LocalDate getContractStartDate() {
return contractStartDate;
}

public LocalDate getContractEndDate() {
return contractEndDate;
}

public List<Rental> getRentals() {
return rentals;
}

public Optional<Rental> getNextDueRental() {
return nextDueRental;
}

public double getTotalCapitalAmount() {
return totalCapitalAmount;
}

public double getTotalInterestAmount() {
return totalInterestAmount;
}

public long getOutstandingRentalCount() {
return outstandingRentalCount;
}
}
Loading