java多态-汽车租赁案例

轿车(子类)

public class Car extends Tool {

	private String model; // 型号

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

	public Car() {
		super();
	}
	public Car(String type, String number, int rent, String model) {
		super(type, number, rent);
		this.model = model;
	}

	@Override
	public double money(int days) {
		double rentdays = super.money(days);
		if (days >= 150) {
			rentdays *= 0.7;
		} else if (days >= 30) {
			rentdays *= 0.8;
		} else if (days >= 7) {
			rentdays *= 0.9;
		}
		return rentdays;
	}
}

 客车(子类)

public class Bus extends Tool {

	private int seatnum; // 座位数

	public int getSeatnum() {
		return seatnum;
	}

	public void setSeatnum(int seatnum) {
		this.seatnum = seatnum;
	}

	public Bus() {
		super();
	}
	public Bus(String type, String number, int rent, int seatnum) {
		super(type, number, rent);
		this.seatnum = seatnum;
	}

	@Override
	public double money(int days) {
		double discount = 1;
		if (days >= 150) {
			discount = 0.6;
		} else if (days >= 30) {
			discount = 0.7;
		} else if (days >= 7) {
			discount = 0.8;
		} else if (days >= 3) {
			discount = 0.9;
		}
		return super.money(days) * discount;
	}
	
}

工具类(父类)

public class Tool {

	private String type; // 种类
	private String number; // 车牌号
	private int rent; // 日租金
	public String getType() {
		return type;
	}
	public void setType(String type) {
		this.type = type;
	}
	public String getNumber() {
		return number;
	}
	public void setNumber(String number) {
		this.number = number;
	}
	public int getRent() {
		return rent;
	}
	public void setRent(int rent) {
		this.rent = rent;
	}
	
	public Tool() {
		super();
	}
	public Tool(String type, String number, int rent) {
		super();
		this.type = type;
		this.number = number;
		this.rent = rent;
	}
	
	public double money(int days) {
		return rent * days;
	}
}

测试类

public class LeaseSystem {

	private static Tool[] toolArr = new Tool[8];
	
	static {
		toolArr[0] = new Bus("金杯", "111", 800, 16);
		toolArr[1] = new Bus("金杯", "222", 1500, 34);
		toolArr[2] = new Bus("金龙", "333", 800, 16);
		toolArr[3] = new Bus("金龙", "444", 1500, 34);
		toolArr[4] = new Car("宝马", "1", 800, "X6");
		toolArr[5] = new Car("宝马", "2", 600, "550i");
		toolArr[6] = new Car("别克", "3", 300, "林荫大道");
		toolArr[7] = new Car("别克", "4", 600, "GL8");
	}
	
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		System.out.println("欢迎光临租车公司");
		System.out.println("1.轿车\t2.客车");
		String model = null;
		String type = null;
		int seatnum = 0;
		int rentdays;
		while (true) {
			System.out.print("请选择你要租赁的汽车类型:");
			String choice = input.next();
			switch (choice) {
			case "1":
				System.out.print("请选择你要租赁的汽车品牌(1.别克2.宝马):");
				type = input.nextInt() == 1 ? "别克" : "宝马";
				if (type.equals("别克")) {
					System.out.print("请选择你要租赁的汽车类型(1.林荫大道2.GL8):");
					model = input.nextInt() == 1 ? "林荫大道" : "GL8";
				} else if (type.equals("宝马")) {
					System.out.print("请选择你要租赁的汽车类型(1.X62.550i):");
					model = input.nextInt() == 1 ? "X6" : "550i";
				}
				
				System.out.print("请输入您要租赁的天数:");
				rentdays = input.nextInt();
				break;
			case "2":
				System.out.print("请选择你要租赁的汽车品牌(1.金龙2.金杯):");
				type = input.nextInt() == 1 ? "金龙" : "金杯";
				System.out.print("请选择你要租赁的汽车类型(1.16座2.34座):");
				seatnum = input.nextInt() == 1 ? 16 : 34;
				System.out.print("请输入您要租赁的天数:");
				rentdays = input.nextInt();
				break;
			default:
				System.out.println("输入错误!");
				continue;
			}
			break;
		}
		LeaseSystem zc = new LeaseSystem();
		Tool tool = zc.getTool(model, type, seatnum);
		if (tool != null) {
			System.out.println("分配给您的汽车牌号是:" + tool.getNumber());
			System.out.println("您需要支付的租赁费用是:" + tool.money(rentdays));
			return;
		}
		System.out.println("没有您想租的车!");
	}
	/*
	 * 租车
	 * Car: 判断车的型号(model):只判断一个是因为Car的每一个类别都不一样
	 * Bus: 判断车的座位数(seatnum),然后再判断车的类别(type)
	 */
	public Tool getTool(String model, String type, int seatnum) {
		for (Tool tool : toolArr) {
			if (tool instanceof Car) {
				Car car = (Car)tool;
					if (car.getModel().equals(model)) {
						return car;
					}
			} else if (tool instanceof Bus) {
				Bus bus = (Bus)tool;
				if (bus.getSeatnum() == seatnum && bus.getType().equals(type)) {
					return bus;
				}
			}
		}
		return null;
	}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值