评论

收藏

[Java] java实现汽车租赁系统

编程语言 编程语言 发布于:2021-10-05 17:14 | 阅读数:546 | 评论:0

这篇文章主要为大家详细介绍了java实现汽车租赁系统,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
本文实例为大家分享了java实现汽车租赁系统的具体代码,供大家参考,具体内容如下
DSC0000.jpg
//车类
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","宝马",800,"x6");  //vehicle v = new car();
 vehicle[1] = new car("京cny32584","宝马",600,"550i");  //vehicle v = new car();
 vehicle[2] = new car("京nt37465","别克",300,"林荫大道");  //vehicle v = new car();
 vehicle[3] = new car("京nt96968","别克",600,"gl8");  //vehicle v = new car();
 vehicle[4] = new bus("京6566754","金杯",800,16);  //vehicle v = new bus();
 vehicle[5] = new bus("京8696997","金龙",800,16);  //vehicle v = new bus();
 vehicle[6] = new bus("京9696996","金杯",1500,34);  //vehicle v = new bus();
 vehicle[7] = new bus("京8696998","金龙",1500,34);  //vehicle v = new bus();
 }
 //租车:根据用户提供的条件去汽车数组中查找相应车辆并返回
 //如果租赁的是轿车  需要条件:品牌  型号
 //如果租赁的是客车  需要条件:品牌  座位数
 //简单工厂模式
 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.轿车\t\t2.客车");
 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+"元");
 }
 
}
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持CodeAE代码之家
原文链接:https://blog.csdn.net/baidu_29343517/article/details/81153722

关注下面的标签,发现更多相似文章