java实现汽车租赁系统

 更新时间:2021年01月20日 09:18:37   作者:妖精小狗  
这篇文章主要为大家详细介绍了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+"元");
 }
 
}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。

相关文章

  • RepeatSubmit若依框架如何防止表单重复提交注解

    RepeatSubmit若依框架如何防止表单重复提交注解

    若依框架中的@RepeatSubmit注解用于防止表单重复提交,通过在控制器方法上添加该注解,并在前端页面和JavaScript代码中实现双重校验,可以确保同一用户在短时间内不会重复提交相同的表单
    2024-11-11
  • Java使用正则表达式删除所有HTML标签的方法示例

    Java使用正则表达式删除所有HTML标签的方法示例

    这篇文章主要介绍了Java使用正则表达式删除所有HTML标签的方法,结合完整实例形式分析了java针对HTML页面元素script标签、style标签、html标签等的正则匹配相关操作技巧,需要的朋友可以参考下
    2017-06-06
  • Java压缩文件工具类ZipUtil使用方法代码示例

    Java压缩文件工具类ZipUtil使用方法代码示例

    这篇文章主要介绍了Java压缩文件工具类ZipUtil使用方法代码示例,具有一定借鉴价值,需要的朋友可以参考下。
    2017-11-11
  • Java指令重排在多线程环境下的解决方式

    Java指令重排在多线程环境下的解决方式

    这篇文章介绍了Java指令重排在多线程环境下的解决方式,文中通过示例代码介绍的非常详细。对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2022-04-04
  • idea创建maven父子工程导致子工程无法导入父工程依赖

    idea创建maven父子工程导致子工程无法导入父工程依赖

    创建maven父子工程时遇到一个问题,本文主要介绍了idea创建maven父子工程导致子工程无法导入父工程依赖,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
    2022-04-04
  • MybatisPlus特殊查询的实现介绍

    MybatisPlus特殊查询的实现介绍

    这篇文章主要介绍了MybatisPlus查询投影、聚合查询、分组查询、等值查询、范围查询、模糊查询、排序查询,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习吧
    2022-10-10
  • idea生成类注释和方法注释的正确方法(推荐)

    idea生成类注释和方法注释的正确方法(推荐)

    这篇文章主要介绍了idea生成类注释和方法注释的正确方法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下
    2020-11-11
  • MyBatis中模糊查询使用CONCAT('%',#{str},'%')出错的解决

    MyBatis中模糊查询使用CONCAT('%',#{str},'%')出错的解

    这篇文章主要介绍了MyBatis中模糊查询使用CONCAT('%',#{str},'%')出错的解决,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教
    2022-01-01
  • Spring boot使用logback实现日志管理过程详解

    Spring boot使用logback实现日志管理过程详解

    这篇文章主要介绍了Spring boot使用logback实现日志管理过程详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下
    2020-06-06
  • 详解SpringBoot中@PostMapping注解的用法

    详解SpringBoot中@PostMapping注解的用法

    在SpringBoot中,我们经常需要编写RESTful Web服务,以便于客户端与服务器之间的通信,@PostMapping注解可以让我们更方便地编写POST请求处理方法,在本文中,我们将介绍@PostMapping注解的作用、原理,以及如何在SpringBoot应用程序中使用它
    2023-06-06

最新评论