java面向对象小案例--汽车租赁系统

今日份图片

大家想要什么类型的图片也可以在评论区里打出来
IU
今天给大家带来一个java的小案例,只用java面向对象的知识写。

首先看需求
  • 汽车租赁信息表
    在这里插入图片描述
  • 类和属性
    在这里插入图片描述
  • 运行效果
    在这里插入图片描述
    在这里插入图片描述
分析

首先我们先来看看谁是父类,谁是子类,根据雷和属性这张图,我们知道汽车类有:车牌号、品牌、日租金,轿车类有:车牌号、品牌、型号、日租金,客车类有:车牌号、品牌、日租金、座位数等这些参数,那么我们可以发现轿车类和客车类他们共有的属性是车牌号、品牌和日租金,我们就可以把车牌号、品牌和日租金这三个属性提取到父类中去,也就是汽车类中,租金我们搞一个方法。那么此时轿车类和客车只用去继承父类就可以。

  • 父类 汽车类
public abstract class Vehicles {
    private String vhId;//车牌号
    private String brand;//品牌
    private double dataRent;//日租金
    public Vehicles() {//无参构造
    }
    //全参构造
    public Vehicles(String vhId, String brand, double dataRent) {
        this.vhId = vhId;
        this.brand = brand;
        this.dataRent = dataRent;
    }
    public String getVhId() {
        return vhId;
    }
    public void setVhId(String vhId) {
        this.vhId = vhId;
    }
    public String getBrand() {
        return brand;
    }
    public void setBrand(String brand) {
        this.brand = brand;
    }
    public double getDataRent() {
        return dataRent;
    }
    public void setDataRent(double dataRent) {
        this.dataRent = dataRent;
    }
    public abstract double calRent(int days);//计算租金
}
  • 子类 轿车类
    子类我们去继承父类,此时我们的子类中就有了父类中的那些属性和方法,其中计算租金的方法我们要重写,因为轿车和客车的计算租金的方式不同。
//子类 轿车
public class Car extends Vehicles{
    private String carType;//汽车型号
    public Car(){}//无参构造
    public Car(String vhId, String brand, double dataRent, String carType) {//全参构造
        super(vhId, brand, dataRent);
        this.carType = carType;
    }
    public String getCarType() {
        return carType;
    }
    public void setCarType(String carType) {
        this.carType = carType;
    }
    @Override//重写父类方法
    public double calRent(int days) {
        double price = getDataRent()*days;
        //折扣计算
        if (days > 7 && days <= 30){
            price *= 0.9;
        }else if (days > 30 && days <= 150){
            price *= 0.8;
        }else if (days > 150){
            price *= 0.7;
        }
        return price;
    }
}
  • 子类 客车类
//子类 客车
public class Bus extends Vehicles{
    private int seatNum;//座位数
    public Bus(){}//无参构造

    public Bus(String vhId, String brand, double dataRent, int seatNum) {//全参构造
        super(vhId, brand, dataRent);
        this.seatNum = seatNum;
    }

    public int getSeatNum() {
        return seatNum;
    }

    public void setSeatNum(int seatNum) {
        this.seatNum = seatNum;
    }

    @Override//重写父类方法
    public double calRent(int days) {
        double price = getDataRent()*days;
        //折扣计算
        if (days > 3 && days <= 7){
            price *= 0.9;
        }else if (days > 7 && days <= 30){
            price *= 0.8;
        }else if (days > 30 && days <=150){
            price *= 0.7;
        }else if (days > 150){
            price *= 0.6;
        }
        return price;
    }
}

子类和父类我们都写好了,下面我们要给车辆初始化,也就是赋值了,没有车辆我们写这些也是白搭。我们在此选择一种较为简单易懂的赋值方式,每个类单独赋值,也就是先给轿车赋值再给客车类赋值,或者两个调换过来,这个都没有影响。基础好的同学可以用汽车去给客车和轿车同时赋值(向上向下转型)。下面这两趴都是我们的汽车业务类(VehicleBusiness)。

//汽车初始化 赋值
    static Car [] car = new Car[4];
    static Bus [] bus = new Bus[4];
    public static void init(){
        //轿车
        car[0] = new Car("京NY28588","宝马",800 ,"X6");
        car[1] = new Car("京CNY3284","宝马",600 ,"550i");
        car[2] = new Car("京NT37465","别克",300 ,"林荫大道");
        car[3] = new Car("京NT96968","别克",600 ,"GL8");
        //客车
        bus[0] = new Bus("京6566754","金杯",800,16);
        bus[1] = new Bus("京8696997","金龙",800,16);
        bus[2] = new Bus("京9696996","金杯",1500,34);
        bus[3] = new Bus("京8696998","金龙",1500,34);
    }

我们分析可知,不管是轿车还是客车,最多我们确定其中的两个属性我们就可以确定是哪辆车了,在这我们轿车类取品牌和型号,客车类我们取品牌和座位数

//租轿车
    public Car rentCar(String brand,String type){
        Car c = null;
        for (Car car:car) {
            if (car.getBrand().equals(brand)&&car.getCarType().equals(type)){
                c=car;
                break;
            }
        }
        return c;
    }
    //租客车
    public Bus rentBus(String brand,int seatNum){
        Bus b = null;
        for (Bus bus:bus) {
            if (bus.getBrand().equals(brand)&&bus.getSeatNum()==seatNum){
                b=bus;
                break;
            }
        }
        return b;
    }

下面就该是测试类

  • 测试类
public class TestVehicle {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
       VehicleBusiness vb = new VehicleBusiness();
        System.out.println("**************************欢迎光临秋名山守望者汽车租赁公司***************************");
        System.out.println("1、轿车  2、客车");
        System.out.print("请选择你要租赁的汽车类型:");
        int i = sc.nextInt();
        //获取品牌 座位数  型号
        String brand=null;
        int seatNum = 0;
        String type=null;
        if (i==1){
            System.out.print("请选择你要租赁的汽车品牌:1、别克  2、宝马");
            int carBrand=sc.nextInt();
            if (carBrand==1){
                brand="别克";
                System.out.print("请选择你要租赁的汽车类型:1、GL8  2、林荫大道");
                int choose = sc.nextInt();
                if (choose==1){
                    type="林荫大道";
                }else {
                    type="GL8";
                }
            }else {
                brand="宝马";
                System.out.print("请选择你要租赁的汽车类型:1、X6  2、550i");
                int choose = sc.nextInt();
                if (choose==1){
                    type="X6";
                }else {
                    type="550i";
                }
            }
        }else {
            System.out.print("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
            int BusBrand=sc.nextInt();
            if (BusBrand==1){
                brand="金龙";
                System.out.print("请选择你要租赁的汽车座位数:1、16座  2、34座");
                int choose = sc.nextInt();
                if (choose==1){
                    seatNum=16;
                }else {
                    seatNum=34;
                }
            }else {
                brand="金杯";
                System.out.print("请选择你要租赁的汽车座位数:1、16座  2、34座");
                int choose = sc.nextInt();
                if (choose==1){
                    seatNum=16;
                }else {
                    seatNum=34;
                }
            }
        }
        //最后一步
        //汽车初始化
        VehicleBusiness.init();
        //租车
        if (i==1){
            Car c = vb.rentCar(brand,type);
            System.out.print("请输入您要租赁汽车的天数:");
            double price=c.calRent(sc.nextInt());
            //车牌号
            System.out.println("分配给您的汽车牌号是::"+c.getVhId());
            System.out.println("您需要支付的租赁费用为:"+price+"元。");
        }else {
            Bus b = vb.rentBus(brand,seatNum);
            System.out.print("请输入您要租赁汽车的天数:");
            double price=b.calRent(sc.nextInt());
            //车牌号
            System.out.println("分配给您的汽车牌号是::"+b.getVhId());
            System.out.println("您需要支付的租赁费用为:"+price+"元。");
        }
    }
}

其中在最后一步如果有的同学是选择同时给轿车和客车赋值的话,那么这里写的也不一样,这里要注意。

这样一个简单的用java面向对象写的汽车租赁系统就好了~~~
点个赞

  • 9
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,让我们来实现一个简单的汽车租赁系统。首先,我们需要定义几个来表示不同的对象。 1. `Car`,表示汽车对象,包含以下属性: - `id`:车辆编号 - `brand`:品牌 - `model`:型号 - `pricePerDay`:每天租金 - `isAvailable`:是否可用 并且需要定义一些方法来获取和设置这些属性。 ```java public class Car { private String id; private String brand; private String model; private double pricePerDay; private boolean isAvailable; public Car(String id, String brand, String model, double pricePerDay, boolean isAvailable) { this.id = id; this.brand = brand; this.model = model; this.pricePerDay = pricePerDay; this.isAvailable = isAvailable; } public String getId() { return id; } public String getBrand() { return brand; } public String getModel() { return model; } public double getPricePerDay() { return pricePerDay; } public boolean isAvailable() { return isAvailable; } public void setAvailable(boolean available) { isAvailable = available; } } ``` 2. `Customer`,表示顾客对象,包含以下属性: - `id`:顾客编号 - `name`:名字 - `rentedCar`:租用的汽车对象 并且需要定义一些方法来获取和设置这些属性。 ```java public class Customer { private String id; private String name; private Car rentedCar; public Customer(String id, String name) { this.id = id; this.name = name; } public String getId() { return id; } public String getName() { return name; } public Car getRentedCar() { return rentedCar; } public void setRentedCar(Car rentedCar) { this.rentedCar = rentedCar; } } ``` 3. `CarRentalSystem`,表示汽车租赁系统,包含以下方法: - `addCar`:添加汽车到系统中 - `removeCar`:从系统中删除汽车 - `rentCar`:顾客租用汽车 - `returnCar`:顾客归还汽车 - `getAvailableCars`:获取所有可用的汽车 - `getRentedCars`:获取所有已租用的汽车 - `getCustomers`:获取所有顾客 ```java import java.util.ArrayList; import java.util.List; public class CarRentalSystem { private List<Car> cars; private List<Customer> customers; public CarRentalSystem() { cars = new ArrayList<>(); customers = new ArrayList<>(); } public void addCar(Car car) { cars.add(car); } public void removeCar(Car car) { cars.remove(car); } public void rentCar(Customer customer, Car car) { if (!car.isAvailable()) { System.out.println("Sorry, this car is not available for rent."); return; } customer.setRentedCar(car); car.setAvailable(false); } public void returnCar(Customer customer, Car car) { customer.setRentedCar(null); car.setAvailable(true); } public List<Car> getAvailableCars() { List<Car> availableCars = new ArrayList<>(); for (Car car : cars) { if (car.isAvailable()) { availableCars.add(car); } } return availableCars; } public List<Car> getRentedCars() { List<Car> rentedCars = new ArrayList<>(); for (Car car : cars) { if (!car.isAvailable()) { rentedCars.add(car); } } return rentedCars; } public List<Customer> getCustomers() { return customers; } } ``` 现在我们可以编写一个简单的测试程序来测试我们的汽车租赁系统。 ```java public class CarRentalSystemTest { public static void main(String[] args) { CarRentalSystem carRentalSystem = new CarRentalSystem(); Car car1 = new Car("001", "Toyota", "Corolla", 50.0, true); Car car2 = new Car("002", "Honda", "Civic", 60.0, true); Car car3 = new Car("003", "Mazda", "CX-5", 70.0, true); carRentalSystem.addCar(car1); carRentalSystem.addCar(car2); carRentalSystem.addCar(car3); Customer customer1 = new Customer("001", "John"); Customer customer2 = new Customer("002", "Mary"); carRentalSystem.rentCar(customer1, car1); carRentalSystem.rentCar(customer2, car2); System.out.println("Available cars:"); for (Car car : carRentalSystem.getAvailableCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Rented cars:"); for (Car car : carRentalSystem.getRentedCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Customers:"); for (Customer customer : carRentalSystem.getCustomers()) { System.out.println(customer.getName()); } carRentalSystem.returnCar(customer1, car1); System.out.println("Available cars:"); for (Car car : carRentalSystem.getAvailableCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Rented cars:"); for (Car car : carRentalSystem.getRentedCars()) { System.out.println(car.getBrand() + " " + car.getModel()); } System.out.println("Customers:"); for (Customer customer : carRentalSystem.getCustomers()) { System.out.println(customer.getName()); } } } ``` 输出结果如下: ``` Available cars: Honda Civic Mazda CX-5 Rented cars: Toyota Corolla Customers: John Mary Available cars: Honda Civic Mazda CX-5 Toyota Corolla Rented cars: Customers: John Mary ``` 以上就是一个简单的汽车租赁系统的实现。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值