您的位置:首页 > 其它

汽车租赁管理系统及所涉及面向对象的一般步骤

2015-09-12 10:31 393 查看
一.面向对象设计的步骤
1.分析需求

2.找名词(类和属性)
父类:
汽车:品牌、日租金、车牌号
子类:
轿车:型号
客车:座位数
汽车业务类
汽车租赁管理类

3.找动词(方法)
汽车(计算租金)
汽车业务类(初始化汽车,提供租赁服务)
汽车租赁管理类(入口和系统界面)

4.优化设计
父子类关系:汽车(父)-->轿车、客车(子类)
汽车(抽象类):计算租金-->抽象方法

5.梳理运行过程
汽车业务类(MoteOperation):
            初始化-->构造车(轿车,客车)
            -->构造这些车怎么存储?
            -->对象数组(MotoVehicle[])

    租车:
        根据用户的租车条件去查找相应的车辆,并返回相应的车辆,用户的租车条件为:方法的参数
        循环遍历汽车数组(Motovehicle[])
        拿到每一辆车
        将车转换为自己实际的子类类型 instanceof
        分别根据用户的不用条件进行比较
                car:brand、type
                bus:brand、seatCount
        发现符合用户条件的相应车辆,返回
    

二.最后进行测试和调试

三.总结

/**
* 汽车父类
*/
public abstract class MotoVehicle {
// 品牌、日租金、车牌号
private String brand;
private String vehicleId;
private int perRent;

// 构造方法
public MotoVehicle() {

}

public MotoVehicle(String brand, String vehicleId, int perRent) {
this.brand = brand;
this.vehicleId = vehicleId;
this.perRent = perRent;
}

// set/get方法
public String getBrand() {
return brand;
}

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

public String getVehicleId() {
return vehicleId;
}

public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}

public int getPerRent() {
return perRent;
}

public void setPerRent(int perRent) {
this.perRent = perRent;
}

// 抽象的计算租金的方法(根据租车的天数)
public abstract float calcRent(int days);

}
/**
* 轿车子类
*/
public class Car extends MotoVehicle {
private String type;// 型号

public Car() {

}

public Car(String brand, String vehicleId, int perRent, String type) {
super(brand, vehicleId, 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 >= 150) {
price = price * 0.7f;
} else if (days >= 30) {
price = price * 0.8f;
} else if (days >= 7) {
price = price * 0.9f;
}
return price;
}

}
/**
* 客车子类
*/
public class Bus extends MotoVehicle {
// 座位数
private int seatCount;

public Bus() {

}

public Bus(String brand, String vehicleId, int perRent, int seatCount) {
super(brand, vehicleId, 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 >= 150) {
price = price * 0.6f;
} else if (days >= 30) {
price = price * 0.7f;
} else if (days >= 7) {
price = price * 0.8f;
} else if (days >= 3) {
price = price * 0.9f;
}
return price;

}

}
/**
* 汽车业务类
*/
public class MotoOperation {
MotoVehicle[] motos = new MotoVehicle[8];

// 初始化汽车
public void init() {
// 轿车
// String brand, String vehicleId, int perRent,String type
motos[0] = new Car("宝马", "京N28588", 800, "X6");
motos[1] = new Car("宝马", "京N26566", 600, "550i");
motos[2] = new Car("别克", "京L98588", 300, "林荫大道");
motos[3] = new Car("别克", "京L96566", 600, "GL8");

// 客车
// String brand, String vehicleId, int perRent,int seatCount
motos[4] = new Bus("金龙", "京M18288", 800, 16);
motos[5] = new Bus("金龙", "京M16266", 1500, 34);
motos[6] = new Bus("金杯", "京H28388", 800, 16);
motos[7] = new Bus("金杯", "京H26366", 1500, 34);
}

// 提供租赁服务(根据条件查找相应车辆并返回)
public MotoVehicle rentVehicle(String brand, String type, int seatCount) {
MotoVehicle rentMoto = null;
for (MotoVehicle moto : motos) {
if (moto instanceof Car) {
Car car = (Car) moto;
if (brand.equals(moto.getBrand()) && type.equals(car.getType())) {
rentMoto = car;
break;
}
} else if (moto instanceof Bus) {
Bus bus = (Bus) moto;
if (brand.equals(moto.getBrand()) && bus.getSeatCount() == seatCount) {
rentMoto = bus;
break;
}
}
}
return rentMoto;
}
/*
* public static void main(String[] args) { MotoOperation motoOpr=new
* MotoOperation(); motoOpr.init(); MotoVehicle
* moto=motoOpr.rentVehicle("宝马", "X6", 0); int days=5;
* System.out.println(moto.getBrand());
* System.out.println("租金"+moto.getPerRent()*days);
* System.out.println(moto.getVehicleId());
* System.out.println(((Car)moto).getType()); }
*/
}
import java.util.Scanner;

/**
* 汽车租赁管理类(入口和系统界面)
*/
public class RentVehicle {
public static void main(String[] args) {
System.o
9352
ut.println("**********欢迎光临腾飞汽车租赁公司**********");
System.out.print("请选择租车类型:(1.轿车 2.客车)");
Scanner input = new Scanner(System.in);
int choose = input.nextInt();
String brand = "";
String type = "";
int seatCount = 0;
switch (choose) {
case 1:
// 租赁轿车
System.out.print("请选择品牌:(1、宝马 2、别克)");
int brandChoose = input.nextInt();
if (brandChoose == 1) {
brand = "宝马";
System.out.print("请选择类型:(1、X6 2、550i)");
/*
* int typeChoose=input.nextInt();
* type=(typeChoose==1)?"X6":"550i"
*/;
type = (input.nextInt() == 1) ? "X6" : "550i";
} else {
brand = "别克";
System.out.print("请选择类型:(1.林荫大道 2.GL8)");
/* int typeChoose=input.nextInt(); */
type = (input.nextInt() == 1) ? "林荫大道" : "GL8";
}
break;
case 2:
// 租赁客车
System.out.print("请选择品牌:(1、金龙 2、金杯)");
brand = (input.nextInt() == 1) ? "金龙" : "金杯";
System.out.print("请选择座位数:(1、16 2、34)");
seatCount = (input.nextInt() == 1) ? 16 : 34;
break;
}
// 租车
System.out.print("请输入租赁天数:");
int days = input.nextInt();
MotoOperation motoOpr = new MotoOperation();
motoOpr.init();
MotoVehicle moto = motoOpr.rentVehicle(brand, type, seatCount);
// 提示租车信息给用户
System.out.println("租车成功!您租的车是:" + moto.getVehicleId());
System.out.print("您应付的租金是(元):" + moto.calcRent(days));
}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: