继承、多态:汽车租赁系统

需求说明:

汽车租赁系统

实现过程
抽象类Moto_Vehilcle

public abstract class Moto_Vehilcle {
    private String no;    //车牌号
    private String brand;   //品牌
    private int perRent;    //日租金
    public Moto_Vehilcle(){

    }
    public Moto_Vehilcle(String no, String brand, int perRent) {
        super();
        this.no = no;
        this.brand = brand;
        this.perRent = perRent;
    }

    public int getPerRent() {
        return perRent;
    }


    public void setPerRent(int perRent) {
        this.perRent = perRent;
    }

    public String getNo() {
        return no;
    }

    public void setNo(String no) {
        this.no = no;
    }

    public String getBrand() {
        return brand;
    }

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

    public abstract float claRent(int days);
}

final轿车类Car 继承自抽象Moto_Vehilcle

public final class Car extends Moto_Vehilcle {
    private String type;//轿车类型
    public Car(){

    }

    public Car(String no, String brand, int perRent,String type) {
        super(no,brand,perRent);
        this.type = type;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }
    /**
     * @Title: claRent
     * @Description: 计算租金-->days>7天9折     days>30天8折   days>150天7折  
     * @param days
     * @return
     * @see four.Moto_Vehilcle.Moto_Vehilcle#claRent(int)
     */
    @Override
    public float claRent(int days) {
        float price =super.getPerRent()*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;
    }

}

final轿车类Bus 继承自抽象Moto_Vehilcle

public class Bus extends Moto_Vehilcle {
    private int setCount;//客车座位号
    public Bus(){

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

    public int getSetCount() {
        return setCount;
    }
    public void setSetCount(int setCount) {
        this.setCount = setCount;
    }
    /**
     * 
     * Title: claRent
     * Description: 计算租金-->days>7天9折     days>30天8折   days>150天7折  
     * @param days
     * @return
     * @see four.Moto_Vehilcle.Moto_Vehilcle#claRent(int)
     */
    @Override
    public float claRent(int days) {
        float price =super.getPerRent()*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;
    }

}

初始化数据并返回对象的中间类Moto_Vehilcle_Service

public class Moto_Vehilcle_Service {
    Moto_Vehilcle[]  motos=new Moto_Vehilcle[8];    //创建8个父类对象数组
    /**
     * 初始化数据
     * @Description: TODO   
     * @return void  
     * @throws
     * @author Young
     * @date 2017年11月25日
     */
    public void init(){
        //轿车的初始化数据
        motos[0]=new Car("京NY28688", "宝马", 880, "林荫大道");
        motos[1]=new Car("京JB38589", "别克", 990, "X9");
        motos[2]=new Car("京DJ52166", "宝马", 600, "GL9");
        motos[3]=new Car("京LI78956", "别克", 300, "500i");
        //汽车的初始化数据
        motos[4]=new Bus(16,"京89888", "金龙", 800);
        motos[5]=new Bus(34,"京85688", "金龙", 1500);
        motos[6]=new Bus(16,"京68974", "金杯", 800);
        motos[7]=new Bus(34,"京96854", "金杯", 1500);
    }
    /*
     * 提供租赁服务方法,
     * 根据用户方法的入参条件去查找相应车辆
     * 入参条件包括车辆品牌,轿车类型,客车座位数
     * 并返回相应车辆。
     * 利用多态概念,用父类作为返回类型。简化代码量
     */
    public Moto_Vehilcle rentService(String brand,String type,int setCount){
        Moto_Vehilcle rentMoto=null;    //初始化父类数据置为空
        for(Moto_Vehilcle moto:motos){
            /**
             * instanceof:比较前面的对象是否为后面的类的实例。
             * 如果遍历出来的moto对象是Car类的实例,则强转为Car类对象
             * 如果Car类的对象与Car类中的品牌和类型一致
             * 把moto对象赋值于父类对象并返回
             */
            if(moto instanceof Car){
                moto=(Car)moto;
                if(brand.equals(moto.getBrand())&&type.equals(((Car) moto).getType())){
                    rentMoto=moto;
                    break;
                }
            }
            if(moto instanceof Bus){
                moto=(Bus)moto;
                if(brand.equals(moto.getBrand())&&((Bus)moto).getSetCount()==setCount){
                    rentMoto=moto;
                    break;
                }
            }
        }
        return rentMoto;
    }

}

汽车租赁系统测试类:

/**
 * 
 *   汽车租赁测试类
 *
 * @Title Moto_Vehicle_Test.java
 * @Package four.Moto_Vehilcle
 * @Description: TODO
 * @author Young
 * @date 2017年11月25日 上午11:43:50
 * @version V1.0
 */
public class Moto_Vehicle_Test {
    public static void main(String[] args) {
        Scanner input=new Scanner(System.in);
        System.out.println("欢迎来到汽车租赁公司!");
        System.out.println("请选择租车类型:1.轿车  2.客车");
        int choose=input.nextInt();
        //给品牌、轿车类型、客车的座位数赋初值
        String brand="";
        String type="";
        int setCount=0;
        boolean flag=false;
        switch (choose) {
        case 1:
            //租赁轿车
            System.out.println("请选择品牌:1.宝马    2.别克");
            int brandChoose=input.nextInt();
            if(brandChoose==1){
                brand="宝马";
                System.out.println("请选择类型:1.林荫大道   2.GL9");
                int typeChoose=input.nextInt();
                type=(typeChoose==1)?"林荫大道":"GL9";
            }else{
                brand="别克";
                System.out.println("请选择类型:1.X9   2.500i");
                int typeChoose=input.nextInt();
                type=(typeChoose==1)?"X9":"500i";
            }
             flag=true;
            break;
        case 2:
            //租赁客车
            System.out.println("请选择品牌:1.金龙    2.金杯");
            brand=(input.nextInt()==1)?"金龙":"金杯";
            System.out.println("请选择座位数:1. 16个座位  2.34个座位");
            int typeChoose=input.nextInt();
            setCount=(typeChoose==1)?16:34;
             flag=true;
            break;

        default:
            System.out.println("抱歉,没有你要租赁的车辆,请等待后续上架.....");
            break;
        }
        if(flag){
            System.out.println("请输入要租赁的天数:");
            int days=input.nextInt();
            Moto_Vehilcle_Service motoSer=new Moto_Vehilcle_Service();
            motoSer.init();
            Moto_Vehilcle moto=motoSer.rentService(brand, type, setCount);
            System.out.println("****************您好,租车结果如下************");
            System.out.println("租车成功!分配给您的车是:"+moto.getNo());
            System.out.println("您应付金额为:"+moto.claRent(days));
        }

    }
}
  • 12
    点赞
  • 56
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
系统名称 汽车租赁系统 carRental 系统概要 汽车租赁系统总共分为两个大的模块,分别是系统模块和业务模块。其中系统模块和业务模块底下又有其子模块。 功能模块 一、业务模块 1、客户管理 客户列表 客户分页和模糊查询 客户添加、修改、删除 导出客户数据 2、车辆管理 车辆列表 车辆车辆分页和模糊查询 车辆添加、修改、删除 3、业务管理 汽车出租 1、根据客户身份证查询所有未出租的车辆信息 2、进行出租 出租单管理 1、多条件的模糊查询和分页 2、出租单的修改、删除、导出 汽车入库 检查单管理 1、多条件模糊查询和分页 2、检查单修改 3、导出检查单 4、统计分析 客户男女比例图 月出租量统计 销售员业绩统计 出租车辆类型统计 二、系统模块 1、用户登陆 校验用户名和密码 登陆成功将登陆信息写入登陆日志 未登录进行拦截 2、菜单管理 全查询菜单和根据左边的树查询不同菜单 菜单的添加、修改、删除 3、角色管理 全查询角色和模糊查询 角色的添加、修改、删除 4、用户管理 全查询用户和模糊查询 用户的添加、修改、删除以及重置密码 5、数据源的监控(druid monitor) 技术选型 后台技术选型 Spring SpringMVC Mybatis 前端技术选型 LayUI、dtree、echarts 开发环境 操作系统:Windows 10 编程语言:Java 开发工具:IDEA、Navicat、Git 项目构建:Maven 3.5.2 服务器:Tomcat 8.5 数据库:MySQL 代码托管平台:GitHub

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值