实现两种车型的租赁
输入天数,实现租金
判断与用户选择的汽车是否和公司里面包含的汽车一致
package cn.com.manage;
import cn.com.vehicles.Bus;
import cn.com.vehicles.Car;
import cn.com.vehicles.Vehicle;
//汽车业务类
public class VehicleOperation {
Vehicle[] vehicles = new Vehicle[8];
//汽车信息初始化
public void init() {
//向上转型
vehicles[0] = new Car("京N85764", "宝马", 800, "X6");
vehicles[1] = new Car("京L79654", "宝马", 600, "550i");
vehicles[2] = new Car("京Y96584", "别克", 400, "林荫大道");
vehicles[3] = new Car("京M36589", "别克", 500, "GL8");
vehicles[4] = new Bus("京Y85754", "金龙", 1000, 34);
vehicles[5] = new Bus("京U88888", "金龙", 800, 16);
vehicles[6] = new Bus("京T66666", "金杯", 1200, 34);
vehicles[7] = new Bus("京P90876", "金杯", 700, 16);
}
//租车
public Vehicle rentVenhicle(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;
}
}
主函数,实现用户输入选择,输入选择情况,输出车的信息和租金
package cn.com.manage;
import cn.com.vehicles.Vehicle;
import java.util.Scanner;
//汽车租赁管理类:入口测试类
public class VehicleRent {
public static void main(String[] args) {
Scanner input=new Scanner(System.in);
VehicleOperation vehicleOpr=new VehicleOperation();
System.out.println("**********欢迎光临腾飞汽车租赁有限公司**********");
System.out.println("请选择您要租赁的车型:1.轿车,2.客车");
int vehicleType=input.nextInt();
//获取用户租赁汽车的三个条件:品牌 座位数 型号
String brand="";
int seatCount=0;
String type="";
switch (vehicleType){
case 1://租赁轿车 获取用户想要租赁的轿车品牌及型号信息
System.out.println("请选择你要租赁的轿车品牌: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.println("请选择你要租赁的类型:1.X6,2.550i");
type=(input.nextInt()==1)?"X6":"550i";
}
break;
case 2://租赁客车 获取用户想要租赁的客车品牌及座位数信息
System.out.println("请选择你要租赁的客车品牌:1.金杯,2.金龙");
int choose1=input.nextInt();
if(choose1==1){
brand="金杯";
System.out.print("请选择你要租赁的座位数:1.16座,2.32座");
seatCount=(input.nextInt()==1)?16:34;
}
else {
brand="金龙";
System.out.println("请选择你要租赁的座位数:1.16座,2.32座");
seatCount=(input.nextInt()==1)?16:34;
}
break;
//brand=(input.nextInt()==1)?"金杯":"金龙";
//System.out.println("请选择你要租赁的汽车座位数1.16座,2.34座");
//seatCount=(input.nextInt()==1)?16:34;
}
//初始化汽车信息
vehicleOpr.init();
//租车
Vehicle v=vehicleOpr.rentVenhicle(brand,seatCount,type);
//提示用户租车的车牌号 计算租金(多态,根据具体返回的汽车子类对象,调用重写后的租金方法)
System.out.println("请输入你要租赁汽车的天数");
int days=input.nextInt();
float price=v.calcRent(days);
System.out.println("分配给你的汽车牌号为:"+v.getVehicleId());
System.out.println("你需要支付的租金为:"+price+"元。");
}
}
客车类型:客车租金算法
package cn.com.vehicles;
//客车类
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 calcRent(int days){
//租金=日租金*租赁周期
float price=this.getPerRent()*days;
//折扣规则
if(days>=3&&days<7){
price=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;
}
}
轿车类型:轿车租金算法
package cn.com.vehicles;
//轿车类
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 calcRent(int days){
//租金=日租金*租赁周期
float price=this.getPerRent()*days;
//折扣规则
if(days>7&&days<30){
price=price*0.9f;
}
else if(days>30&&days<=150){
price*=0.8f;
}
else if(days>150){
price*=0.7f;
}
return price;
}
}
父类:实现汽车品牌,汽车型号,日租金
package cn.com.vehicles;
//父类,汽车类
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 calcRent(int days);
}