<< Back to Object Oriented Programming Portfolio
Assignment 14
UML
Vehicle class
package com.nhlstenden.week2;
public abstract class Vehicle
{
private final String id;
private final double dailyRate;
private final double deposit;
private boolean isUnderMaintenance;
private double totalTurnover;
public Vehicle(String id, double dailyRate, double deposit)
{
this.id = id;
this.dailyRate = dailyRate;
this.deposit = deposit;
this.isUnderMaintenance = false;
this.totalTurnover = 0;
}
public abstract double calculateRental(int days, double kmDriven);
public void markMaintenance(boolean status)
{
setUnderMaintenance(status);
}
public String getId()
{
return this.id;
}
public double getDailyRate()
{
return this.dailyRate;
}
public double getDeposit()
{
return this.deposit;
}
public boolean isUnderMaintenance()
{
return this.isUnderMaintenance;
}
public void setUnderMaintenance(boolean underMaintenance)
{
this.isUnderMaintenance = underMaintenance;
}
public double getTotalTurnover()
{
return this.totalTurnover;
}
public void setTotalTurnover(double totalTurnover)
{
this.totalTurnover = totalTurnover;
}
public void addToTurnover(double amount)
{
this.totalTurnover += amount;
}
}
Bicycle class
package com.nhlstenden.week2;
public class Bicycle extends Vehicle
{
public Bicycle(String id, double dailyRate, double deposit)
{
super(id, dailyRate, deposit);
}
@Override
public double calculateRental(int days, double kmDriven)
{
return days * getDailyRate();
}
}
Car class
package com.nhlstenden.week2;
public class Car extends Vehicle
{
public Car(String id, double dailyRate, double deposit)
{
super(id, dailyRate, deposit);
}
@Override
public double calculateRental(int days, double kmDriven)
{
double surcharge = kmDriven > 100 ? (kmDriven - 100) * 0.10 : 0;
return days * getDailyRate() + surcharge;
}
}
Scooter class
package com.nhlstenden.week2;
public class Scooter extends Vehicle
{
public Scooter(String id, double dailyRate, double deposit)
{
super(id, dailyRate, deposit);
}
@Override
public double calculateRental(int days, double kmDriven)
{
double surcharge = kmDriven > 100 ? (kmDriven - 100) * 0.10 : 0;
return days * getDailyRate() + surcharge;
}
}
Customer class
package com.nhlstenden.week2;
import java.util.*;
public class Customer
{
private String id;
private String name;
private List<Rental> rentals = new ArrayList<>();
public Customer(String id, String name)
{
this.id = id;
this.name = name;
}
public String getId()
{
return this.id;
}
public void setId(String id)
{
this.id = id;
}
public String getName()
{
return this.name;
}
public void setName(String name)
{
this.name = name;
}
public List<Rental> getRentals()
{
return this.rentals;
}
public void setRentals(List<Rental> rentals)
{
this.rentals = rentals;
}
public void addRental(Rental r)
{
rentals.add(r);
}
}
Invoice class
package com.nhlstenden.week2;
public class Invoice
{
private Rental rental;
private double dailyCharge;
private double kmSurcharge;
private double deposit;
public double total;
public Invoice(Rental rental, double dailyCharge, double kmSurcharge, double deposit, double total)
{
this.rental = rental;
this.dailyCharge = dailyCharge;
this.kmSurcharge = kmSurcharge;
this.deposit = deposit;
this.total = total;
}
public Rental getRental()
{
return this.rental;
}
public void setRental(Rental rental)
{
this.rental = rental;
}
public double getDailyCharge()
{
return this.dailyCharge;
}
public void setDailyCharge(double dailyCharge)
{
this.dailyCharge = dailyCharge;
}
public double getKmSurcharge()
{
return this.kmSurcharge;
}
public void setKmSurcharge(double kmSurcharge)
{
this.kmSurcharge = kmSurcharge;
}
public double getDeposit()
{
return this.deposit;
}
public void setDeposit(double deposit)
{
this.deposit = deposit;
}
public double getTotal()
{
return this.total;
}
public void setTotal(double total)
{
this.total = total;
}
}
Rental class
package com.nhlstenden.week2;
import java.time.*;
public class Rental
{
private String id;
private Customer customer;
private Vehicle vehicle;
private LocalDate startDate;
private LocalDate endDate;
private double kmDriven;
public Rental(String id, Customer customer, Vehicle vehicle, LocalDate startDate, LocalDate endDate, double kmDriven)
{
this.id = id;
this.customer = customer;
this.vehicle = vehicle;
this.startDate = startDate;
this.endDate = endDate;
this.kmDriven = kmDriven;
}
public String getId()
{
return this.id;
}
public void setId(String id)
{
this.id = id;
}
public Customer getCustomer()
{
return this.customer;
}
public void setCustomer(Customer customer)
{
this.customer = customer;
}
public Vehicle getVehicle()
{
return this.vehicle;
}
public void setVehicle(Vehicle vehicle)
{
this.vehicle = vehicle;
}
public LocalDate getStartDate()
{
return this.startDate;
}
public void setStartDate(LocalDate startDate)
{
this.startDate = startDate;
}
public LocalDate getEndDate()
{
return this.endDate;
}
public void setEndDate(LocalDate endDate)
{
this.endDate = endDate;
}
public double getKmDriven()
{
return this.kmDriven;
}
public void setKmDriven(double kmDriven)
{
this.kmDriven = kmDriven;
}
public Invoice calculateInvoice()
{
int days = Period.between(startDate, endDate).getDays() + 1;
double totalRental = vehicle.calculateRental(days, kmDriven);
double baseCost = days * vehicle.getDailyRate();
double surcharge = totalRental - baseCost;
return new Invoice(this, baseCost, surcharge, vehicle.getDeposit(), totalRental + vehicle.getDeposit());
}
}
RentalSystem class
package com.nhlstenden.week2;
import java.time.*;
import java.util.*;
public class RentalSystem
{
private List<Vehicle> vehicles = new ArrayList<>();
private List<Customer> customers = new ArrayList<>();
private List<Rental> rentals = new ArrayList<>();
public void addVehicle(Vehicle v)
{
vehicles.add(v);
}
public void addCustomer(Customer c)
{
customers.add(c);
}
public Invoice rentVehicle(String customerId, String vehicleId, LocalDate start, LocalDate end, double km)
{
Customer customer = customers.stream().filter(c -> c.getId().equals(customerId)).findFirst().orElse(null);
Vehicle vehicle = vehicles.stream().filter(v -> v.getId().equals(vehicleId) && !v.isUnderMaintenance()).findFirst().orElse(null);
if(customer == null || vehicle == null)
{
return null;
}
Rental rental = new Rental(UUID.randomUUID().toString(), customer, vehicle, start, end, km);
rentals.add(rental);
customer.addRental(rental);
Invoice invoice = rental.calculateInvoice();
vehicle.addToTurnover(invoice.total);
return invoice;
}
public Map<String, Double> getTurnoverByVehicleType(Month month)
{
Map<String, Double> turnover = new HashMap<>();
turnover.put("Bicycle", 0.0);
turnover.put("Scooter", 0.0);
turnover.put("Car", 0.0);
for(Rental r : rentals)
{
if(r.getStartDate().getMonth() == month)
{
String type = r.getVehicle().getClass().getSimpleName();
Invoice inv = r.calculateInvoice();
turnover.put(type, turnover.get(type) + inv.total);
}
}
return turnover;
}
}