简单汽车租赁系统JAVA程序

//车类
public abstract class Vehicle {
	//车牌号    品牌    日租金
	private String id;
	private String brand;
	private int perRent;
	
	public Vehicle(){}
	//Vehicle的带参构造方法
	public Vehicle(String id, String brand, int perRent) {
		this.id = id;
		this.brand = brand;
		this.perRent = perRent;
	}

	public void setId(String id){
		this.id=id ;
	}
	public String getId(){
		return id;
	}
	
	public void setBrand(String brand){
		this.brand=brand;
	}
	public String getBrand(){
		return brand;
	}
	
	public void setPerRent(int perRent){
		this.perRent=perRent;
	}
	public int getPerRent(){
		return perRent;
	}
	//抽象方法计算租金
	public abstract double calcRent(int days);
	
}

//轿车类
public class Car extends Vehicle{
	//型号
	private String type;
	public void setType(String type){
		this.type=type;
	}
	public String getType(){
		return type;
	}
	
	public Car(){}
	//Car的带参构造方法
	public Car(String id, String brand, int perRent,String type) {
		super(id,brand,perRent);
		this.type = type;
	}
	//重写父类的计算租金方法:根据自己的计算租金规则
	public double calcRent(int days) {
		double price = this.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;
	}
}

//客车类
public class Bus extends Vehicle{
	//座位数
	private int seatCount;
	public void setSeatCount(int seatCount){
		this.seatCount=seatCount;
	}
	public int getSeatCount(){
		return seatCount;
	}
	

	public Bus(){}
	//Bus的带参构造方法
	public Bus(String id,String brand,int perRent,int seatCount){
		super(id,brand,perRent);
		this.seatCount = seatCount;
	}
	//重写父类的计算租金方法:根据自己的计算租金规则
	public double calcRent(int days) {
		double price = this.getPerRent()*days;
		if(days>=3 && days<7){
			price *= 0.9;
		}else if(days>=7 && days<30){
			price *= 0.8;
		}else if(days>=30 && days<150){
			price *= 0.7;
		}else if(days>150){
			price *= 0.6;
		}
		return price;
		
	}
}

//汽车业务类
public class Operation {
	public Vehicle[] vehicle = new Vehicle[8];
//初始化汽车信息
	public void init(){
		vehicle[0] = new Car("京NY28588","宝马",800,"X6");   //vehicle v = new Car();
		vehicle[1] = new Car("京CNY32584","宝马",600,"550i");   //vehicle v = new Car();
		vehicle[2] = new Car("京NT37465","别克",300,"林荫大道");   //vehicle v = new Car();
		vehicle[3] = new Car("京NT96968","别克",600,"GL8");   //vehicle v = new Car();
		vehicle[4] = new Bus("京6566754","金杯",800,16);   //vehicle v = new Bus();
		vehicle[5] = new Bus("京8696997","金龙",800,16);   //vehicle v = new Bus();
		vehicle[6] = new Bus("京9696996","金杯",1500,34);   //vehicle v = new Bus();
		vehicle[7] = new Bus("京8696998","金龙",1500,34);   //vehicle v = new Bus();
	}
	//租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
	//如果租赁的是轿车   需要条件:品牌        型号
	//如果租赁的是客车   需要条件:品牌        座位数
	//简单工厂模式
	public Vehicle zuChe(String brand,String type,int seatCount){
		Vehicle che = null;
		//for循环遍历数组vehicle
		for(Vehicle myche : vehicle){
			//判断Vehicle类的myche的类型是否和Car一样
			if(myche instanceof Car){
				//Vehicle类的myche向下转型变成子类Car
				Car car = (Car)myche;
				if(car.getBrand().equals(brand) && car.getType().equals(type)){
					che=car;
					break;
				}
			}else{
				//Vehicle类的myche向下转型变成子类Bus
				Bus bus = (Bus)myche;
				if(bus.getBrand().equals(brand) && bus.getSeatCount() == seatCount){
					che=bus;
					break;
				}
			}
		}
		return che;
		
	}
}

//汽车租赁
public class Rent {
	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Operation operation = new Operation();
		//租赁公司界面
		operation.init();
		System.out.println("**********欢迎光临租赁公司**********");
		System.out.println("1.轿车\t\t2.客车");
		System.out.println("请选择您要租赁的汽车类型:(1.轿车    2.客车)");
		int cheType = input.nextInt();
		String brand = "";//品牌
		String type = "";//型号
		int seatCount = 0;//座位数
		//收集用户条件
		if(cheType == 1){
			//租赁轿车
			System.out.println("请选择您要租赁的轿车品牌:(1.别克    2.宝马)");
			int choose = input.nextInt();
			if(choose == 1){
				brand="别克";
				System.out.println("请选择您要租赁的汽车型号:(1.林荫大道    2.GL8)");
				type=(input.nextInt() == 1)?"林荫大道":"GL8";
			}else if(choose == 2){
				brand="宝马";
				System.out.println("请选择您要租赁的汽车型号:(1.X6   2.550i)");
				type=(input.nextInt() == 1)?"X6":"550i";
			}
			
		}else if(cheType == 2){
			//租赁客车
			type= "";
			System.out.println("请选择您要租赁的客车品牌:(1.金杯    2.金龙)");
				brand=(input.nextInt()==1)?"金杯":"金龙";
				System.out.println("请选择您要租赁的客车座位数:(1.16座    2.32座)");
				seatCount=(input.nextInt() == 1)?16:34;
			}
		//租车
		Vehicle che = operation.zuChe(brand, type, seatCount);
		System.out.println("请输入您的租赁天数:");
		int days = input.nextInt();
		double money = che.calcRent(days);
		System.out.println("租车成功,请按照如下车牌号提车:"+che.getId());
		System.out.println("您需要支付:"+money+"元");
	}

}

猜你喜欢

转载自blog.csdn.net/baidu_29343517/article/details/81153722
今日推荐