租车信息:

Java代码~~汽车租赁系统-LMLPHP

输出结果:

Java代码~~汽车租赁系统-LMLPHP

Java代码~~汽车租赁系统-LMLPHP

Java代码~~汽车租赁系统-LMLPHP

代码:

1、先定义抽象类(汽车类:Moto)

 package cn.aura.demo01;

 public abstract class Moto {
//公共属性
private String id;//车牌号
private String brand;//品牌
private int preRent;//日租金
//构造方法
public Moto(String id, String brand, int preRent) {
this.id = id;
this.brand = brand;
this.preRent = preRent;
}
//set和get方法
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getBrand() {
return brand;
}
public void setBrand(String brand) {
this.brand = brand;
}
public int getPreRent() {
return preRent;
}
public void setPreRent(int preRent) {
this.preRent = preRent;
}
//计算租金方法
public abstract double calRent(int days);
}

2.定义轿车类继承汽车类

 package cn.aura.demo01;
/**
* 轿车类
*/
public class Car extends Moto{
//私有属性
private String type;//型号
//set和get方法
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
//构造方法
public Car(String id, String brand, String type,int preRent) {
super(id, brand, preRent);
this.type = type;
} //重写计算租金的算法
@Override
public double calRent(int days) {
if(days >0 && days <= 7) {
//days小于7天,不打折
return getPreRent()*days;
}else if (days > 7 && days <= 30) {
//days大于7天,9折
return getPreRent()*days*0.9;
}else if (days > 30 && days <= 150) {
//days大于30天,8折
return getPreRent()*days*0.8;
}else if (days > 150) {
//days大于150天,7折
return getPreRent()*days*0.7;
}
return 0;
}
}

3.定义客车类

 package cn.aura.demo01;
/**
* 客车类
*/
public class Bus extends Moto{
//私有属性
private int seatCount;//座位数
//set和get方法
public int getSeatCount() {
return seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
//构造方法
public Bus(String id, String brand, int seatCount, int preRent) {
super(id, brand, preRent);
this.seatCount = seatCount;
}
//重写计算租金的算法
@Override
public double calRent(int days) {
if(days >0 && days <3) {
//days小于3天,不打折
return getPreRent()*days;
}else if (days >= 3 && days < 7) {
//days大于3天,9折
return getPreRent()*days*0.9;
}else if (days >= 7 && days <30) {
//days大于3天,8折
return getPreRent()*days*0.8;
}else if (days >= 30 && days <150) {
//days大于30天,7折
return getPreRent()*days*0.7;
}else if (days >= 150) {
//days大于150天,6折
return getPreRent()*days*0.6;
}
return 0;
}
}

4.定义汽车业务类

 package cn.aura.demo01;
/**
* 定义汽车业务类
*/
public class MotoOperation {
//初始化数组
public static Moto[] motos = new Moto[8];
//创建8个汽车对象
public void init() {
motos[0] = new Car("京NY28558", "宝马", "X6", 800);
motos[1] = new Car("京CNY3284", "宝马", "550i", 600);
motos[2] = new Car("京NT37465", "别克", "林荫大道", 300);
motos[3] = new Car("京NT96968", "别克", "GL8", 600);
motos[4] = new Bus("京6566754", "金杯", 16, 800);
motos[5] = new Bus("京8696997", "金龙", 16, 800);
motos[6] = new Bus("京9696996", "金杯", 34, 1500);
motos[7] = new Bus("京8696998", "金龙", 34, 1500);
}
//租赁汽车的方法
public Moto motoLeaseOut(String brand,String type,int seat) {
//for循环遍历数组motos
for(int i=0;i<motos.length;i++){
if (brand.equals(motos[i].getBrand())) {
// 判断Moto类的motos[i]的类型是否和Car一样
if (motos[i] instanceof Car) {
// 强转成小汽车Car
Car car = (Car) motos[i];
if (type.equals(car.getType())) {
return car;
}
}
if(motos[i] instanceof Bus){
// 强转成大客车Bus
Bus bus = (Bus) motos[i];
if (seat == bus.getSeatCount()) {
return bus;
}
}
} }
//如果没有就返回空
return null;
}
}

5.定义测试类

 package cn.aura.demo01;

 import java.util.Scanner;

 /**
* 汽车租赁管理类
*/
public class TestRent {
//主函数
public static void main(String[] args) {
MotoOperation motoOperation = new MotoOperation();
// 初始化汽车
motoOperation.init();
//设置一个空容器
Moto moto = null;
// 提示信息
System.out.println("***********欢迎光临腾飞汽车租赁公司***********");
System.out.println("1、轿车 2、客车");
System.out.println("请输入你要租赁的汽车类型:");
// 获取用户输入
Scanner scanner = new Scanner(System.in);
int flag = scanner.nextInt();
if (flag == 1) {
//轿车
System.out.println("请选择你要租赁的汽车品牌:1、别克 2、宝马");
int brand = scanner.nextInt();
if (brand == 1) {
System.out.println("请选择你要租赁的汽车类型:1、林荫大道 2、GL8");
int type = scanner.nextInt();
if (type == 1) {
moto = motoOperation.motoLeaseOut("别克", "林荫大道", 4);
} else if (type == 2) {
moto = motoOperation.motoLeaseOut("别克", "GL8", 4);
}
} else if (brand == 2) {
System.out.println("请选择你要租赁的汽车类型:1、X6 2、550i");
int type = scanner.nextInt();
if (type == 1) {
moto = motoOperation.motoLeaseOut("宝马", "X6", 4);
} else if (type == 2) {
moto = motoOperation.motoLeaseOut("别克", "550i", 4);
}
}
} else if (flag == 2) {
//客车
System.out.println("请选择你要租赁的汽车品牌:1、金龙 2、金杯");
int brand = scanner.nextInt();
if (brand == 1) {
System.out.println("请选择你要租赁的汽车座位数:1、16座 2、34座");
int type = scanner.nextInt();
if (type == 1) {
moto = motoOperation.motoLeaseOut("金龙", "", 16);
} else if (type == 2) {
moto = motoOperation.motoLeaseOut("金龙", "", 34);
}
} else if (brand == 2) {
System.out.println("请选择你要租赁的汽车座位数:1、16座 2、34座");
int type = scanner.nextInt();
if (type == 1) {
moto = motoOperation.motoLeaseOut("金杯", "", 16);
} else if (type == 2) {
moto = motoOperation.motoLeaseOut("金杯", "", 34);
}
}
}
//客户汽车匹配完成
if (moto != null) {
// 获取用户租车天数
System.out.println("请输入您的租车天数:");
int days = scanner.nextInt();
// 计算租车钱
double money = moto.calRent(days);
System.out.println("分配给您的汽车牌号是:" + moto.getId());
System.out.println("您需要支付的租赁费用是:" + money + "元");
}else {
System.out.println("很抱歉先生:当前的公司内,暂无您要的汽车类型!请重新选择!");
} } }
05-26 22:27