在编写这个系统时,我用到了面向对象的思想,编写这个系统的基本步骤如下:
一、面向对象设计的步骤
1、分析需求2、找名词(类、属性)
父类
汽车:品牌、日租金、车牌号
子类
轿车:型号
客车:座位数
汽车业务类
汽车租赁管理类
3、找动词(方法)
汽车(计算租金)
汽车业务类(初始化车、提供租赁服务)
汽车租赁管理类(入口和系统界面)
4、优化设计
父子类的关系:汽车(父)-->轿车、客车(子类)
汽车(抽象类):计算租金 -> 抽象方法
汽车业务类(MotoOperation)
初始化车->构造车(轿车、客车)
->构造这些车怎么存储?
->对象数组(MotoVehicle[])
(如果你不理解,那想想下面:
MotoVehicle moto1=new Car(....);
MotoVehicle moto2=new Bus(....);)
租车:
根据用户的租车条件去查找相应车辆,并返回相应车辆,用户的租车条件:方法的参数
循环遍历汽车数组(MotoVehicle[])
拿到每一辆车(Car、Bus)
将车辆转换为自己实际的子类类型
instanceof
分别根据用户不同的条件进行比较
Car:brand、type
Bus:brand、seatCount
发现符合用户条件的相应车辆,返回
二、最后进行测试和调试
三、总结
技能总结(面向对象:封装、继承、多态)
思路总结
团队合作
异常总结
ArraysIndexOutOfBoundsException
NullPointerException
ClassCastException
在编写的过程中,遇到了很多麻烦,最后通过找同学帮忙,通过视频讲解,最后都解决了这些问题。让我感悟最深的是,在遇到问题时,不要一味的钻牛角尖,一定要解决这个问题,这会让你越来越烦躁,越来越混乱,这样问题就会很难解决,我们可以通过适当的放松来缓解一下压力,然后再回过头来继续想这个问题,也可以通过询求老师和同学的帮助解决遇到的问题。
下面是汽车租赁管理系统的代码:
package cn.jbit.vehicle;
/*
* 汽车:父类
* */
public abstract class MotoVehicle {
//品牌、日租金、车牌号
private String brand;
private int perRent;
private String vehicleId;
public MotoVehicle() {}
public MotoVehicle(String brand, int perRent, String vehicleId) {
this.brand = brand;
this.perRent = perRent;
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 String getVehicleId() {
return vehicleId;
}
public void setVehicleId(String vehicleId) {
this.vehicleId = vehicleId;
}
//计算租金:根据用户租车的天数计算租金
public abstract float calcRent(int days);
}
package cn.jbit.vehicle;
/*
* 子类:轿车类
* */
public class Car extends MotoVehicle {
// 型号
private String type;
public Car() {
}
public Car(String brand, int perRent, String vehicleId, String type) {
super(brand, perRent, vehicleId);
this.type = type;
}
public void setType(String type) {
this.type = type;
}
public String getType() {
return this.type;
}
// 计算租金:
/*
* days>7天9折 days>30天8折 days>150天7折
*/
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 = price * 0.8f;
} else if (days > 150) {
price = price * 0.7f;
}
return price;
}
package cn.jbit.vehicle;
/*
*子类客车类
* */
public class Bus extends MotoVehicle {
private int seatCount;
public Bus() {
}
public Bus(String brand, int perRent, String vehicleId, int seatCount) {
super(brand, perRent, vehicleId);
this.seatCount = seatCount;
}
public void setSeatCount(int seatCount) {
this.seatCount = seatCount;
}
public int getSeatCount() {
return this.seatCount;
}
// 计算租金:
/*
* days>=3天9折 days>=7天8折 days>=30天7折 days>=150天6折
*/
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 = price * 0.8f;
} else if (days >= 30 && days < 150) {
price = price * 0.7f;
} else if (days >= 150) {
price = price * 0.6f;
}
return price;
}
}
package cn.jbit.vehicle;
/*
* 汽车业务类
* */
public class MotoOperation {
MotoVehicle[] motos = new MotoVehicle[8];
// 初始化数据
public void init() {
// 品牌、日租金、车牌号、型号
motos[0] = new Car("宝马", 800, "京NY28588", "X6");
motos[1] = new Car("宝马", 600, "京CNY3284", "550i");
motos[2] = new Car("别克", 300, "京NT37465", "林荫大道");
motos[3] = new Car("别克", 600, "京NT96968", "GL8");
// 品牌、日租金、车牌号、座位数
motos[4] = new Bus("金龙", 800, "京8696997", 16);
motos[5] = new Bus("金龙", 1500, "京8696998", 34);
motos[6] = new Bus("金杯", 800, "京6566754", 16);
motos[7] = new Bus("金杯", 1500, "京9696996", 34);
}
// 提供租赁服务
// 根据用户的租车条件去查找相应车辆,并返回相应车辆
// 用户的租车条件,方法的参数
public MotoVehicle rentVehicle(String brand, String type, int seatCount) {
MotoVehicle rentMoto = null;
for (MotoVehicle moto : motos) {
if (moto instanceof Car) {
moto = (Car) moto;
if (brand.equals(moto.getBrand()) && type.equals(((Car) moto).getType())) {
rentMoto = moto;
break;
}
}
if (moto instanceof Bus) {
moto = (Bus) moto;
if(brand.equals(moto.getBrand()) && ((Bus)moto).getSeatCount()==seatCount){
rentMoto=moto;
break;
}
}
}
return rentMoto;
}
}package cn.jbit.vehicle;
import java.util.Scanner;
/*
* 汽车租赁管理类(入口和系统界面)
* */
public class RentVehicleSys {
public static void main(String[] args) {
System.out.println("*********欢迎光临腾飞汽车租赁公司***********");
System.out.println("请选择租车类型: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.println("请选择类型:1、X6 2、550i");
int typeChoose = input.nextInt();
type = (typeChoose == 1) ? "X6" : "550i";
} else {
brand = "别克";
System.out.println("请选择类型:1、林荫大道 2、GL8");
int typeChoose = input.nextInt();
type = (typeChoose == 1) ? "林荫大道 " : "GL8";
}
break;
case 2:
// 租赁客车
System.out.print("请选择品牌:1、金龙2、金杯");
brand=(input.nextInt()==1)?"金龙":"金杯";
System.out.println("请选择座位数:1、16座 2、34座");
int typeChoose=input.nextInt();
seatCount=(typeChoose==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("*************租车结果***************");
// 提供租车信息给用户
System.out.println("租车成功!分配给您的车是:" + moto.getVehicleId());
System.out.println("您应付租金(元):" + moto.calcRent(days));
}
}
本文介绍了作者在实现汽车租赁管理系统时运用面向对象思想的过程,包括汽车抽象类的设计和解决遇到的ClassCastException问题。作者强调了遇到问题时应当保持冷静,适时寻求帮助的重要性,并分享了部分代码。

4935

被折叠的 条评论
为什么被折叠?



