Repository navigation
London | 26-Java-June | Pooriya Ketabi | Sprint 4 | Rental Summary Generator #75
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
Open
PooriyaKTB
wants to merge
3
commits into
CodeYourFuture:main
Choose a base branch
from
PooriyaKTB:Sprint-4-Rental-Summary-Generator
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Contract { | ||
|
|
||
| private final String customerName; | ||
| private final int customerAge; | ||
| private final LocalDate startDate; | ||
| private final double carPrice; | ||
| private final int contractLengthYears; | ||
|
|
||
| public Contract(Customer customer, LocalDate startDate, double carPrice, int contractLengthYears) { | ||
| this.customerName = customer.getCustomerName(); | ||
| this.customerAge = customer.getCustomerAge(); | ||
| this.carPrice = carPrice; | ||
| this.contractLengthYears = contractLengthYears; | ||
| this.startDate = startDate; | ||
| } | ||
|
|
||
| public String getCustomerName() { | ||
| return customerName; | ||
| } | ||
|
|
||
| public int getCustomerAge() { | ||
| return customerAge; | ||
| } | ||
|
|
||
| public LocalDate getStartDate() { | ||
| return startDate; | ||
| } | ||
|
|
||
| public double getCarPrice() { | ||
| return carPrice; | ||
| } | ||
|
|
||
| public int getContractLengthYears() { | ||
| return contractLengthYears; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| public class Customer { | ||
|
|
||
| private final String customerName; | ||
| private final int customerAge; | ||
|
|
||
| public Customer(String firstName, String lastName, int customerAge) { | ||
| this.customerName = firstName + " " + lastName; | ||
| this.customerAge = customerAge; | ||
| } | ||
|
|
||
| public String getCustomerName() { | ||
| return customerName; | ||
| } | ||
|
|
||
| public int getCustomerAge() { | ||
| return customerAge; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return customerName + " " + customerAge; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Main { | ||
| public static void main(String[] args) { | ||
|
|
||
| Customer customer1 = new Customer("Pooriya", "ketabi", 34); | ||
| Contract contract1 = new Contract(customer1, LocalDate.now(), 30000, 3); | ||
|
|
||
| System.out.println(customer1.getCustomerName()); | ||
| System.out.println(contract1.getCustomerName()); | ||
|
|
||
| Contract oneYearContract = new Contract(new Customer("John", "Smith", 25), LocalDate.of(2025, 8, 12), 10000, 1); | ||
| Contract twoYearContract = new Contract(new Customer("Michael", "Jones", 56), LocalDate.of(2025, 12, 12), 20000, 2); | ||
| Contract threeYearContract = new Contract(new Customer("Jane", "Doe", 77), LocalDate.of(2024, 4, 1), 50000, 3); | ||
| Contract completedContract = new Contract(new Customer("Rosie", "Parker", 47), LocalDate.of(2021, 7, 10), 40000, 3); | ||
|
|
||
| RentalSummaryService rentalSummaryService = new RentalSummaryService(); | ||
| try { | ||
| rentalSummaryService.printRentalSummary(oneYearContract); | ||
| rentalSummaryService.printRentalSummary(threeYearContract); | ||
| rentalSummaryService.printRentalSummary(completedContract); // Should print a message to say the contract is complete | ||
| rentalSummaryService.printRentalSummary(twoYearContract); // Should throw a suitable error | ||
| } catch (IllegalArgumentException e) { | ||
| System.out.println(e.getMessage()); | ||
| } | ||
| } | ||
| } |
22 changes: 22 additions & 0 deletions
22
src/main/java/rentalSummaryGenerator/OneYearContractRentalGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class OneYearContractRentalGenerator implements RentalCalculator { | ||
|
|
||
| @Override | ||
| public List<Rental> generateRentals(Contract contract) { | ||
|
|
||
| List<Rental> rentals = new ArrayList<>(); | ||
|
|
||
| for (int i = 1; i <= contract.getContractLengthYears() * 12; i++) { | ||
|
|
||
| LocalDate nextRental = contract.getStartDate().plusMonths(i); | ||
| rentals.add(new Rental(nextRental, contract.getCarPrice() / 12, (contract.getCarPrice() * 0.02) / 12, !nextRental.isAfter(LocalDate.now()))); | ||
| } | ||
|
|
||
| return rentals; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Rental { | ||
|
|
||
| private final LocalDate dueDate; | ||
| private final double capitalAmount; | ||
| private final double interestAmount; | ||
| private boolean paid; | ||
|
|
||
| public Rental(LocalDate dueDate, double capitalAmount, double interestAmount, boolean isPaid) { | ||
| this.dueDate = dueDate; | ||
| this.capitalAmount = capitalAmount; | ||
| this.interestAmount = interestAmount; | ||
| this.paid = isPaid; | ||
| } | ||
|
|
||
| public LocalDate getDueDate() { | ||
| return dueDate; | ||
| } | ||
|
|
||
| public double getCapitalAmount() { | ||
| return capitalAmount; | ||
| } | ||
|
|
||
| public double getInterestAmount() { | ||
| return interestAmount; | ||
| } | ||
|
|
||
| public boolean isPaid() { | ||
| return paid; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "Rental{" + | ||
| "dueDate=" + dueDate + | ||
| ", capitalAmount=" + String.format("%.2f", capitalAmount) + | ||
| ", interestAmount=" + String.format("%.2f", interestAmount) + | ||
| ", paid=" + paid + | ||
| '}'; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| public interface RentalCalculator { | ||
|
|
||
| List<Rental> generateRentals(Contract contract); | ||
| } |
102 changes: 102 additions & 0 deletions
102
src/main/java/rentalSummaryGenerator/RentalSummary.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,102 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class RentalSummary { | ||
|
|
||
| private final String customerName; | ||
| private final int customerAge; | ||
| private final LocalDate startDate; | ||
| private final LocalDate endDate; | ||
| private List<Rental> rentals; | ||
| private LocalDate nextDue; | ||
| private double totalAmount; | ||
| private double outstandingAmount; | ||
| private long outstandingCount; | ||
| private boolean isSettled; | ||
|
|
||
| public RentalSummary(String customerName, int customerAge, LocalDate startDate, LocalDate endDate) { | ||
| this.customerName = customerName; | ||
| this.customerAge = customerAge; | ||
| this.startDate = startDate; | ||
| this.endDate = endDate; | ||
| } | ||
|
|
||
| public String getCustomerName() { | ||
| return customerName; | ||
| } | ||
|
|
||
| public int getCustomerAge() { | ||
| return customerAge; | ||
| } | ||
|
|
||
| public LocalDate getStartDate() { | ||
| return startDate; | ||
| } | ||
|
|
||
| public LocalDate getEndDate() { | ||
| return endDate; | ||
| } | ||
|
|
||
| public List<Rental> getRentals() { | ||
| return rentals; | ||
| } | ||
|
|
||
| public void setRentals(List<Rental> rentals) { | ||
| this.rentals = rentals; | ||
| } | ||
|
|
||
| public Optional<LocalDate> getNextDue() { | ||
| return Optional.ofNullable(this.nextDue); | ||
| } | ||
|
|
||
| public void setNextDue(LocalDate nextDue) { | ||
| this.nextDue = nextDue; | ||
| } | ||
|
|
||
| public double getTotalAmount() { | ||
| return totalAmount; | ||
| } | ||
|
|
||
| public void setTotalAmount(double totalInterestAmount, double capitalAmount) { | ||
| this.totalAmount = totalInterestAmount + capitalAmount; | ||
| } | ||
|
|
||
| public double getOutstandingAmount() { | ||
| return outstandingAmount; | ||
| } | ||
|
|
||
| public void setOutstandingAmount(double outstandingAmount) { | ||
| this.outstandingAmount = outstandingAmount; | ||
| } | ||
|
|
||
| public void setOutstandingCount(long outstandingCount) { | ||
| this.outstandingCount = outstandingCount; | ||
| } | ||
|
|
||
| public boolean isSettled() { | ||
| return isSettled; | ||
| } | ||
|
|
||
| public void setSettled(boolean settled) { | ||
| isSettled = settled; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "RentalSummary{" + | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Your IDEs generate tostring command might help here |
||
| "customerName='" + customerName + '\'' + | ||
| ", customerAge=" + customerAge + | ||
| ", startDate=" + startDate + | ||
| ", endDate=" + endDate + | ||
| ", nextDue=" + nextDue + | ||
| ", totalAmount=" + String.format("%.2f", totalAmount) + | ||
| ", outstandingAmount=" + String.format("%.2f", outstandingAmount) + | ||
| ", outstandingCount=" + outstandingCount + | ||
| ", isSettled=" + isSettled + | ||
| ", rentals=" + rentals + | ||
| '}'; | ||
| } | ||
| } | ||
53 changes: 53 additions & 0 deletions
53
src/main/java/rentalSummaryGenerator/RentalSummaryService.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,53 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.List; | ||
| import java.util.Optional; | ||
|
|
||
| public class RentalSummaryService { | ||
|
|
||
| private Optional<RentalSummary> generateRentalSummary(Contract contract) { | ||
|
|
||
| RentalSummary summary = new RentalSummary(contract.getCustomerName(), contract.getCustomerAge(), contract.getStartDate(), contract.getStartDate().plusYears(contract.getContractLengthYears())); | ||
|
|
||
| RentalCalculator rentalCalculator; | ||
|
|
||
| if (contract.getContractLengthYears() == 1) { | ||
| rentalCalculator = new OneYearContractRentalGenerator(); | ||
| } else if (contract.getContractLengthYears() == 3) { | ||
| rentalCalculator = new ThreeYearsContractRentalGenerator(); | ||
| } else { | ||
| throw new IllegalArgumentException( | ||
| "Invalid contract length: " + contract.getContractLengthYears()); | ||
| } | ||
|
|
||
| summary.setRentals(rentalCalculator.generateRentals(contract)); | ||
|
|
||
| List<Rental> rentals = summary.getRentals(); | ||
| LocalDate today = LocalDate.now(); | ||
|
|
||
| if (rentals.get(rentals.size() - 1).getDueDate().isBefore(today)) { | ||
| return Optional.empty(); | ||
| } | ||
|
|
||
| summary.setNextDue(rentals.stream().map(Rental::getDueDate).filter(dueDate -> (dueDate).isAfter(today)).findFirst().orElse(null)); | ||
| summary.setTotalAmount(rentals.stream().map(Rental::getInterestAmount).reduce(0.0, Double::sum), contract.getCarPrice()); | ||
| summary.setOutstandingAmount(rentals.stream().filter(r -> (r.getDueDate()).isAfter(today)).mapToDouble(r -> r.getCapitalAmount() + r.getInterestAmount()).reduce(0, Double::sum)); | ||
| summary.setOutstandingCount(rentals.stream().filter(r -> (r.getDueDate()).isAfter(today)).count()); | ||
| // summary.setSettled(rentals.stream().map(r -> r.getDueDate().isAfter(LocalDate.now())).findAny().isPresent()); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Commented out line? |
||
| summary.setSettled(rentals.stream().allMatch(Rental::isPaid)); | ||
|
|
||
| return Optional.of(summary); | ||
| } | ||
|
|
||
| void printRentalSummary(Contract contract) { | ||
|
|
||
| Optional<RentalSummary> summaryOptional = generateRentalSummary(contract); | ||
|
|
||
| if (summaryOptional.isPresent()) { | ||
| System.out.println(summaryOptional.get()); | ||
| } else { | ||
| System.out.println("Contract for " + contract.getCustomerName() + " is completed."); | ||
| } | ||
| } | ||
| } | ||
22 changes: 22 additions & 0 deletions
22
src/main/java/rentalSummaryGenerator/ThreeYearsContractRentalGenerator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| package rentalSummaryGenerator; | ||
|
|
||
| import java.time.LocalDate; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class ThreeYearsContractRentalGenerator implements RentalCalculator { | ||
|
|
||
| @Override | ||
| public List<Rental> generateRentals(Contract contract) { | ||
|
|
||
| List<Rental> rentals = new ArrayList<>(); | ||
|
|
||
| for (int i = 1; i <= contract.getContractLengthYears() * 12; i++) { | ||
|
|
||
| LocalDate nextRental = contract.getStartDate().plusMonths(i); | ||
| rentals.add(new Rental(nextRental, contract.getCarPrice() / 36, (contract.getCarPrice() * 0.03) / 36, !nextRental.isAfter(LocalDate.now()))); | ||
| } | ||
|
|
||
| return rentals; | ||
| } | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not store as two fields?