[go: up one dir, main page]

0% found this document useful (0 votes)
30 views10 pages

Car Rental System

Uploaded by

thirdparty546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
30 views10 pages

Car Rental System

Uploaded by

thirdparty546
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 10

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-
default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit


this template

*/

package rentalsystem_composition_casestudy;

/**

* @author Jazib E Nazar

*/

public class Vehicle {

private String vehicleType;

private String make;

private String model;

private double rentalRatePerDay;

// Constructor

public Vehicle(String vehicleType, String make, String model, double


rentalRatePerDay) {

this.vehicleType = vehicleType;

this.make = make;

this.model = model;

this.rentalRatePerDay = rentalRatePerDay;

// Getters and Setters


public String getVehicleType() {

return vehicleType;

public void setVehicleType(String vehicleType) {

this.vehicleType = vehicleType;

public String getMake() {

return make;

public void setMake(String make) {

this.make = make;

public String getModel() {

return model;

public void setModel(String model) {

this.model = model;

public double getRentalRatePerDay() {

return rentalRatePerDay;

}
public void setRentalRatePerDay(double rentalRatePerDay) {

this.rentalRatePerDay = rentalRatePerDay;

// Method to calculate rental charges

public double calculateRentalCharge(int days) {

return rentalRatePerDay * days;

// Method to provide vehicle details

public String getVehicleDetails() {

return "Vehicle Type: " + vehicleType + ", Make: " + make + ", Model: "
+ model;

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-
default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit


this template

*/

package rentalsystem_composition_casestudy;
/**

* @author Jazib E Nazar

*/

public class Rental {

private Vehicle vehicle;

private Date startDate;

private Date endDate;

// Constructor

public Rental(Vehicle vehicle, Date startDate, Date endDate) {

this.vehicle = vehicle;

this.startDate = startDate;

this.endDate = endDate;

// Method to calculate rental duration in days

public int calculateRentalDuration() {

int [] day=new int[endDate.getDay()];

int counter=0;

for(int i=startDate.getDay();i<=day.length;i++)

counter++;

return counter;

}
// Method to calculate total rental charge

public double calculateTotalRentalCharge() {

int days =calculateRentalDuration() ;

return vehicle.calculateRentalCharge(days);

// Method to provide rental details

public String getRentalDetails() {

return "Rental Details: " + vehicle.getVehicleDetails() + ", Start Date: "


+ startDate + ", End Date: " + endDate;

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-
default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Main.java to edit


this template

*/

package rentalsystem_composition_casestudy;

import java.time.LocalDate;

/**

* @author Jazib E Nazar

*/
public class RENTALSYSTEM_COMPOSITION_CASESTUDY {

/**

* @param args the command line arguments

*/

public static void main(String[] args) {

// TODO code application logic here

Date dS=new Date(1,1,2024);

Date dE=new Date(30,1,2024);

Vehicle car = new Vehicle("Car", "Toyota", "Camry", 50.0);

Vehicle bike = new Vehicle("Bike", "Honda", "CBR", 20.0);

// Creating a customer

Customer customer = new Customer(1, "John Doe",


"john@example.com");

// Renting vehicles

Rental carRental = new Rental(car,dS, dE);

Rental bikeRental = new Rental(bike, dS,dE);

// Adding rentals to the customer

customer.addRental(carRental);

customer.addRental(bikeRental);

// Displaying rental history and total charges for the customer

customer.displayRentalHistory();
System.out.println("Total Charges: $" +
customer.calculateTotalCharges());

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-
default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit


this template

*/

package rentalsystem_composition_casestudy;

/**

* @author Jazib E Nazar

*/

public class Date {

private int day;

private int month;

private int year;

public Date(int day, int month, int year) {

this.day = day;

this.month = month;

this.year = year;

}
public int getDay() {

return day;

public int getMonth() {

return month;

public int getYear() {

return year;

public String toString() {

return String.format("%02d/%02d/%04d", day, month, year);

/*

* Click nbfs://nbhost/SystemFileSystem/Templates/Licenses/license-
default.txt to change this license

* Click nbfs://nbhost/SystemFileSystem/Templates/Classes/Class.java to edit


this template

*/

package rentalsystem_composition_casestudy;

import java.util.ArrayList;

import java.util.List;
/**

* @author Jazib E Nazar

*/

public class Customer {

private int customerId;

private String name;

private String email;

private ArrayList<Rental> rentals;

// Constructor

public Customer(int customerId, String name, String email) {

this.customerId = customerId;

this.name = name;

this.email = email;

this.rentals = new ArrayList<>();

// Method to add a new rental

public void addRental(Rental rental) {

rentals.add(rental);

// Method to calculate total charges for all rentals

public double calculateTotalCharges() {

double totalCharges = 0;
for (Rental rental : rentals) {

totalCharges += rental.calculateTotalRentalCharge();

return totalCharges;

// Method to display rental history

public void displayRentalHistory() {

System.out.println("Rental History for Customer: " + name);

for (Rental rental : rentals) {

System.out.println(rental.getRentalDetails() + ", Total Charge: $" +


rental.calculateTotalRentalCharge());

You might also like