使用java实现汽车租赁系统的案例

发布时间:2021-04-15 11:44:19 作者:小新
来源:亿速云 阅读:269

这篇文章给大家分享的是有关使用java实现汽车租赁系统的案例的内容。小编觉得挺实用的,因此分享给大家做个参考,一起跟随小编过来看看吧。

具体内容如下

使用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","宝马",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+"元");
 }
 
}

感谢各位的阅读!关于“使用java实现汽车租赁系统的案例”这篇文章就分享到这里了,希望以上内容可以对大家有一定的帮助,让大家可以学到更多知识,如果觉得文章不错,可以把它分享出去让更多的人看到吧!

推荐阅读:
  1. python如何实现汽车管理系统
  2. C#实现汽车租赁系统项目

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

java

上一篇:java实现租车系统的示例分析

下一篇:如何使用Java实现简单订餐系统

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》