How To Create a Car Rental System Using Java AWT

In this tutorial, you will learn how to create a car rental system using java AWT. Creating a simple car rental system using Java AWT involves creating a GUI application that allows users to interact with various components such as text fields, buttons and labels to perform tasks like entering customer details, selecting a car, and submitting the rental request.

Car Rental System Using Java AWT

The following steps provides a foundational structure to create a car rental system using Java AWT.

1. Setup the project:

  • Ensure you have Java Development Kit(JDK) installed.
  • Use any IDE like Eclipse or Intelli J IDEA, or a simple text editor and compile using the command line.

2. Code Implementation:

Here’s the complete Java code for a basic car rental system using AWT:

import java.awt.*;
import java.awt.event.*;
public class CarRentalSystem extends Frame implements ActionListener {
    Label nameLabel, carLabel, daysLabel;
    TextField nameField, daysField;
    Choice carChoice;
    Button rentButton;
    TextArea outputArea;
    public CarRentalSystem() {
        setLayout(new FlowLayout());
        nameLabel = new Label("Name:");
        add(nameLabel);
        nameField = new TextField(20);
        add(nameField);
        carLabel = new Label("Car:");
        add(carLabel);
        carChoice = new Choice();
        carChoice.add("Sedan");
        carChoice.add("SUV");
        carChoice.add("Truck");
        carChoice.add("Convertible");
        add(carChoice);
        daysLabel = new Label("Days:");
        add(daysLabel);
        daysField = new TextField(5);
        add(daysField);
        rentButton = new Button("Rent Car");
        add(rentButton);
        rentButton.addActionListener(this);
        outputArea = new TextArea(5, 40);
        add(outputArea);
        setTitle("Car Rental System");
        setSize(450, 300);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        String name = nameField.getText();
        String car = carChoice.getSelectedItem();
        int days = Integer.parseInt(daysField.getText());
        double cost = calculateRentalCost(car, days);
        outputArea.setText("Name: " + name + "\nCar: " + car + "\nDays: " + days + "\nTotal Cost: $" + cost);
    }

    public double calculateRentalCost(String car, int days) {
        double dailyRate;
        switch (car) {
            case "SUV":
                dailyRate = 60;
                break;
            case "Truck":
                dailyRate = 70;
                break;
            case "Convertible":
                dailyRate = 100;
                break;
            case "Sedan":
            default:
                dailyRate = 50;
                break;
        }

        return dailyRate * days;
    }

    public static void main(String[] args) {
        new CarRentalSystem();
    }
}

3. Adding Backend Functionality:

import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;
import java.util.List;
class CarRentalSystem extends Frame implements ActionListener {
    Label nameLabel, carLabel, daysLabel;
    TextField nameField, daysField;
    Choice carChoice;
    Button rentButton;
    TextArea outputArea;
    List<Rental> rentals;
    public CarRentalSystem() {
        setLayout(new FlowLayout());
        nameLabel = new Label("Name:");
        add(nameLabel);
        nameField = new TextField(20);
        add(nameField);
        carLabel = new Label("Car:");
        add(carLabel);
        carChoice = new Choice();
        carChoice.add("Sedan");
        carChoice.add("SUV");
        carChoice.add("Truck");
        carChoice.add("Convertible");
        add(carChoice);
        daysLabel = new Label("Days:");
        add(daysLabel);
        daysField = new TextField(5);
        add(daysField);
        rentButton = new Button("Rent Car");
        add(rentButton);
        rentButton.addActionListener(this);
        outputArea = new TextArea(5, 40);
        add(outputArea);
        rentals = new ArrayList<>();
        setTitle("Car Rental System");
        setSize(450, 300);
        setVisible(true);
    }

    public void actionPerformed(ActionEvent ae) {
        String name = nameField.getText();
        String car = carChoice.getSelectedItem();
        int days = Integer.parseInt(daysField.getText());
        double cost = calculateRentalCost(car, days);
        Rental rental = new Rental(name, car, days, cost);
        rentals.add(rental);
        outputArea.setText("Rental Recorded\n\n" + rental.toString());
    }

    public double calculateRentalCost(String car, int days) {
        double dailyRate;
        switch (car) {
            case "SUV":
                dailyRate = 60;
                break;
            case "Truck":
                dailyRate = 70;
                break;
            case "Convertible":
                dailyRate = 100;
                break;
            case "Sedan":
            default:
                dailyRate = 50;
                break;
        }

        return dailyRate * days;
    }

    public static void main(String[] args) {
        new CarRentalSystem();
    }
}

class Rental {
    private String name;
    private String carType;
    private int days;
    private double cost;
    public Rental(String name, String carType, int days, double cost) {
        this.name = name;
        this.carType = carType;
        this.days = days;
        this.cost = cost;
    }
    public String toString() {
        return "Name: " + name + "\nCar: " + carType + "\nDays: " + days + "\nTotal Cost: $" + cost;
    }
}

4.Improve and Extend:

  • Data Persistence: For a full application, integrate a database to store rental records.
  • Validation: Add input validation to handle edge cases and incorrect inputs.
  • GUI Enhancements: Consider using Swing or JavaFX for more advanced and visually appealing interfaces.
  • Additional Features: Implementing functionalities such as returning cars, checking availability, and user management.

Output:

When you run the application, you will find the following components:

  1. A label and a text field for the customer’s name.
  2. A label and dropdown menu for car type
  3. A label and text field for the number of days.
  4. A “Rent Car” button.
  5. A text area for displaying the rental details.

The text area appears as:

Rental Recorded

Name: Anvitha

Car: SUV

Days: 3

Total cost: $190.0

 

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top