美文网首页
【JAVA学习笔记】汽车租赁系统

【JAVA学习笔记】汽车租赁系统

作者: Geekero | 来源:发表于2021-04-13 08:25 被阅读0次

    学习自华为开发者学院陈璇老师的JAVA系列课程

    一、面向对象设计

    1. 具有相同属性和方法的对象抽取为类

    2. 考虑类的属性和方法

    3. 考虑各个类之间的关系

    4. 搭建好框架后,转化为代码


      需求分析

      汽车业务类的两个方法分析:
    5. 汽车信息初始化
      数组存储汽车信息(汽车:客车 轿车)
      数组类型:父类类型Vehicle

    6. 租赁汽车:返回值为父类类型Vehicle 父类类型作为方法返回值(多态)
      用户需要提供的信息:客车(品牌 座位数)、轿车(品牌、型号) --- 方法参数
      通用的参数:品牌 座位数 型号(客车:品牌 座位数 ""; 轿车:品牌 0 型号)

      方法体:根据用户提供的租车信息(方法参数)去遍历汽车数组,找到相应车辆返回给用户

    租赁天数:在计算租金时使用,在测试类(汽车租赁管理类)中直接使用
    日期类:略

    由于父类写了计算租金的抽象方法,子类也重写过了该方法,所以在测试类中只需要调用父类的计算租金的方法即可,它与租车业务无关。

    二、代码实现

    2.1 汽车相关类

    cn.com.vehicles:
    Vehicle.java

    package cn.com.vehicles;
    
    //父类:汽车类
    public abstract class Vehicle {
        //车牌号 品牌 日租金
        private String vehicleId;
        private String brand;
        private int perRent;
    
        public Vehicle(){}
    
        public Vehicle(String vehicleId, String brand, int perRent) {
            this.vehicleId = vehicleId;
            this.brand = brand;
            this.perRent = perRent;
        }
    
        public String getVehicleId() {
            return vehicleId;
        }
    
        public void setVehicleId(String vehicleId) {
            this.vehicleId = vehicleId;
        }
    
        public String getBrand() {
            return brand;
        }
    
        public void setBrand(String brand) {
            this.brand = brand;
        }
    
        public int getPerRent() {
            return perRent;
        }
    
        public void setPerRent(int perRent) {
            this.perRent = perRent;
        }
    
        //抽象方法:计算租金-->根据租赁周期来计算租金
        public abstract float calcRent(int days);
    }
    

    Car.java

    package cn.com.vehicles;
    
    //轿车类
    public class Car extends Vehicle {
        //型号
        private String type;
    
        public Car(){}
    
        public Car(String vehicleId, String brand, int perRent, String type){
            super(vehicleId, brand, perRent);
            this.type = type;
        }
    
    
        public String getType() {
            return type;
        }
    
        public void setType(String type) {
            this.type = type;
        }
    
        //根据轿车计算租金的规则重写父类方法
        public float calcRent(int days) {
            //租金 = 日租金 * 租赁周期
            float price = this.getPerRent() * days;
            //折扣规则
            if (days>7 && days<=30) {
               price *= 0.9f;  //让计算认为0.9是float
            }else if (days>30 && days<=150){
                price *= 0.8f;
            }else if (days>150) {
                price *= 0.7f;
            }
            return price;
        }
    }
    

    Bus.java

    package cn.com.vehicles;
    
    //客车类
    public class Bus extends Vehicle {
        //座位数
        private int seatCount;
    
        public Bus(){}
    
        public Bus(String vehicleId, String brand, int perRent, int seatCount){
            super(vehicleId, brand, perRent);
            this.seatCount = seatCount;
        }
    
        public int getSeatCount() {
            return seatCount;
        }
    
        public void setSeatCount(int seatCount) {
            this.seatCount = seatCount;
        }
    
        //根据客车计算租金的规则重写父类方法
        public float calcRent(int days) {
            //租金 = 日租金 * 租赁周期
            float price = this.getPerRent() * days;
            //折扣规则
            if (days>=3 && days<7) {
               price *= 0.9f;  //让计算认为0.9是float
            }else if (days>=7 && days<30){
                price *= 0.8f;
            }else if (days>=30 && days<150) {
                price *= 0.7f;
            }else if (days>=150) {
                price *= 0.6f;
            }
            return price;
        }
    }
    

    2.2 汽车业务类

    cn.com.manage

    VehicleOperation.java

    package cn.com.manage;
    
    import cn.com.vehicles.Car;
    import cn.com.vehicles.Vehicle;
    import cn.com.vehicles.Bus;
    
    //汽车业务类
    public class VehicleOperation {
        //汽车数组
        Vehicle[] vehicles = new Vehicle[8];
    
        //汽车信息初始化
        public void init() {
            //向上转型
            //多态的一种使用,父类引用指向子类对象 类似于Vehicle v = new Car(); v = new Bus();
            vehicles[0] = new Car("京N85764","宝马",800,"X6");
            vehicles[1] = new Car("京L79654","宝马",600,"550i");
            vehicles[2] = new Car("京Y96854","别克",400,"林荫大道");
            vehicles[3] = new Car("京M36589","别克",500,"GL8");
            vehicles[4] = new Bus("京Y85764","金龙",1000,34);
            vehicles[5] = new Bus("京U88888","金龙",800,16);
            vehicles[6] = new Bus("京T66666","金杯",1200,34);
            vehicles[7] = new Bus("京P90876","金杯",700,16);
        }
    
        //租车: 简单工厂模式
        //品牌 座位数 型号(客车:品牌 座位数 ""; 轿车:品牌 0 型号)
        public Vehicle rentVehicle(String brand, int seatCount, String type) {
            Vehicle v = null;
            //根据用户提供的租车信息(方法参数)去遍历汽车数组,找到相应车辆返回给用户
            for (Vehicle vehicle:vehicles) {
                if (vehicle instanceof Car) {
                    //轿车
                    Car car = (Car)vehicle; //向下转型
                    //轿车的品牌和型号与用户想要的轿车的品牌和型号吻合
                    if (car.getBrand().equals(brand) && car.getType().equals(type)) {
                        v = car;
                        break;
                    }
                }else{
                    //客车
                    Bus bus = (Bus)vehicle; //向下转型
                    //客车的品牌和座位数与用户想要的轿车的品牌和座位数吻合
                    if (bus.getBrand().equals(brand) && bus.getSeatCount()==seatCount) {
                        v = bus;
                        break;
                    }
                }
            }
            return v;
        }
    }
    

    2.3 汽车租赁管理类

    cn.com.manage

    VehicleRent.java

    package cn.com.manage;
    
    import cn.com.vehicles.Vehicle;
    
    import java.util.Scanner;
    
    //汽车租赁管理类:入口测试类
    public class VehicleRent {
        public static void main(String[] args) {
            Scanner input = new Scanner(System.in);
            VehicleOperation vehicleOpr = new VehicleOperation();
            System.out.println("**************欢迎光临腾飞汽车租赁有限公司*************");
            System.out.print("请选择您需要租赁的车型:1.轿车;2.客车:");
            int vehicleType = input.nextInt();
    
            //获取用户租赁的3个条件: 品牌 座位数 型号
            String brand = "";
            int seatCount = 0;
            String type = "";
            switch (vehicleType) {
                case 1: //租赁轿车 获取到用户想租赁的轿车的品牌和型号信息
                    System.out.print("请选择您要租赁的汽车品牌:1、别克 2、宝马");
                    int choose = input.nextInt();
                    if (choose == 1) {
                        brand = "别克";
                        System.out.print("请选择您要租赁的汽车类型:1、林荫大道 2、GL8");
                        type = (input.nextInt() == 1) ? "林荫大道" : "GL8" ;
                    }else{
                        brand = "宝马";
                        System.out.print("请选择您要租赁的汽车类型:1、X6 2、550i");
                        type = (input.nextInt() == 1) ? "X6" : "550i" ;
                    }
                    break;
                case 2: //租赁客车 获取到用户想租赁的客车的品牌和座位数信息
                    System.out.print("请选择您要租赁的汽车品牌:1、金杯 2、金龙");
                    brand = (input.nextInt() == 1) ? "金杯" : "金龙";
                    System.out.print("请选择您要租赁的汽车座位数:1、16座 2、34座");
                    seatCount = (input.nextInt() == 1) ? 16 : 34;
                    break;
                default:
                    break;
            }
    
            //初始化汽车信息
            vehicleOpr.init();
    
            //租车
            Vehicle v = vehicleOpr.rentVehicle(brand,seatCount,type);
            //提示用户租车的车牌号 计算租金(多态,会根据具体返回的汽车子类对象,返回重写后的计算租金方法)
            System.out.print("请输入您需要租赁的天数:");
            int days = input.nextInt();
            float price = v.calcRent(days);
            System.out.println("分配给您的汽车的牌号为:"+v.getVehicleId());
            System.out.println("您需要的支付的租赁费用为"+price+"元");
        }
    }
    

    相关文章

      网友评论

          本文标题:【JAVA学习笔记】汽车租赁系统

          本文链接:https://www.haomeiwen.com/subject/cwetlltx.html