亲宝软件园·资讯

展开

Java汽车租赁系统

勤奋的小镇青年、 人气:0

父类Vehicle

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

    //重写equals方法
    public abstract boolean equals(Vehicle v);

    //计算费用
    public abstract double cost(int days,double rent);

    //3个参数的有参构造
    public Vehicle(String num, String brand, double rent) {
        this.num = num;
        this.brand = brand;
        this.rent = rent;
    }

    //无参构造
    public Vehicle() {
    }

    //get,set方法
    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;
    }
}

子类Cars

public class Cars extends Vehicle{
    private String type;

    //重写父类equals方法
    @Override
    public boolean equals(Vehicle v) {
        if(v instanceof Cars){
            Cars c=(Cars) v;
            return this.getBrand().equals(c.getBrand())&&this.getType().equals(c.getType());
        }
        return false;
    }

    //重写父类费用方法
    @Override
    public double cost(int days,double sent) {
        if (days>150){
            return days*sent*0.7;
        }else if (days>30){
            return days*sent*0.8;
        }else if (days>7){
            return days*sent*0.9;
        }else {
            return days*sent;
        }
    }

    //无参构造
    public Cars() {
    }

    //有参构造
    public Cars(String num, String brand, double rent, String type) {
        super(num, brand, rent);
        this.type = type;
    }

    //2个有参构造的方法
    public Cars(String brand,String type){
        super.setBrand(brand);
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
}

子类Bus

public class Bus extends Vehicle{
    private int seat;

    //重写父类的equals方法
    @Override
    public boolean equals(Vehicle v) {
        if(v instanceof Bus){
            Bus b=(Bus) v;
            return this.getBrand().equals(b.getBrand())&&this.getSeat()==b.getSeat();
        }
        return false;
    }

    //重写父类费用方法
    @Override
    public double cost(int days,double rent) {
        if (days>=150){
            return days*rent*0.6;
        }else if (days>=30){
            return days*rent*0.7;
        }else if (days>=7){
            return days*rent*0.8;
        }else if (days>=3){
            return days*rent*0.9;
        }else {
            return days*rent;
        }
    }

    //2个参数的有参构造
    public Bus(String brand,int seat){
        super.setBrand(brand);
        this.seat=seat;
    }

    //子类有参构造
    public Bus(String num, String brand, double rent, int seat) {
        super(num, brand, rent);
        this.seat = seat;
    }

    //子类无参构造
    public Bus(){}

    //子类get set 方法
    public int getSeat() {
        return seat;
    }

    public void setSeat(int seat) {
        this.seat = seat;
    }
}

汽车业务类VehicleServicer

public class VehicleServicer {

    public static List initVehicle(){
        Vehicle v1=new Bus("京6566754","金杯",800,16);
        Vehicle v2=new Bus("京9696996","金杯",1500,34);
        Vehicle v3=new Bus("京8696997","金龙",800,16);
        Vehicle v4=new Bus("京8696998","金龙",1500,34);
        Vehicle c1 =new Cars("京NT37465","别克",300,"林荫大道");
        Vehicle c2 =new Cars("京9696996","别克",600,"GLB");
        Vehicle c3 =new Cars("京8696997","宝马",800,"X6");
        Vehicle c4 =new Cars("京8696998","宝马",600,"550i");

        //先装入数组中
        Vehicle[] ve = {v1,v2,v3,v4,c1,c2,c3,c4};

        //将数组转换成集合
        List<Vehicle> vehicles = Arrays.asList(ve);
        return vehicles;
    }


}

测试类Test

public class Test {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        System.out.println("**********欢迎光临秋明山守望者汽车租赁公司**********");
        System.out.println("1.轿车    2.客车");
        System.out.print("请选择你要租赁的汽车类型:");
        int type = sc.nextInt();
        //桥车
        Vehicle ve;
        String brand;
        if(type==1){
            System.out.print("请选择你要租赁的汽车品牌(1.别克  2.宝马):");
            int pinpai = sc.nextInt();
            String model=pinpai==1?"别克":"宝马";
            if(pinpai==1){
                System.out.print("请输入你要租赁的汽车类型(1.X6  2.550i):");
                int leixin = sc.nextInt();
                brand=leixin==1?"林荫大道":"GL8";
            }else {
                System.out.print("请输入你要租赁的汽车类型(1.X6  2.550i):");
                int leixin = sc.nextInt();
                brand=leixin==1?"X6":"550i";
            }
             ve = new Cars(model, brand);

        }else {//客车
            int seat;
            System.out.print("请选择你要租赁的汽车品牌(1.金龙  2.金杯):");
            int pinpai = sc.nextInt();
            String s=pinpai==1?"金龙":"金杯";
            System.out.print("请选择你要租赁的汽车座位数(1.16座 2.34座):");
            int z = sc.nextInt();
            seat =z==1?16:34;
            ve = new Bus(s, seat);
        }
        //根据选好的车型,输出车牌和总价
        List<Vehicle> list = VehicleServicer.initVehicle();
        for (Vehicle v:list) {
            if(ve.equals(v)){
                System.out.print("请输入你要租赁的天数:");
                int days = sc.nextInt();
                System.out.println("分配给您的汽车牌号是:"+v.getNum());
                System.out.println("您需要支付的租赁费用是:"+v.cost(days,v.getRent()));
            }
        }

    }
}

加载全部内容

相关教程
猜你喜欢
用户评论