java---writing car rental system

java—write car rental system

Subject requirements:
1. The car rental information table is as follows:

Insert picture description here
2. Type and attributes:
Insert picture description here
3. Operation effect:
Insert picture description here

Effect realization:
Insert picture description here
Insert picture description here
code realization:

1. Car category:

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);
}

2. Automobile category:

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;
    }
    //重写equals方法
    @Override
    public boolean equals(Vehicle o) {
    
    
        if (o instanceof Cars){
    
    
            Cars cars= (Cars) o;
            return this.getType().equals(cars.getType())&&this.getBrand().equals(o.getBrand());
        }
        return false;
    }
}

3. Passenger cars:

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;
    }
    //重写equals方法
    @Override
    public boolean equals(Vehicle o) {
    
    
        return false;
    }
}

4. Vehicle management:

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");
        Cars[] arr1 ={
    
    c1,c2,c3,c4};
        return arr1;
    }
    //创建客车数组,将汽车的信息放在数组中
    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座");
        Bus[] arr2={
    
    b1,b2,b3,b4};
        return arr2;
    }
}

5. Business service category:

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];        //将车赋值给car
                    break;
                }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.print("分配给你的汽车牌号是:");
            System.out.println(car.getNum());   //获取汽车的车牌
            double totalmoney =0;           //调用total
            totalmoney = car.totalmoney(days, car.getRent());   //计算用户的租金
            System.out.print("你需要支付的租赁分费用是:");
            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];   //将选择的车辆赋值给bus
                    break;
                }
            }
            System.out.print("请输入你要租赁的天数:");
            int days = sc.nextInt();
            System.out.print("分配给你的汽车牌号是:");
            System.out.println();
            System.out.println(bus.getNum());   //拿到用户选择的车牌号
            double totalmoney = 0;      //调用totalmoney方法
            totalmoney=bus.totalmoney(days, bus.getRent());     //用用户输入的天数。来计算租金
            System.out.print("你需要支付的租赁分费用是:");
            System.out.print(totalmoney);
        }
    }
}

6. Test category:

package homework.exam;

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

The content of the console input, I chose the input string type, not in accordance with the renderings, if you do, you can use the ternary operator to achieve it!
When I first received this project, I always felt that I couldn't do it, but I didn't want to admit defeat, so I tried a little bit, and finally I wrote it out. I still feel like a little accomplishment, haha! You should try it too!

Guess you like

Origin blog.csdn.net/weixin_44889894/article/details/111331088
Recommended