Backend Development 10 min read

Java Vehicle Rental System Implementation with Classes, Inheritance, and Service Logic

This article presents a complete Java console application for a vehicle rental system, detailing class designs for vehicles, cars, buses, a management class, service logic for renting, and a test driver, along with full source code examples.

Java Captain
Java Captain
Java Captain
Java Vehicle Rental System Implementation with Classes, Inheritance, and Service Logic

The article describes a Java console‑based vehicle rental system, starting from the assignment requirements that include a car information table, class specifications, and expected program output.

Vehicle abstract class defines common fields (number, brand, rent) with getters, setters, constructors, a toString method, and abstract methods totalmoney and equals for subclasses.

package homework.exam;

public abstract class Vehicle {
    private String num;
    private String brand;
    private double rent;

    public String getNum() { return num; }
    public void setNum(String num) { this.num = num; }
    public String getBrand() { return brand; }
    public void setBrand(String brand) { this.brand = brand; }
    public double getRent() { return rent; }
    public void setRent(double rent) { this.rent = rent; }
    public Vehicle() {}
    //含参构造
    public Vehicle(String num, String brand, double rent) {
        this.num = num; this.brand = brand; this.rent = rent;
    }
    @Override
    public String toString() {
        return "汽车{" + "车牌号='" + num + '\'' + ", 品牌='" + brand + '\'' + ", 日租金=" + rent + '}';
    }
    public abstract double totalmoney(int days , double rent);
    public abstract boolean equals(Vehicle o);
}

Cars class extends Vehicle , adds a type attribute, implements the rental‑price calculation with tiered discounts, and overrides equals to compare type and brand.

package homework.exam;

public class Cars extends Vehicle {
    private String type;
    public String getType() { return type; }
    public void setType(String type) { this.type = type; }
    public Cars(String brand, String type) { this.type = type; }
    public Cars(String num, String brand, double rent, String type) {
        super(num, brand, rent); this.type = type; }
    @Override
    public String toString() { return "Cars{" + "type='" + type + '\'' + '}'; }
    @Override
    public double totalmoney(int days, double rent) {
        if (days > 7) return days*rent*0.9;
        else if (days > 30) return days*rent*0.8;
        else if (days > 150) return days*rent*0.7;
        return days*rent; }
    @Override
    public boolean equals(Vehicle o) {
        if (o instanceof Cars) {
            Cars c = (Cars) o;
            return this.getType().equals(c.getType()) && this.getBrand().equals(o.getBrand());
        }
        return false; }
}

Bus class also extends Vehicle , adds a seat field, and provides its own discount rules for longer rentals.

package homework.exam;

public class Bus extends Vehicle {
    private String seat;
    public String getSeat() { return seat; }
    public void setSeat(String seat) { this.seat = seat; }
    public Bus(String num, String brand, double rent, String seat) {
        super(num, brand, rent); this.seat = seat; }
    @Override
    public double totalmoney(int days, double rent) {
        if (days >= 3) return days*rent*0.9;
        else if (days >= 7) return days*rent*0.8;
        else if (days >= 30) return days*rent*0.7;
        else if (days >= 150) return days*rent*0.6;
        return days*rent; }
    @Override
    public boolean equals(Vehicle o) { return false; }
}

CarRent class creates sample arrays of Cars and Bus objects to simulate a small inventory.

package homework.exam;

public class CarRent {
    public Cars[] carMake() {
        Cars c1 = new Cars("京NY28588", "宝马", 800, "x6");
        Cars c2 = new Cars("京CNY3284", "宝马", 600, "550i");
        Cars c3 = new Cars("京NT37465", "别克", 300, "林荫大道");
        Cars c4 = new Cars("京NT96928", "别克", 600, "GL8");
        return new Cars[]{c1,c2,c3,c4};
    }
    public Bus[] busMake() {
        Bus b1 = new Bus("京6566754", "金杯", 800, "16座");
        Bus b2 = new Bus("京8696667", "金龙", 800, "16座");
        Bus b3 = new Bus("京9696996", "金杯", 1500, "34座");
        Bus b4 = new Bus("京8696998", "金龙", 1500, "34座");
        return new Bus[]{b1,b2,b3,b4};
    }
}

CarService class implements the interactive console workflow: it prompts the user to choose vehicle type and brand, searches the appropriate array, asks for rental days, and computes the total cost using the overridden totalmoney methods.

package homework.exam;

import java.util.Scanner;

public class CarService {
    public void rentcar() {
        System.out.println("**********欢迎光临秋名山守望者汽车租赁公司**********");
        Scanner sc = new Scanner(System.in);
        System.out.println("1,轿车  2,客车");
        System.out.print("请输入您要租赁的汽车类型:");
        int i = sc.nextInt();
        CarRent carRent = new CarRent();
        Cars[] cars = carRent.carMake();
        Cars car = null;
        Bus[] buses = carRent.busMake();
        Bus bus = null;
        if (i == 1) {
            System.out.print("请选择你要租赁的汽车品牌:(1,别克  2,宝马)");
            int i1 = sc.nextInt();
            if (i1 == 1) System.out.print("请输入你要租赁的汽车类型:(1,林荫大道 2,GL8 )");
            else System.out.print("请输入你要租赁的汽车类型:(1,x6 2,550i )");
            String i2 = sc.next();
            for (int j = 0; j < cars.length; j++) {
                if (cars[j].getType().equals(i2)) { car = cars[j]; break; }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.println("分配给你的汽车牌号是:" + car.getNum());
            double totalmoney = car.totalmoney(days, car.getRent());
            System.out.print("你需要支付的租赁分费用是:" + totalmoney);
        } else if (i == 2) {
            System.out.print("请选择你要租赁的汽车品牌:(1,金龙  2,金杯)");
            String i2 = sc.next();
            System.out.print("请输入你要租赁的汽车座位数:(1,16座  2,34座)");
            String i3 = sc.next();
            for (int j = 0; j < buses.length; j++) {
                if (buses[j].getBrand().equals(i2) && buses[j].getSeat().equals(i3)) { bus = buses[j]; break; }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.println("分配给你的汽车牌号是:" + bus.getNum());
            double totalmoney = bus.totalmoney(days, bus.getRent());
            System.out.print("你需要支付的租赁分费用是:" + totalmoney);
        }
    }
}

Test class contains the main method that launches the rental service.

package homework.exam;

public class Test {
    public static void main(String[] args) {
        CarService cs = new CarService();
        cs.rentcar();
    }
}

The author notes that the console input in the demonstration uses string literals instead of the exact UI shown in the assignment, and suggests that a ternary operator could be used to simplify some of the conditional logic.

backendJavaoopConsole ApplicationVehicle Rental
Java Captain
Written by

Java Captain

Focused on Java technologies: SSM, the Spring ecosystem, microservices, MySQL, MyCat, clustering, distributed systems, middleware, Linux, networking, multithreading; occasionally covers DevOps tools like Jenkins, Nexus, Docker, ELK; shares practical tech insights and is dedicated to full‑stack Java development.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.