java面向对象案例:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示:

车型

具体信息

日租金

折扣

轿车

宝马X6(京NY28588

800

days>79

days>308

days>1507

宝马550i(京CNY3284

600

别克林荫大道(京NT37465

300

别克GL8(京NT96968

600

客车

金杯,16座(京6566754

800

days>=39

days>=78

days>=307

days>=1506

金龙,16座(京8696997

金杯,34座(京9696996

1500

金龙,34座(京8696998

 汽车类:车牌号、品牌、日租金

客车类:车牌号、品牌、日租金、座位数

轿车类:车牌号、品牌、日租金、型号

汽车业务类:忽略

汽车租赁管理类:忽略

父类:MotoVehicle类

public abstract class MotoVehicle {

	private String vehicleId;//车牌号
	private String brand;//品牌
	private double perRent;//日租金
	
	public MotoVehicle() {
		super();
	}

	public MotoVehicle(String vehicleId, String brand, double perRent) {
		super();
		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 double getPerRent() {
		return perRent;
	}

	public void setPerRent(double perRent) {
		this.perRent = perRent;
	}
	
	public abstract double calRent(int days);//计算租金
	
}

子类:Car类

public class Car extends MotoVehicle {

	private String type;//型号
	
	public Car() {
		super();
	}

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

	
	public String getType() {
		return type;
	}

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

	@Override
	public double calRent(int days) {//重写租金
		double price = this.getPerRent()*days;
		if (days>150) {
			price*=0.7;
		}else if (days>30) {
			price*=0.8;
		}else if (days>7) {
			price*=0.9;
		}
		return price;
	}

}

子类:Bus类

public class Bus extends MotoVehicle {

	private int seatCount;//座位数
	
	
	public Bus() {
		super();
	}


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


	public int getSeatCount() {
		return seatCount;
	}


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


	@Override
	public double calRent(int days) {//重写租金
		double price = this.getPerRent()*days;
		if (days>=150) {
			price*=0.6;
		}else if (days>=30) {
			price*=0.7;
		}else if (days>=7) {
			price*=0.8;
		}else if (days>=3) {
			price*=0.9;
		}
		return price;
	}

}

业务类:MotoOperation 类

public class MotoOperation {

	public MotoVehicle[] motos=new MotoVehicle[8];
	
	public void init(){//初始化
		motos[0]=new Car("京NY28588", "宝马", 800, "X6");
		motos[1]=new Car("京CNY3284", "宝马", 600, "550i");
		motos[2]=new Car("京NT37465", "别克", 300, "林荫大道");
		motos[3]=new Car("京NT96968", "别克", 600, "GLB");
		motos[4]=new Bus("京6566754", "金杯", 800, 16);
		motos[5]=new Bus("京8696997", "金龙", 800, 16);
		motos[6]=new Bus("京9696996", "金杯", 1500, 34);
		motos[7]=new Bus("京8696998", "金龙", 1500, 34);
		
	}
	//租赁汽车
	public MotoVehicle motoLeaseOut(String brand,String type,int seat){
		MotoVehicle moto=null;
		for (MotoVehicle mymoto : motos) {
			if (mymoto instanceof Car) {//判断类型
				Car car=(Car) mymoto;
				if (car.getBrand().equals(brand)&&car.getType().equals(type)) {
					moto=car;
					break;
				}
			}else if (mymoto instanceof Bus) {
				Bus bus=(Bus) mymoto;
				if (bus.getBrand().equals(brand)&&bus.getSeatCount()==seat) {
					moto=bus;
					break;
				}
			}
		}
		return moto;
	}
}

测试类:RentMgrSys 类

import java.util.Scanner;

public class RentMgrSys {

	public static void main(String[] args) {
		Scanner sc = new Scanner(System.in);
		MotoOperation motoMgr = new MotoOperation();
		motoMgr.init();
		MotoVehicle moto=null;
		System.out.println("****欢迎光临腾飞汽车租赁有限公司****");
		System.out.println("1、轿车 \t2、客车");
		System.out.println("请输入您要租赁的汽车类型:");
		int choose = sc.nextInt();
		String brand = "";
		String type = "";
		int seat = 0;
		if (choose == 1) {//选择汽车
			System.out.println("请输入您要租赁的汽车品牌:1、别克  \t2、宝马");
			int choose2 = sc.nextInt();
			if (choose2 == 1) {
				brand = "别克";
				System.out.println("请输入您要租赁的汽车类型:1、林荫大道\t2、GLB");
				int choose3 = sc.nextInt();
				type = (choose3 == 1) ? "林荫大道" : "GLB";
			} else if (choose2 == 2) {
				brand = "宝马";
				System.out.println("请输入您要租赁的汽车类型:1、X6 \t2、550i");
				int choose3 = sc.nextInt();
				type = (choose3 == 1) ? "X6" : "550i";
			}

		}else if (choose == 2) {
			System.out.println("请输入您要租赁的客车品牌:1、金杯 \t2、金龙");
			int choose2 = sc.nextInt();
			if (choose2 == 1) {
				brand = "金杯";
				System.out.println("请输入您要租赁的客车座位:1、16座 \t2、34座");
				int choose3 = sc.nextInt();
				seat = (choose3 == 1) ? 16 : 34;
			} else if (choose2 == 2) {
				brand = "金龙";
				System.out.println("请输入您要租赁的汽车类型:1、16座 \t2、34座");
				int choose3 = sc.nextInt();
				seat = (choose3 == 1) ? 16 : 34;
			}

		} 

		moto=motoMgr.motoLeaseOut(brand, type, seat);//获得汽车
		System.out.println("请输入您要租赁的天数:");
		int days=sc.nextInt();
		double sumprice=moto.calRent(days);//费用
		System.out.println("分配给你的汽车牌号是:"+moto.getVehicleId());
		System.out.println("您需要租赁的汽车费用是:"+sumprice+"元");
		sc.close();
	}

}

结果:

 

评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

豪仔思密达

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值