某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示
![](https://img.haomeiwen.com/i11260363/d0c3044c3fddde90.png)
模块分析:
该系统分为5个模块MotoVehicle(汽车类(父类))、Bus(客车(子类))、Car(汽车(子类))、MotoOperation(业务)、RenMgr(测试))
第一个模块:
//汽车类
public abstract class MotoVehicle {
private String vehicleId; //车牌号
private String brand; //品牌
private int perRent;//日租金
public MotoVehicle() { //无参构造方法
}
public MotoVehicle(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); //让子类继承父类的抽象方法
}
第二个模块:
//客车类
public class Bus extends MotoVehicle { //子类继承父类属性
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;
}
//重写了父类的计算方法;根据自己的计算租金规则
@Override
public float calcRent(int days){
float price =this.getPerRent()*days; //不打折初始值
if( days>=3&&days<7){ //大于等于3天打9折
price*=0.9f;
}else if(days>=7&&days<30){ //大于等于7天打8折
price*=0.8f;
}else if(days>=30&&days<150){ //大于等于30天打7折
price*=0.7f;
}else if(days>=150){ //大于等于150天打6折
price*=0.6f;
}
return price; //其他情况直接返回初始值
}
}
第三个模块:(和第二个模块类似)
//轿车类
public class Car extends MotoVehicle { //子类继承父类属性
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;
}
//重写了父类的计算方法;根据自己的计算租金规则
@Override
public float calcRent(int days){
float price =this.getPerRent()*days; //不打折初始值
if( days>7&&days<=30){ //大于7天打9折
price*=0.9f;
}else if(days>30&&days<=150){ //大于30天打8折
price*=0.8f;
}else if(days>150){ //大于150天打7折
price*=0.7f;
}
return price;
}
}
第四个模块:
//汽车业务类
public class MotoOperation {
//汽车类型的数组,将来数组声明为父类类型
public MotoVehicle[] motos = new MotoVehicle[8];
//初始化汽车信息
public void init(){
motos[0] =new Car("京N78576","宝马",800,"X6");
motos[1] =new Car("京K98976","宝马",600,"550i");
motos[2] =new Car("京S78976","别克",500,"林荫大道");
motos[3] =new Car("京B78006","别克",600,"GL8");
motos[4] =new Bus("京U32576","金杯",800,16);
motos[5] =new Bus("京H76786","金杯",1500,34);
motos[6] =new Bus("京F99999","金龙",800,16);
motos[7] =new Bus("京A78676","金龙",800,34);
}
//租车:根据用户提供的条件去汽车数组中查找相应的车辆并返回
//如果租赁的是客车 需要的条件:品牌 座位数
//如果租赁的是轿车 需要的条件: 品牌 型号 座位数
public MotoVehicle motoLeaseOut(String brand,String type,int seatCount){
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 {
Bus bus=(Bus)mymoto;
if(bus.getBrand().equals(brand)&&bus.getSeatCount()==seatCount){
moto = bus;
break;
}
}
}
return moto;
}
}
第五个模块:
public class RenMgr {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
MotoOperation motoMgr = new MotoOperation();
//租赁公司首先需要购置汽车
motoMgr.init();
System.out.println("**********欢迎光临租赁公司********");
System.out.println("1、轿车 2、客车");
System.out.println("请选择您要租的车型:");
int motoType = input.nextInt();
//客户租车的条件
String brand = ""; //品牌
String type = ""; //型号
int seat = 0; //座位数
//收集用户条件
if (motoType == 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 (motoType == 2) {
//租客车
type="";
System.out.println("请选择您要租赁的客车品牌:1、金杯 2、金龙");
brand = (input.nextInt())==1?"金杯":"金龙";
System.out.println("请选择您要租赁的客车座位数:1、16 2、34");
seat = (input.nextInt())==1?16:34;
}
//租车
MotoVehicle moto= motoMgr.motoLeaseOut(brand,type,seat);
System.out.println("请输入您的租赁天数:");
int days = input.nextInt();
float money=moto.calcRent(days); //动态绑定
System.out.println("您需要支付:"+money+"元");
System.out.println("租车成功,请按照如下车牌号去提车:"+moto.getVehicleId());
}
}
网友评论