17、Java——汽车租赁系统(对象+数组)

简介: 17、Java——汽车租赁系统(对象+数组)

项目需求


车型

具体信息

日租金

折扣

轿车

宝马X6(京NY28588)

800

days>7天9折

days>30天8折

days>150天7折

宝马550i(京CNY3284)

600

别克林荫大道(京NT37465)

300

别克GL8(京NT96968)

600

客车

金杯,16座(京6566754)

800

days>=3天9折

days>=7天8折

days>=30天7折

days>=150天6折

金龙,16座(京8696997)

金杯,34座(京9696996)

1500

金龙,34座(京8696998)


设计步骤


1.gif


类的属性和方法

属性:


汽车类:车牌号、品牌、日租金

客车类:车牌号、品牌、日租金、座位数

轿车类:车牌号、品牌、日租金、型号

汽车业务类:忽略

汽车租赁管理类:忽略方法:


1.gif


代码展示


1、汽车类(父类)


package cn.bdqn.demo06;
public abstract class Automobile {
  // 定义汽车类的属性(车牌号、品牌、日租金)
  private String numberPlate;
  private String brand;
  private double dayRent;
  public Automobile() {
  super();
  }
  public Automobile(String numberPlate, String brand, double dayRent) {
  super();
  this.numberPlate = numberPlate;
  this.brand = brand;
  this.dayRent = dayRent;
  }
  public String getNumberPlate() {
  return numberPlate;
  }
  public void setNumberPlate(String numberPlate) {
  this.numberPlate = numberPlate;
  }
  public String getBrand() {
  return brand;
  }
  public void setBrand(String brand) {
  this.brand = brand;
  }
  public double getDayRent() {
  return dayRent;
  }
  public void setDayRent(double dayRent) {
  this.dayRent = dayRent;
  }
  //定义计算租金的抽象方法
  public abstract double calRent(int days);
}


2、轿车类(子类)


package cn.bdqn.demo06;
public class Car extends Automobile {
  // 定义特有属性
  private String type;
  public Car() {
  super();
  }
  public Car(String numberPlate, String brand, double dayRent, String type) {
  super(numberPlate, brand, dayRent);
  this.type = type;
  }
  public String getType() {
  return type;
  }
  public void setType(String type) {
  this.type = type;
  }
  @Override
  public double calRent(int days) {
  double discount=this.getDayRent()*days;
  if(days>150){
    discount*=0.7;
  }else if(days>30){
    discount*=0.8;
  }else if(days>7){
    discount*=0.9;
  }
  return discount;
  }
}


3、客车类(子类)


package cn.bdqn.demo06;
public class Bus extends Automobile {
  private int seat;
  public Bus() {
  super();
  }
  public Bus(String numberPlate, String brand, double dayRent, int seat) {
  super(numberPlate, brand, dayRent);
  this.seat = seat;
  }
  public int getSeat() {
  return seat;
  }
  public void setSeat(int seat) {
  this.seat = seat;
  }
  @Override
  public double calRent(int days) {
  double discount=this.getDayRent()*days;
  if(days>150){
    discount*=0.6;
  }else if(days>30){
    discount*=0.7;
  }else if(days>7){
    discount*=0.8;
  }else if(days>3){
    discount*=0.9;
  }
  return discount;
  }
}



4、汽车业务类


package cn.bdqn.demo06;
//汽车业务类
public class AutomobileMethod {
  //汽车类型的数组
  Automobile[] automobiles=new Automobile[8];
  //初始化数据
  public void initData(){
  automobiles[0]=new Car("京NY28588", "宝马", 800, "X6");
  automobiles[1]=new Car("京CNY3284", "宝马", 600, "550i");
  automobiles[2]=new Car("京NT37465", "别克", 300, "林荫大道");
  automobiles[3]=new Car("京NT96968", "别克", 600, "GL8");
  automobiles[4]=new Bus("京6566754", "金杯", 800, 16);
  automobiles[5]=new Bus("京8696997", "金龙", 800, 16);
  automobiles[6]=new Bus("京9696996", "金杯", 1500, 34);
  automobiles[7]=new Bus("京8696998", "金龙", 1500, 34);
  }
  //租车
  public Automobile autoMethod(String brand,String type,int seat){
  Automobile auto=null;
  for (Automobile autobile : automobiles) {
    if(autobile instanceof Car){
    Car car=(Car)autobile;
    if(car.getBrand().equals(brand)&&car.getType().equals(type)){
      auto=car;
      break;
    }
    }else if(autobile instanceof Bus){
    Bus bus=(Bus)autobile;
    if(bus.getBrand().equals(brand)&&bus.getSeat()==seat){
      auto=bus;
      break;
    }
    }
  }
  return auto;
  }
}

5、租车测试类


package cn.bdqn.demo06;
import java.util.Scanner;
//测试类
public class AutomobileLease {
  public static void main(String[] args) {
  Scanner sc=new Scanner(System.in);
  String brand=null,type=null;
  int seat=0;
  AutomobileMethod autoM=new AutomobileMethod();
  //获取所有车辆信息
  autoM.initData();
  System.out.println("********欢迎光临租赁公司********\n");
  System.out.println("请选择汽车类型:1、轿车\t2、客车");
  int autoType=sc.nextInt();
  if(autoType==1){
    System.out.println("请选择轿车品牌:1、宝马\t2、别克");
    if(sc.nextInt()==1){
    brand="宝马";
    System.out.println("请选择轿车型号:1、X6\t2、550i");
    type=(sc.nextInt()==1)?"X6":"550i";
    }else if(sc.nextInt()==2){
    brand="别克";
    System.out.println("请选择轿车型号:1、林荫大道\t2、GL8");
    type=(sc.nextInt()==1)?"林荫大道":"GL8";
    }
  }else if(autoType==2){
    System.out.println("请选择客车品牌:1、金杯\t2、金龙");
    brand=(sc.nextInt()==1)?"金杯":"金龙";
    System.out.println("请选择需要的座位数:1、16座\t2、34座");
    type=(sc.nextInt()==1)?"16":"34";
  }
  Automobile auto= autoM.autoMethod(brand, type, seat);
  System.out.println("请选择租赁天数:");
  int days=sc.nextInt();
  double money=auto.calRent(days);
  System.out.println("租车成功!请按照"+auto.getNumberPlate()+"车牌号取车!租金为:"+money);
  }
}


效果展示




  1.gif


1.gif

相关文章
|
16天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的服装商城管理系统
基于Java+Springboot+Vue开发的服装商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的服装商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
48 2
基于Java+Springboot+Vue开发的服装商城管理系统
|
2天前
|
Java Apache Maven
Java百项管理之新闻管理系统 熟悉java语法——大学生作业 有源码!!!可运行!!!
文章提供了使用Apache POI库在Java中创建和读取Excel文件的详细代码示例,包括写入数据到Excel和从Excel读取数据的方法。
15 6
Java百项管理之新闻管理系统 熟悉java语法——大学生作业 有源码!!!可运行!!!
|
13天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的大学竞赛报名管理系统
基于Java+Springboot+Vue开发的大学竞赛报名管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的大学竞赛报名管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
32 3
基于Java+Springboot+Vue开发的大学竞赛报名管理系统
|
14天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的蛋糕商城管理系统
基于Java+Springboot+Vue开发的蛋糕商城管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的蛋糕商城管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
40 3
基于Java+Springboot+Vue开发的蛋糕商城管理系统
|
14天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的美容预约管理系统
基于Java+Springboot+Vue开发的美容预约管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的美容预约管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
25 3
基于Java+Springboot+Vue开发的美容预约管理系统
|
16天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的房产销售管理系统
基于Java+Springboot+Vue开发的房产销售管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的房产销售管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
33 3
基于Java+Springboot+Vue开发的房产销售管理系统
|
17天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的反诈视频宣传系统
基于Java+Springboot+Vue开发的反诈视频宣传系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的反诈视频宣传管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
46 4
基于Java+Springboot+Vue开发的反诈视频宣传系统
|
18天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的健身房管理系统
基于Java+Springboot+Vue开发的健身房管理系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的健身房管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
47 5
基于Java+Springboot+Vue开发的健身房管理系统
|
16天前
|
Java
java基础(12)抽象类以及抽象方法abstract以及ArrayList对象使用
本文介绍了Java中抽象类和抽象方法的使用,以及ArrayList的基本操作,包括添加、获取、删除元素和判断列表是否为空。
16 2
java基础(12)抽象类以及抽象方法abstract以及ArrayList对象使用
|
17天前
|
前端开发 JavaScript Java
基于Java+Springboot+Vue开发的医院门诊预约挂号系统
基于Java+Springboot+Vue开发的医院门诊预约挂号系统(前后端分离),这是一项为大学生课程设计作业而开发的项目。该系统旨在帮助大学生学习并掌握Java编程技能,同时锻炼他们的项目设计与开发能力。通过学习基于Java的门诊预约挂号管理系统项目,大学生可以在实践中学习和提升自己的能力,为以后的职业发展打下坚实基础。
43 2
基于Java+Springboot+Vue开发的医院门诊预约挂号系统