studyNote_java基础_Day09

能够表达多态的应用场景:
    1.以长辈作为函数或方法的参数
    2.以长辈作为函数或方法的返回值
    

抽象类,接口,多态的收尾案例
    汽车租赁系统
        所有的汽车都具备品牌,车牌号,日租金
    
    大巴车Bus:大巴车有很多座位
    普通轿车Car:有排量
    商务车Mpv:空间大(String );
    
    根据不同车型有不同的计算日租金的方法和租车流程
        大巴车:日租金1000,押金100000,三天起租;
            流程:五年内没有任何扣分的A驾照
        小汽车:日租金400,押金50000,一天起租
            流程:一年内没有任何扣分的C驾照
        商务车:日租金800,押金80000,两天起租
            流程:三年内没有扣分的b驾照
    要求:
        应用多态,简单工厂,键盘录入车型  

实现源码:

接口IVehicle

public interface IVehicle {
		double calcRent();
		boolean leaseOutFlow();
}

 抽象类Vehicle 实现IVehicle接口

public abstract class Vehicle implements IVehicle{
	private String vehicleID;//车牌号
	private String brand ;//品牌
	private int perRent;//日租金
	private int deposit;//押金
	private int beginDayRent;//起租天数
	private int daysOfRent;//租车天数
	public Vehicle(){}
	public Vehicle(String vehicleID, String brand, int perRent, int deposit, int beginDayRent, int daysOfRent) {
		super();
		this.vehicleID = vehicleID;
		this.brand = brand;
		this.perRent = perRent;
		this.deposit = deposit;
		this.beginDayRent = beginDayRent;
		this.daysOfRent = daysOfRent;
	}

	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 int getDeposit() {
		return deposit;
	}

	public void setDeposit(int deposit) {
		this.deposit = deposit;
	}

	public int getBeginDayRent() {
		return beginDayRent;
	}

	public void setBeginDayRent(int beginDayRent) {
		this.beginDayRent = beginDayRent;
	}

	public int getDaysOfRent() {
		return daysOfRent;
	}

	public void setDaysOfRent(int daysOfRent) {
		this.daysOfRent = daysOfRent;
	}
	/**
	 * 所有车型用此种方式计算租金
	 */
	@Override
	public double calcRent() {
		double result = 0;
		result=this.perRent*this.daysOfRent;
		return result;
	}

	@Override
	public  abstract boolean leaseOutFlow() ;
	
}

子类Car继承Vehicle

public class Car extends Vehicle{
	private double dispacement;
	public Car(){}
	public Car(String vehicle,
			String brand,
			int percent,
				int deposit,
				int  beginDayRent,
				int daysOfRent,double dispacement) {
		super(vehicle,brand,percent,deposit,beginDayRent,daysOfRent);
		this.dispacement = dispacement;
	}

	public double getDispacement() {
		return dispacement;
	}

	public void setDispacement(double dispacement) {
		this.dispacement = dispacement;
	}

	@Override
	public boolean leaseOutFlow() {
		// TODO Auto-generated method stub
		boolean flag=false;
		if(this.getDeposit()>=50000 &&
				this.getBeginDayRent()>=1){
			//调用本地交通管理局
			Scanner input=new Scanner(System.in);
			System.out.println("是否是一年内没有扣分的C驾照:");
			String answer= input.next();
			if("y".equals(answer)){
				
				System.out.println("给小轿车的钥匙,可以开走车!");
				flag=true;
			}else {
				System.out.println("驾照不符合要求!");
			}
		}else{
			System.out.println("不符合起租天数");
			return flag;
		}
		return flag;
	}
	
}

子类BUs继承Vehicle

public class Bus extends Vehicle {
	private int sites;
	public Bus(){}
	public Bus(String vehicle,
						String brand,
							int percent,
								int deposit,
								int  beginDayRent,
								int daysOfRent,
								int sites) {
		super(vehicle,brand,percent,deposit,beginDayRent,daysOfRent);
		this.sites = sites;
	}

	public int getSites() {
		return sites;
	}

	public void setSites(int sites) {
		this.sites = sites;
	}

	@Override
	public boolean leaseOutFlow() {
		// TODO Auto-generated method stub
		boolean flag=false;
		if(this.getDeposit()>=100000 &&
				this.getBeginDayRent()>=3){
			//调用本地交通管理局
			Scanner input=new Scanner(System.in);
			System.out.println("是否是五年内没有扣分的A驾照:");
			String answer= input.next();
			if("y".equals(answer)){
				System.out.println("给大巴车的钥匙,可以开走车!");
				flag=true;
			}else {
				System.out.println("驾照不符合要求!");
			}
		}else{
			System.out.println("不符合起租天数");
			return flag;
		}
		return flag;
	}

}

子类Mpv继承Vehicle

public class Mpv extends Vehicle {
	private String space;
	public Mpv(){}
	public Mpv(String vehicle,
			String brand,
			int percent,
				int deposit,
				int  beginDayRent,
				int daysOfRent,String space) {
		super(vehicle,brand,percent,deposit,beginDayRent,daysOfRent);
		this.space = space;
	}
	public String getSpace() {
		return space;
	}
	public void setSpace(String space) {
		this.space = space;
	}
	@Override
	public boolean leaseOutFlow() {
		boolean flag=false;
		if(this.getDeposit()>=80000 &&
				this.getBeginDayRent()>=2){
			//调用本地交通管理局
			Scanner input=new Scanner(System.in);
			System.out.println("是否是三年内没有扣分的B驾照:");
			String answer= input.next();
			if("y".equals(answer)){
				System.out.println("给Mpv的钥匙,可以开走车!");
				flag=true;
			}else {
				System.out.println("驾照不符合要求!");
			}
		}else{
			System.out.println("不符合起租天数");
			return flag;
		}
		return flag;
	}

}

ManageVehicle类管理生产汽车(简单工厂) 

public class ManageVehicle {
	//简单工厂
	public static Vehicle getVehicle(String type){
		Vehicle vehicle=null;
		if("car".equals(type)){
			vehicle=new Car("京A0001","奔驰",400,50000,1,2,2.0);
		}else if("bus".equals(type)){
			vehicle=new Bus("京A0002","黄海",1000,100000,3,2,70);
		}else if("mpv".equals(type)){
			vehicle=new Mpv("京A0003","GMC",800,80000,2,2,"大");
		}
		return vehicle;
	}
	public static double calcRent(IVehicle iv){//静态方便调用
		return iv.calcRent();//计算租金
	}
	public static boolean leaseOutFlow(IVehicle iv){//静态方便调用
		return iv.leaseOutFlow();//租车流程
	}
}

Demo01类实现方法:

public class Demo01 {
	public static void main(String[] args) {
		Scanner input=new Scanner(System.in);
		System.out.println("欢迎光临,汽车租赁");
		System.out.println("1.轿车\t2.大巴车\t3.商务车");
		int choice=input.nextInt();
		String type=null;
		if(choice==1){
			type="car";
		}else if(choice==2){
			type="bus";
		}else if(choice==3){
			type="mpv";
		}else {
			type="car";
		}
		//根据类型生产车辆
		Vehicle vehicle=ManageVehicle.getVehicle(type);
		boolean flag=ManageVehicle.leaseOutFlow(vehicle);
		if(flag){
			System.out.println("请输入租车天数:");
			int days=input.nextInt();
			if(days>=vehicle.getBeginDayRent()){
				vehicle.setDaysOfRent(days);
				double money=ManageVehicle.calcRent(vehicle);
				System.out.println("需要付款:"+money);
			}else{
				System.out.println("天数不够!");
			}
		}
		
	}
}

 

上一篇:Python学习day09 - Python进阶(3)


下一篇:Day09 函数