java实现汽车租赁项目

星光不问赶路人,时光不负追梦人

1、汽车抽象类

package vehicle;

//汽车类
public abstract class Automobile {
	// 车牌号 品牌 日租金
	private String licensePlateNumber;
	private String brand;
	private int dailyRent;

	public String getLicensePlateNumber() {
		return licensePlateNumber;
	}

	public void setLicensePlateNumber(String licensePlateNumber) {
		this.licensePlateNumber = licensePlateNumber;
	}

	public String getBrand() {
		return brand;
	}

	public void setBrand(String brand) {
		this.brand = brand;
	}

	public int getDailyRent() {
		return dailyRent;
	}

	public void setDailyRent(int dailyRent) {
		this.dailyRent = dailyRent;
	}

	public Automobile() {
		super();
	}

	public Automobile(String licensePlateNumber, String brand, int dailyRent) {
		super();
		this.licensePlateNumber = licensePlateNumber;
		this.brand = brand;
		this.dailyRent = dailyRent;
	}

	// 计算租金(抽象方法)

	public abstract float calculation(int days);

2.继承汽车抽象类的轿车类

package vehicle;

//轿车类
public class Car extends Automobile {
	// 型号
	private String model;

	public String getModel() {
		return model;
	}

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

	public Car() {
		super();
	}

	public Car(String licensePlateNumber, String brand, int dailyRent,
			String model) {
		super(licensePlateNumber, brand, dailyRent);
		this.model = model;
	}

	@Override
	public float calculation(int days) {
		float price = this.getDailyRent() * days;
		if (days > 7 && days <= 30) {
			price *= 0.9f;
		}
		if (days > 30 && days <= 150) {
			price *= 0.8f;
		}
		if (days > 150) {
			price *= 0.7f;
		}
		return price;
	}

}

3.继承汽车抽象类的客车类

package vehicle;

//客车类
public class Bus extends Automobile {
	// 座位数

	private int seatingCapacity;

	public int getSeatingCapacity() {
		return seatingCapacity;
	}

	public void setSeatingCapacity(int seatingCapacity) {
		this.seatingCapacity = seatingCapacity;
	}

	public Bus() {
		super();
	}

	public Bus(String licensePlateNumber, String brand, int dailyRent,
			int seatingCapacity) {
		super(licensePlateNumber, brand, dailyRent);
		this.seatingCapacity = seatingCapacity;
	}

	@Override
	public float calculation(int days) {
		float price = this.getDailyRent() * days;
		if (days >= 3 && days < 7) {
			price *= 0.9f;
		}
		if (days >= 7 && days < 30) {
			price *= 0.8f;
		}
		if (days >= 30 && days < 150) {
			price *= 0.7f;
		}
		if (days >= 150) {
			price *= 0.6f;
		}
		return price;
	}

}

4、创建汽车业务类

package system;

import vehicle.Automobile;
import vehicle.Bus;
import vehicle.Car;

//汽车业务类
public class Business {
	// 汽车类型的数组,将该数组声明为父类类型
	public Automobile[] motos = new Automobile[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 Automobile carRental(String brand, String model, int seatingCapacity) {
		Automobile moto = null;
		for (Automobile m : motos) {
			if (m instanceof Car) {
				Car car = (Car) m;
				if (car.getBrand().equals(brand)
						&& car.getModel().equals(model)) {
					moto = car;
					break;
				}
			} else {
				Bus bus = (Bus) m;
				if (bus.getBrand().equals(brand)
						&& bus.getSeatingCapacity() == seatingCapacity) {
					moto = bus;
					break;
				}
			}
		}

		return moto;
	}
}

5、创建测试类

package system;

import java.util.Scanner;

import vehicle.Automobile;

//汽车租赁管理类
public class LeaseManagement {

	public static void main(String[] args) {
		Scanner input = new Scanner(System.in);
		Business motoMgr = new Business();
		// 租赁公司首先需要购置汽车
		motoMgr.init();
		System.out
				.println("-----------------------欢迎光临租赁公司----------------------");
		System.out.println("1 轿车\t2 客车");
		System.out.print("请选择你要租赁的汽车类型:");
		int num = input.nextInt();
		String brand = "";// 品牌
		String model = "";// 型号
		int seatingCapacity = 0;// 座位数

		if (num == 1) {
			// 租赁轿车
			System.out.println("请选择您要租赁的桥车品牌:1、别克   2、宝马 ");
			int choose = input.nextInt();
			if (choose == 1) {
				brand = "别克";
				System.out.println("请选择您要租赁的汽车型号:1、林荫大道 2、GLB");
				model = (input.nextInt() == 1) ? "林荫大道" : "GLB";
			} else if (choose == 2) {
				brand = "宝马";
				System.out.println("请选择您要租赁的汽车型号:1、X6 2、550i");
				model = (input.nextInt() == 1) ? "X6" : "550i";
			}
		} else if (num == 2) {
			// 租赁客车
			model = "";
			System.out.println("请选择您要租赁的桥车品牌:1、金杯   2、金龙 ");
			int choose = input.nextInt();
			if (choose == 1) {
				brand = "金杯";
				System.out.println("请选择您要租赁的汽车座位数:1、16座 2、34座");
				seatingCapacity = (input.nextInt() == 1) ? 16 : 34;
			} else if (choose == 2) {
				brand = "金龙";
				System.out.println("请选择您要租赁的汽车座位数:1、16座 2、34座");
				seatingCapacity = (input.nextInt() == 1) ? 16 : 34;
			}
		}
		
		// 租车

		Automobile moto = motoMgr.carRental(brand, model, seatingCapacity);
		System.out.println("请输入您的租赁天数:");
		int days = input.nextInt();
		double money = moto.calculation(days);
		System.out.println("租车成功:请按照如下车牌去提车:" + moto.getLicensePlateNumber());
		System.out.println("您需要支付:" + money + "元");

	}

}

6、实现效果

 

  • 9
    点赞
  • 46
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值