本⽂实例为⼤家分享了java实现汽车租赁系统的具体代码,供⼤家参考,具体内容如下
//车类
public abstract class Vehicle { //车牌号 品牌 ⽇租⾦ private String id; private String brand; private int perRent;
public Vehicle(){}
//Vehicle的带参构造⽅法
public Vehicle(String id, String brand, int perRent) { this.id = id;
this.brand = brand; this.perRent = perRent; }
public void setId(String id){ this.id=id ; }
public String getId(){ return id; }
public void setBrand(String brand){ this.brand=brand; }
public String getBrand(){ return brand; }
public void setPerRent(int perRent){ this.perRent=perRent; }
public int getPerRent(){ return perRent; }
//抽象⽅法计算租⾦
public abstract double calcRent(int days); }
//轿车类
public class Car extends Vehicle{ //型号
private String type;
public void setType(String type){ this.type=type; }
public String getType(){ return type; }
public Car(){}
//Car的带参构造⽅法
public Car(String id, String brand, int perRent,String type) { super(id,brand,perRent); this.type = type; }
//重写⽗类的计算租⾦⽅法:根据⾃⼰的计算租⾦规则 public double calcRent(int days) {
double price = this.getPerRent()*days; if(days>7 && days<=30){ price *= 0.9;
}else if(days>30 && days<=150){ price *= 0.8;
}else if(days>150){ price *= 0.7; }
return price; }
}
//客车类
public class Bus extends Vehicle{ //座位数
private int seatCount;
public void setSeatCount(int seatCount){ this.seatCount=seatCount; }
public int getSeatCount(){ return seatCount; }
public Bus(){}
//Bus的带参构造⽅法
public Bus(String id,String brand,int perRent,int seatCount){ super(id,brand,perRent); this.seatCount = seatCount; }
//重写⽗类的计算租⾦⽅法:根据⾃⼰的计算租⾦规则 public double calcRent(int days) {
double price = this.getPerRent()*days; if(days>=3 && days<7){ price *= 0.9;
}else if(days>=7 && days<30){ price *= 0.8;
}else if(days>=30 && days<150){ price *= 0.7;
}else if(days>150){ price *= 0.6; }
return price; }}
//汽车业务类
public class Operation {
public Vehicle[] vehicle = new Vehicle[8];//初始化汽车信息 public void init(){
vehicle[0] = new Car(\"京NY28588\宝马\ vehicle[1] = new Car(\"京CNY32584\宝马\ vehicle[2] = new Car(\"京NT37465\别克\林荫⼤道\"); //vehicle v = new Car(); vehicle[3] = new Car(\"京NT96968\别克\ vehicle[4] = new Bus(\"京65667\⾦杯\ vehicle[5] = new Bus(\"京8696997\⾦龙\ vehicle[6] = new Bus(\"京9696996\⾦杯\ vehicle[7] = new Bus(\"京8696998\⾦龙\ }
//租车:根据⽤户提供的条件去汽车数组中查找相应车辆并返回 //如果租赁的是轿车 需要条件:品牌 型号 //如果租赁的是客车 需要条件:品牌 座位数 //简单⼯⼚模式
public Vehicle zuChe(String brand,String type,int seatCount){ Vehicle che = null;
//for循环遍历数组vehicle for(Vehicle myche : vehicle){
//判断Vehicle类的myche的类型是否和Car⼀样 if(myche instanceof Car){
//Vehicle类的myche向下转型变成⼦类Car Car car = (Car)myche;
if(car.getBrand().equals(brand) && car.getType().equals(type)){ che=car; break; }
}else{
//Vehicle类的myche向下转型变成⼦类Bus Bus bus = (Bus)myche;
if(bus.getBrand().equals(brand) && bus.getSeatCount() == seatCount){ che=bus; break; } } }
return che; }}
//汽车租赁
public class Rent {
public static void main(String[] args) { Scanner input = new Scanner(System.in); Operation operation = new Operation(); //租赁公司界⾯ operation.init();
System.out.println(\"**********欢迎光临租赁公司**********\"); System.out.println(\"1.轿车\\2.客车\");
System.out.println(\"请选择您要租赁的汽车类型:(1.轿车 2.客车)\"); int cheType = input.nextInt(); String brand = \"\";//品牌 String type = \"\";//型号 int seatCount = 0;//座位数 //收集⽤户条件 if(cheType == 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(cheType == 2){ //租赁客车 type= \"\";
System.out.println(\"请选择您要租赁的客车品牌:(1.⾦杯 2.⾦龙)\"); brand=(input.nextInt()==1)?\"⾦杯\":\"⾦龙\";
System.out.println(\"请选择您要租赁的客车座位数:(1.16座 2.32座)\"); seatCount=(input.nextInt() == 1)?16:34; }
//租车
Vehicle che = operation.zuChe(brand, type, seatCount); System.out.println(\"请输⼊您的租赁天数:\"); int days = input.nextInt();
double money = che.calcRent(days);
System.out.println(\"租车成功,请按照如下车牌号提车:\"+che.getId()); System.out.println(\"您需要⽀付:\"+money+\"元\"); } }
以上就是本⽂的全部内容,希望对⼤家的学习有所帮助,也希望⼤家多多⽀持。
因篇幅问题不能全部显示,请点此查看更多更全内容
Copyright © 2019- net188.cn 版权所有 湘ICP备2022005869号-2
违法及侵权请联系:TEL:199 1889 7713 E-MAIL:2724546146@qq.com
本站由北京市万商天勤律师事务所王兴未律师提供法律服务