第五章 汽车租赁系统

 

 

1.创建汽车父类 Car

//父类汽车类
public abstract class Car {
    protected String carId;   //车牌号
    protected String brand;   //品牌
    protected int dayPrice;   //日租金

    public Car(String carId, String brand, int dayPrice) {
        this.carId = carId;
        this.brand = brand;
        this.dayPrice = dayPrice;
    }

    public Car(){}

    public String getCarId() {
        return carId;
    }

    public void setCarId(String carId) {
        this.carId = carId;
    }

    public String getBrand() {
        return brand;
    }

    public void setBrand(String brand) {
        this.brand = brand;
    }

    public int getDayPrice() {
        return dayPrice;
    }

    public void setDayPrice(int dayPrice) {
        this.dayPrice = dayPrice;
    }

    //计算租金方法
    public abstract double rentPrice(int day);
    //汽车类型
    public abstract void type();
}

 2.创建子类轿车类

import java.util.Scanner;

//子类轿车类
public class Sedan extends Car{

    private String type;    //型号

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

    public Sedan(String carId, String brand, int dayPrice, String type) {
        super(carId, brand, dayPrice);
        this.type = type;
    }

    public Sedan(){}

    @Override
    public double rentPrice(int day) {
        double price = getDayPrice()*day;
        if (day > 7 && day <= 30){
            price = price*0.9;
        }else if(day > 30 && day <= 150){
            price = price*0.8;
        }else if(day > 150){
            price = price*0.7;
        }
        System.out.println("共租"+day+"天,"+"您需要支付的总费用为:"+price+"元。");
        return price;
    }

    @Override
    public void type(){
        Scanner sc = new Scanner(System.in);
        System.out.print("请选择汽车品牌:(1、别克\t2、宝马)");
        int sBrand = sc.nextInt();
        if (sBrand == 1){
            //别克
            setBrand("别克");
            System.out.print("请选择汽车类型:(1、林荫大道\t2、GL8)");
            int sType = sc.nextInt();
            if (sType == 1){
                setType("林荫大道");
            }else {
                setType("GL8");
            }
        }else {
            setBrand("宝马");
            System.out.print("请选择汽车类型:(1、X6\t2、550i)");
            int sType = sc.nextInt();
            if (sType == 1){
                setType("X6");
            }else {
                setType("550i");
            }
        }
    }
}

 3.创建子类客车类

import java.util.Scanner;

//子类客车类
public class Bus extends Car{
    private int setCount;   //座位数

    public Bus(String carId, String brand, int dayPrice, int setCount) {
        super(carId, brand, dayPrice);
        this.setCount = setCount;
    }
    public Bus() {}

    public int getSetCount() {
        return setCount;
    }

    public void setSetCount(int setCount) {
        this.setCount = setCount;
    }

    @Override
    public double rentPrice(int day) {
        double price = day*getDayPrice();
        if (day >= 3 && day < 7){
            price = price*0.9;
        }else if(day >= 7 && day <30){
            price =price*0.8;
        }else if(day >= 30 && day <150){
            price = price*0.7;
        }else if(day >= 150){
            price = price*0.6;
        }
        System.out.println("共租"+day+"天,"+"您需要支付的总费用为:"+price+"元。");
        return price;
    }

    @Override
    public void type(){
        Scanner sc = new Scanner(System.in);
        //客车
        System.out.print("请选择汽车品牌:(1、金龙\t2、金杯)");
        int bBrand = sc.nextInt();
        if (bBrand == 1){
            //金龙
            setBrand("金龙");
            System.out.print("请选择汽车类型:(1、16座\t2、34座)");
            int bNum = sc.nextInt();
            if (bNum == 1){
                setSetCount(16);
            }else {
                setSetCount(34);
            }
        }else {
            setBrand("金杯");
            System.out.print("请选择汽车类型:(1、16座\t2、34座)");
            int bNum = sc.nextInt();
            if (bNum == 1){
                setSetCount(16);
            }else {
                setSetCount(34);
            }
        }
    }
}

 4.创建汽车服务类

public class CarService{
    //初始化
    private static Bus[] bus = new Bus[4];
    private static Sedan[] sedans = new Sedan[4];
    public static void init(){
        //轿车
        sedans[0] = new Sedan("京NY28588", "宝马", 800, "X6");
        sedans[1] = new Sedan("京CNY3284", "宝马", 600, "550i");
        sedans[2] = new Sedan("京NT37465", "别克", 300, "林荫大道");
        sedans[3] = new Sedan("京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 getBrand(int num){
        if (num == 1){
            return new Sedan();
        }else if(num == 2){
            return new Bus();
        }
        return null;
    }

    //获取客车品牌和座位数
    public Bus rentBus(String brand,int setNum){
        Bus buses = null;
        for (Bus b:bus){
            if (brand.equals(b.getBrand()) && b.getSetCount() == setNum){
                buses = b;
                break;
            }
        }
        return buses;
    }
    //获取轿车品牌和类型
    public Sedan rentSedan(String brand,String type){
        Sedan sedan = null;
        for (Sedan s:sedans){
            if (s.getBrand().equals(brand) && s.getType().equals(type)){
                sedan = s;
                break;
            }
        }
        return sedan;
    }

}

5.测试类


import java.util.Scanner;

public class TestCar {
    public static void main(String[] args) {
        Sedan sedan = new Sedan();
        Bus bus = new Bus();
        //创建CarService对象
        CarService carService = new CarService();
        //初始化汽车信息
        carService.init();
        Scanner sc = new Scanner(System.in);
        //显示所有数据
        System.out.println("*********欢迎来到汽车租赁公司**********");
        System.out.println("1.轿车\t2.客车");
        System.out.print("请选择要租赁的车型:");
        int choose = sc.nextInt();
        Car car = carService.getBrand(choose);
        if ( car instanceof Sedan){
             sedan =(Sedan) car;
             sedan.type();
        }
        if ( car instanceof Bus){
             bus =(Bus) car;
             bus.type();
        }
        //租车
        if (choose == 1){
            Sedan s = carService.rentSedan(sedan.getBrand(), sedan.getType());
            System.out.print("请输入要租赁的天数:");
            int days = sc.nextInt();
            System.out.println("租赁成功!\n您的汽车为:" + s.getCarId()+"的"+s.getBrand()+s.getType());
            s.rentPrice(days);
        }else {
            Bus b = carService.rentBus(bus.getBrand(),bus.getSetCount());
            System.out.print("请输入要租赁的天数:");
            int days = sc.nextInt();
            System.out.println("租赁成功!\n您的汽车为:"+b.getCarId()+"的"+b.getBrand()+b.getSetCount()+"座");
            b.rentPrice(days);
        }
    }
}

运行结果:

  • 7
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值