继承、多态:汽车租赁系统
来源:互联网 发布:淘宝二手ipad 哪家靠谱 编辑:程序博客网 时间:2024/05/01 13:19
需求说明:
实现过程
抽象类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)); } }}
阅读全文
0 0
- 继承、多态:汽车租赁系统
- 汽车租赁管理系统
- 汽车租赁系统
- 汽车租赁系统
- 汽车租赁系统
- 汽车租赁管理系统
- 汽车租赁系统
- 汽车租赁系统
- 汽车租赁系统
- 汽车租赁系统项目
- java汽车租赁系统代码
- JavaOOP 继承实现汽车租赁计算价格
- 金石汽车租赁管理系统 下载
- 汽车租赁管理系统-期末项目
- 实现汽车租赁系统的计价功能
- 深入.NET 第十章 汽车租赁系统
- C# 第十章 汽车租赁系统
- 汽车租赁系统的简单实现
- 一个特殊的中断:SVCall
- 安卓UI 自定义View SwitchButton
- AttributeError: 'NoneType' object has no attribute 'startswith'
- QT 使用QSetting读取配置文件中的中文乱码解决方案
- 大小端判断
- 继承、多态:汽车租赁系统
- SQL语句中对基本表的操作(DDL)
- iOS xcode里面鼠标右击菜单栏~翻译(基础)
- 1、TensorFlow 的安装
- 机器学习-线性回归
- 无环有向图最长路径
- 人脸识别之人脸检测(十二)--DDFD算法
- 机房收费系统之用户登录
- 程序人生