***汽车租赁,仅供参考适应参考。*

不问归期 提交于 2020-01-15 04:17:22

汽车租赁,仅供参考适应参考。

//汽车业务类
public class VehicleOperation {
//汽车数组
Vehicle[] vehicles = new Vehicle[8];

//汽车信息初始化
public void init(){
	//向上转型
	vehicles[0] = new Car("京N85764","宝马",800,"X6"); //Vehicle v = new Car();
	vehicles[1] = new Car("京L79654","宝马",600,"550i"); //Vehicle v = new Car();
	vehicles[2] = new Car("京Y96584","别克",400,"林荫大道"); //Vehicle v = new Car();
	vehicles[3] = new Car("京M36589","别克",500,"GL8"); //Vehicle v = new Car();
	vehicles[4] = new Bus("京Y85754","金龙",1000,34); //Vehicle v = new Bus();
	vehicles[5] = new Bus("京U88888","金龙",800,16); //Vehicle v = new Bus();
	vehicles[6] = new Bus("京T66666","金杯",1200,34); //Vehicle v = new Bus();
	vehicles[7] = new Bus("京P90876","金杯",700,16); //Vehicle v = new Bus();
}

//租车:简单工厂模式
//参数:品牌   座位数   型号  (客车:品牌  座位数   "";轿车:品牌  0  型号)
public Vehicle rentVehicle(String brand,int seatCount,String type){
	Vehicle v = null;
	//根据用户提供的租车信息(方法参数)去遍历汽车数组,找到相应车辆返回给用户
	for(Vehicle vehicle:vehicles){
		if(vehicle instanceof Car){
			//轿车
			Car car = (Car)vehicle; //向下转型
			//轿车的品牌和型号与用户想要的轿车品牌与型号吻合
			if(car.getBrand().equals(brand) && car.getType().equals(type)){
				v = car;
				break;
			}
		}else{
			//客车
			Bus bus = (Bus)vehicle; //向下转型
			//客车的品牌和座位数与用户想要的客车品牌与座位数吻合
			if(bus.getBrand().equals(brand) && bus.getSeatCount()==seatCount){
				v = bus;
				break;
			}
		}
	}
	return v;
}

}

//汽车租赁管理类:入口测试类
public class VehicleRent {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
VehicleOperation vehicleOpr = new VehicleOperation();
System.out.println(“欢迎光临腾飞汽车租赁有限公司*****”);
System.out.print("请选择您要租赁的车型:1、轿车 2、客车 ");
int vehicleType = input.nextInt();

	//获取用户租赁汽车的三个条件:品牌   座位数   型号
	String brand = "";
	int seatCount = 0;
	String type = "";
	
	switch(vehicleType){
		case 1: //租赁轿车   获取到用户想租赁的轿车的品牌及型号信息
			System.out.print("请选择您要租赁的汽车品牌:1、别克  2、宝马 ");
			int choose = input.nextInt();
			if(choose == 1){
				brand = "别克";
				System.out.print("请选择您要租赁的汽车类型:1、林荫大道  2、GL8");
				type = (input.nextInt() == 1 )?"林荫大道":"GL8";
			}else{
				brand = "宝马";
				System.out.print("请选择您要租赁的汽车类型:1、X6  2、550i");
				type = (input.nextInt() == 1 )?"X6":"550i";
			}
			
			break;
		case 2://租赁客车 获取到用户想租赁的客车的品牌及座位数信息
			System.out.print("请选择您要租赁的汽车品牌:1、金杯  2、金龙 ");
			brand = (input.nextInt() == 1 )?"金杯":"金龙";
			System.out.print("请选择您要租赁的汽车座位数:1、16座  2、34座 ");
			seatCount = (input.nextInt() == 1 )?16:34;
			break;
	}
	
	//初始化汽车信息
	vehicleOpr.init();
	//租车
	Vehicle v = vehicleOpr.rentVehicle(brand, seatCount, type);
	//提示用户租车的车牌号    计算租金(多态,会根据具体返回的汽车子类对象,调用重写后的计算租金方法)
	System.out.print("请输入您要租赁汽车的天数:");
	int days = input.nextInt();
	float price = v.clacRent(days);
	System.out.println("分配给您的汽车牌号为:"+v.getVehicleId());
	System.out.println("您需要支付的租赁费用为:"+price+"元");
}

}

//子类:客车类
public class Bus extends Vehicle {
//座位数
private int seatCount;

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

public int getSeatCount() {
	return seatCount;
}
public void setSeatCount(int seatCount) {
	this.seatCount = seatCount;
}

//根据客车计算租金的规则重写父类方法
public float clacRent(int days) {
	//租金 = 日租金 * 租赁周期
	float price = this.getPerRent() * days;
	//折扣规则
	if(days>=3 && days<7){
		price *=  0.9f;
	}else if(days>=7 &&days<30){
		price *=  0.8f;
	}else if(days>=30 && days<150){
		price *=  0.7f;
	}else if(days>=150){
		price *= 0.6f;
	}
	return price;
}

}

//子类:轿车类
public class Car extends Vehicle {
//型号
private String type;

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

public String getType() {
	return type;
}

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

//根据轿车计算租金的规则重写父类方法
public float clacRent(int days) {
	//租金 = 日租金 * 租赁周期
	float price = this.getPerRent() * days;
	//折扣规则
	if(days>7 && days<=30){
		price *=  0.9f;
	}else if(days>30 &&days<=150){
		price *=  0.8f;
	}else if(days>150){
		price *=  0.7f;
	}
	return price;
}

}

//父类:汽车类
public abstract class Vehicle {
//车牌号 品牌 日租金
private String vehicleId;
private String brand;
private int perRent;

public Vehicle(){}


public Vehicle(String vehicleId, String brand, int perRent) {
	this.vehicleId = vehicleId;
	this.brand = brand;
	this.perRent = perRent;
}


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 abstract float clacRent(int days);

}

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!