0% found this document useful (0 votes)
29 views

Carrent

This Java project contains code for a car rental system with classes for cars, customers, and a car rental business. The code includes a GUI interface to view and rent cars to customers.
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)
29 views

Carrent

This Java project contains code for a car rental system with classes for cars, customers, and a car rental business. The code includes a GUI interface to view and rent cars to customers.
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/ 7

https://github.

com/yuenci/Java-Car-Rental-System

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import java.io.*;

import java.util.ArrayList;

class Customer {

private String name;

private String gender;

private String address;

public Customer(String name, String gender, String address) {

this.name = name;

this.gender = gender;

this.address = address;

public String getName() {

return name;

public String getGender() {

return gender;

public String getAddress() {

return address;
}

@Override

public String toString() {

return name;

class Car {

private String plateNumber;

private String destination;

private String rentDate;

private String returnDate;

public Car(String plateNumber, String destination, String rentDate, String returnDate) {

this.plateNumber = plateNumber;

this.destination = destination;

this.rentDate = rentDate;

this.returnDate = returnDate;

public String getPlateNumber() {

return plateNumber;

public String getDestination() {

return destination;

}
public String getRentDate() {

return rentDate;

public String getReturnDate() {

return returnDate;

@Override

public String toString() {

return plateNumber;

class CarRental {

private ArrayList<Car> cars;

private ArrayList<Customer> customers;

public CarRental() {

cars = new ArrayList<>();

customers = new ArrayList<>();

public void addCar(Car car) {

cars.add(car);

public void removeCar(int index) {

cars.remove(index);
}

public void addCustomer(Customer customer) {

customers.add(customer);

public void removeCustomer(int index) {

customers.remove(index);

public ArrayList<Car> getCars() {

return cars;

public ArrayList<Customer> getCustomers() {

return customers;

class CarRentalGUI extends JFrame {

private CarRental carRental;

private JComboBox<Car> carList;

private JComboBox<Customer> customerList;

public CarRentalGUI(CarRental carRental) {

this.carRental = carRental;

JLabel carLabel = new JLabel("Select a car:");

carList = new JComboBox<>();


JLabel customerLabel = new JLabel("Select a customer:");

customerList = new JComboBox<>();

JButton rentButton = new JButton("Rent");

JButton returnButton = new JButton("Return");

for (Car car : carRental.getCars()) {

carList.addItem(car);

for (Customer customer : carRental.getCustomers()) {

customerList.addItem(customer);

rentButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

int carIndex = carList.getSelectedIndex();

int customerIndex = customerList.getSelectedIndex();

if (carIndex >= 0 && customerIndex >= 0) {

carRental.removeCar(carIndex);

carRental.removeCustomer(customerIndex);

updateCarList();

updateCustomerList();

});

returnButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

String plateNumber = JOptionPane.showInputDialog("Enter car plate number:");


String destination = JOptionPane.showInputDialog("Enter customer destination:");

String rentDate = JOptionPane.showInputDialog("Enter rent date:");

String returnDate = JOptionPane.showInputDialog("Enter return date:");

carRental.addCar(new Car(plateNumber, destination, rentDate, returnDate));

String name = JOptionPane.showInputDialog("Enter customer name:");

String gender = JOptionPane.showInputDialog("Enter customer gender:");

String address = JOptionPane.showInputDialog("Enter customer address:");

carRental.addCustomer(new Customer(name, gender, address));

updateCarList();

updateCustomerList();

});

setLayout(new GridLayout(4, 2));

add(carLabel);

add(carList);

add(customerLabel);

add(customerList);

add(rentButton);

add(returnButton);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

pack();

setVisible(true);

private void updateCarList() {


carList.removeAllItems();

for (Car car : carRental.getCars()) {

carList.addItem(car);

private void updateCustomerList() {

customerList.removeAllItems();

for (Customer customer : carRental.getCustomers()) {

customerList.addItem(customer);

public class CarRentalSystem {

public static void main(String[] args) {

CarRental carRental = new CarRental();

carRental.addCar(new Car("ABC123", "Destination 1", "2023-05-01", "2023-05-05"));

carRental.addCar(new Car("DEF456", "Destination 2", "2023-05-02", "2023-05-06"));

carRental.addCar(new Car("GHI789", "Destination 3", "2023-05-03", "2023-05-07"));

carRental.addCustomer(new Customer("John Doe", "Male", "123 Main St"));

carRental.addCustomer(new Customer("Jane Smith", "Female", "456 Elm St"));

carRental.addCustomer(new Customer("Mike Johnson", "Male", "789 Oak St"));

CarRentalGUI gui = new CarRentalGUI(carRental);

You might also like