您的位置:首页 > 其它

汽车租赁

2015-09-26 10:10 393 查看
代码和任务

1、汽车租赁系统

需求说明:某汽车租赁公司出租多种轿车和客车,出租费用以日为单位计算。出租车型及信息如下表所示。

从上表可以分析出该项目的类和类的属性,以及类的方法:

(1)轿车和客车是两个常用的类,汽车可以作为两者的父类设计,也就是汽车类

(2)别克、宝马、金杯、金龙是汽车的品牌,作为汽车类的属性品牌

(3)X6、550i、GL8、林荫大道是轿车的型号,可以作为轿车类的属性型号的值;

(4)京NY28588、京CNY3284、京6566754等是汽车的车牌号,可以作为汽车类的属性车牌号的值;

(5)16座、34座可作为客车的属性座位数的值;

(6)日租金可以作为汽车对象的属性;

(7)汽车有租赁业务,所以要有汽车业务类,主要负责公司汽的租赁;

(8)最后要有个汽车租赁管理类,程序的入口;

(9)每种类型汽车的计算租金的折扣是不同的,所以将计算租金这个方法设计为父类的方法,取名为calRent(int days),子类轿车和客车根据租赁的天数计算打折的标准不同,分别重写父类的方法;

(10)在租赁汽车时,轿车的日租金和品牌、型号有关,需要客户提供所需租赁汽车的品牌和型号;客车的日租金和座位数有关,需要客户提供所需租赁汽车的品牌和座位数;

(11)客户在租赁时,系统根据客户的选择类型来决定是租赁轿车还是客车,属于租赁的业务,所以将租赁方法设计为汽车业务类的方法此方法会得到一个具体的汽车;

(12)汽车租赁管理类设计一个程序入口的方法,用来显示用户界面以及信息输出;

编写程序实现汽车租赁业务,效果如下图所示。

2、新增卡车车型

需求说明:新增“卡车”车型。新购置了卡车,根据吨位,租金每吨每天50元,在作业1的基础上对系统进行扩展,计算汽车租赁的总租金,实现效果如下图所示。

<span style="font-size:18px;">public class Bus extends Cars {
private	int seatNum;

public int getSeatNum() {
return this.seatNum;
}
public void setSeatNum(int seatNum) {
this.seatNum = seatNum;
}
//构造方法
public Bus(){

}
public Bus(String brand,String license,int seatNum,int dailyRent){
this.setBrand(brand);
this.setLicense(license);
this.setSeatNum(seatNum);
this.setDailyRent(dailyRent);

}
//租金计算
public double callRent(int days){
if(days<3){
return days*this.getDailyRent();
}else if(days<7){
return days*this.getDailyRent()*0.9;
}else if(days<30){
return days*this.getDailyRent()*0.8;
}else if(days<150){
return days*this.getDailyRent()*0.7;
}else{
return days*this.getDailyRent()*0.6;
}

}
}
</span>


<span style="font-size:18px;">public class MiniCar extends Cars {
private String model;

public String getModel() {
return this.model;
}

public void setModel(String model) {
this.model = model;
}
//构造方法
public MiniCar(){

}
public MiniCar(String brand,String model,String license,int dailyRent){
this.setBrand(brand);
this.setLicense(license);
this.setModel(model);
this.setDailyRent(dailyRent);

}
//租金计算
public double callRent(int days){
if(days<=7){
return days*this.getDailyRent();
}else if(days<=30){
return days*this.getDailyRent()*0.9;
}else if(days<=150){
return days*this.getDailyRent()*0.8;
}else{
return days*this.getDailyRent()*0.7;
}

}

}
</span>
<span style="font-size:18px;">public class Truck extends Cars {
private int tonnage;
public void setTonnage(int tonnage){
this.tonnage=tonnage;
}
public int getTonnage(){
return this.tonnage;
}
//构造方法
public Truck(){

}
public Truck (String brand,String license,int tonnage,int dailyRent){
this.setBrand(brand);
this.setDailyRent(dailyRent);
this.setLicense(license);
this.setTonnage(tonnage);

}
//租金计算

public double callRent(int days){

return this.getDailyRent()*this.getTonnage()*days;
}

}
</span>


<span style="font-size:18px;">public class Cars{
private String brand;
private String license;
private  int dailyRent;

public String getBrand() {
return brand;
}

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

public String getLicense() {
return license;
}

public void setLicense(String license) {
this.license = license;
}

public int getDailyRent() {
return dailyRent;
}

public void setDailyRent(int dailyRent) {
this.dailyRent = dailyRent;
}

public double callRent(int days){
return 0;
}
}
</span>


<span style="font-size:18px;">public class CarBusiness {
//在数据库初始化车辆
Cars[] cars = new Cars[12];
public void init(){

cars[0]=new MiniCar ("宝马","x6","京NY98887",800);
cars[1]=new MiniCar ("宝马","550i","京NY87763",600);
cars[2]=new MiniCar ("别克","林荫大道","京AC95543",300);
cars[3]=new MiniCar ("别克","GL8","京RT88772",600);
cars[4]=new Bus("金杯","京AS42876",16,800);
cars[5]=new Bus("金龙","京DF87654",16,800);
cars[6]=new Bus("金杯","京YU87323",34,1500);
cars[7]=new Bus("金龙","京OP89232",34,1500);
cars[8]=new Truck("一汽解放","京OP56232",7,1500);
cars[9]=new Truck("重庆红岩","京OP19232",7,1000);
cars[10]=new Truck("一汽解放","京OP34232",10,2000);
cars[11]=new Truck("重庆红岩","京OP19932",10,3000);

}
//		Cars[] cars = new Cars[8];
//		cars[0]=new MiniCar ("宝马","x6","京NY98887",800);
//		cars[1]=new MiniCar ("宝马","550i","京NY87763",600);
//		cars[2]=new MiniCar ("别克","林荫大道","京AC95543",300);
//		cars[3]=new MiniCar ("别克","GL8","京RT88772",600);
//		cars[4]=new Bus("金杯","京AS42876",16,800);
//		cars[5]=new Bus("金龙","京DF87654",16,800);
//		cars[6]=new Bus("金杯","京YU87323",34,1500);
//		cars[7]=new Bus("金龙","京OP89232",34,1500);
//在数据库中搜索的方法
public Cars search(String brand,String model,int seatNum,int tonnage){
Cars moto=new Cars();
for(Cars motocar:cars){
if (motocar instanceof MiniCar){
MiniCar motoMiniCar=(MiniCar)motocar;
if(motoMiniCar.getBrand().equals(brand) &&motoMiniCar.getModel().equals(model) ){
moto=motoMiniCar;
break;
}
}
if(motocar instanceof Bus){
Bus motobus=(Bus)motocar;
if(motobus.getBrand().equals(brand) &&motobus.getSeatNum()==seatNum){
moto=motobus;
break;
}
}

if(motocar instanceof Truck){
Truck motoTruck=(Truck)motocar;
if(motoTruck.getBrand().equals(brand) && motoTruck.getTonnage()==tonnage){
moto=motoTruck;
break;
}
}

}
return moto;
}

}
</span>


<span style="font-size:18px;">import java.util.Scanner;

public class CarBusinessManger {

/**
* @param args
*/
public static void main(String[] args) {
String answer="";

System.out.println("*********************欢迎来到腾飞汽车租赁公司***********************");
Scanner input=new Scanner(System.in);
System.out.print("请选择你要的汽车类型: 1.轿车 2.客车 3.卡车");
int chooseType=input.nextInt();
int chooseBrand=-1;
int chooseModel=-1;
int chooseSeatNum;
String brand="";
String model="";
int seatNum=-1;
int chooseTonnage=-1;
int tonnage=-1;
switch(chooseType){
case 1:
System.out.print("请选择汽车品牌:1.宝马 2.别克");
chooseBrand=input.nextInt();

switch(chooseBrand){
case 1:
brand="宝马";
System.out.print("请选择汽车型号1.x6 2.550i");
chooseModel=input.nextInt();
switch(chooseModel){
case 1:
model="x6";
break;
case 2:
model="550i";
break;

}
break;
case 2:
brand="别克";
System.out.print("请选择汽车型号1.林荫大道 2.Gl8");
chooseModel=input.nextInt();
switch(chooseModel){
case 1:
model="林荫大道";
break;
case 2:
model="GL8";
break;

}
break;

}
break;
case 2:
System.out.print("请选择汽车品牌:1.金杯 2.金龙");
chooseBrand=input.nextInt();
switch(chooseBrand){
case 1:
brand="金杯";
System.out.print("请选择汽车座位数1.16 2.34");
chooseSeatNum=input.nextInt();
switch(chooseSeatNum){
case 1:
seatNum=16;
break;
case 2:
seatNum=34;
break;

}
break;
case 2:
brand="金龙";
System.out.print("请选择汽车座位数1.16 2.34");
chooseSeatNum=input.nextInt();
switch(chooseSeatNum){
case 1:
seatNum=16;
break;
case 2:
seatNum=34;
break;

}
break;

}

break;
case 3:
System.out.print("请选择汽车品牌:1.一汽解放 2.重庆红岩");
chooseBrand=input.nextInt();
switch(chooseBrand){
case 1:
brand="一汽解放";
System.out.print("请选择汽车吨位1.  7吨 2.  10吨");
chooseTonnage=input.nextInt();
switch(chooseTonnage){
case 1:
tonnage=7;
break;
case 2:
tonnage=10;
break;

}
break;
case 2:
brand="重庆红岩";
System.out.print("请选择汽车吨位1.  7吨 2.  10吨");
chooseTonnage=input.nextInt();
switch(chooseTonnage){
case 1:
tonnage=7;
break;
case 2:
tonnage=10;
break;

}

break;

}

break;
}

System.out.print("请输入租赁天数:");
int days=input.nextInt();
CarBusiness cb=new CarBusiness();
cb.init();
Cars searchCar=cb.search(brand, model,seatNum,tonnage);
double price=searchCar.callRent(days);
System.out.println("你租赁的车车牌号为:"+searchCar.getLicense());
System.out.println("您应付的租金为:"+price);
//		if (searchCar instanceof MiniCar){
//			MiniCar newCar=(MiniCar)searchCar;
//			}
//		if(searchCar instanceof Bus){
//			Bus newCar=(Bus)searchCar;
//			}

}

}
</span>


运行结果



总结体会

分类还是模糊不清。



一开始,我想的是车是父类客车卡车轿车,是其子类,然后再根据品牌,宝马一个子类,别克是轿车的子类。宝马可以单独成为一个类,同为宝马也可以有很多型号。这样就有三个层次的类了。

但是视频中的却是把宝马和别克,作为一种品牌的属性,给了轿车的成员属性。

这点让我很疑惑。

父类和子类的边界在哪?意思是什么时候父类生出子类?

这里车的品牌宝马和别克既可以作为轿车的成员属性,也可以作为单独的子类。

甚至,车这个类可以把客车卡车轿车,合为一个车类中的属性,车的用途类型,这样只需要一个类。所有实例化的车,都有用途属性,品牌属性,型号属性,好像也说的过去。但是好像跟面向过程没什么区别了。

那分为三个类的,是不是更面向对象?

如果车的属性分的更细,同一种轿车也有不同的优惠折扣,同一款车有多辆车牌。轿车这个类应该也要抽象为抽象类吧。这分类要靠实际需求,在这里分为两个层次很合适。

还有测试类大致想到了。测试类之前的一个数据初始化,和数据搜索的类没想到。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: