22、Java——汽车租赁系统(对象+集合)

简介: 22、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.png

开发思路


       (1)明确需求


       (2)编码顺序


               1)、先完成父类(汽车类)的编写


               2)、再完成子类(客车类和轿车类)的编写


               3)、然后再完成汽车业务类的编写


               4)、最后完成测试类的编写


       (3)逻辑测试


类的属性和方法


属性:


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

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

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

汽车业务类:忽略

汽车租赁管理类:忽略


方法:


       定义租车的方法,不同类型的汽车采用不同租金方法进行计算。



1.png

代码展示


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、汽车业务类


 

用ArrayList automobiles=new ArrayList();集合的方式存储数据。
package cn.bdqn.demo01;
import java.util.ArrayList;
//汽车业务类
public class AutomobileMethod {
  //汽车类型的集合
  ArrayList automobiles=new ArrayList();
  //初始化数据
  public void initData(){
  Automobile ab1=new Car("京NY28588", "宝马", 800, "X6");
  Automobile ab2=new Car("京CNY3284", "宝马", 600, "550i");
  Automobile ab3=new Car("京NT37465", "别克", 300, "林荫大道");
  Automobile ab4=new Car("京NT96968", "别克", 600, "GL8");
  Automobile ab5=new Bus("京6566754", "金杯", 800, 16);
  Automobile ab6=new Bus("京8696997", "金龙", 800, 16);
  Automobile ab7=new Bus("京9696996", "金杯", 1500, 34);
  Automobile ab8=new Bus("京8696998", "金龙", 1500, 34);
  automobiles.add(ab1);
  automobiles.add(ab2);
  automobiles.add(ab3);
  automobiles.add(ab4);
  automobiles.add(ab5);
  automobiles.add(ab6);
  automobiles.add(ab7);
  automobiles.add(ab8);
  }
  //租车
  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.demo01;
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(auto.getDayRent());
  System.out.println("请选择租赁天数:");
  int days=sc.nextInt();
  double money=auto.calRent(days);
  System.out.println("租车成功!请按照"+auto.getNumberPlate()+"车牌号取车!租金为:"+money);
  }
}


效果展示


2.png


 

相关文章
|
11月前
|
设计模式 网络协议 数据可视化
Java 设计模式之状态模式:让对象的行为随状态优雅变化
状态模式通过封装对象的状态,使行为随状态变化而改变。以订单为例,将待支付、已支付等状态独立成类,消除冗长条件判断,提升代码可维护性与扩展性,适用于状态多、转换复杂的场景。
1184 157
|
12月前
|
JavaScript Java 大数据
基于JavaWeb的销售管理系统设计系统
本系统基于Java、MySQL、Spring Boot与Vue.js技术,构建高效、可扩展的销售管理平台,实现客户、订单、数据可视化等全流程自动化管理,提升企业运营效率与决策能力。
|
Java 大数据 API
Java Stream API:现代集合处理与函数式编程
Java Stream API:现代集合处理与函数式编程
477 100
|
Java API 数据处理
Java Stream API:现代集合处理新方式
Java Stream API:现代集合处理新方式
451 101
|
算法 Java
50道java集合面试题
50道 java 集合面试题
|
11月前
|
移动开发 监控 小程序
java家政平台源码,家政上门清洁系统源码,数据多端互通,可直接搭建使用
一款基于Java+SpringBoot+Vue+UniApp开发的家政上门系统,支持小程序、APP、H5、公众号多端互通。涵盖用户端、技工端与管理后台,支持多城市、服务分类、在线预约、微信支付、抢单派单、技能认证、钱包提现等功能,源码开源,可直接部署使用。
849 24
|
11月前
|
设计模式 消息中间件 传感器
Java 设计模式之观察者模式:构建松耦合的事件响应系统
观察者模式是Java中常用的行为型设计模式,用于构建松耦合的事件响应系统。当一个对象状态改变时,所有依赖它的观察者将自动收到通知并更新。该模式通过抽象耦合实现发布-订阅机制,广泛应用于GUI事件处理、消息通知、数据监控等场景,具有良好的可扩展性和维护性。
779 8
|
11月前
|
安全 前端开发 Java
使用Java编写UDP协议的简易群聊系统
通过这个基础框架,你可以进一步增加更多的功能,例如用户认证、消息格式化、更复杂的客户端界面等,来丰富你的群聊系统。
380 11
|
11月前
|
机器学习/深度学习 人工智能 自然语言处理
Java与生成式AI:构建内容生成与创意辅助系统
生成式AI正在重塑内容创作、软件开发和创意设计的方式。本文深入探讨如何在Java生态中构建支持文本、图像、代码等多种生成任务的创意辅助系统。我们将完整展示集成大型生成模型(如GPT、Stable Diffusion)、处理生成任务队列、优化生成结果以及构建企业级生成式AI应用的全流程,为Java开发者提供构建下一代创意辅助系统的完整技术方案。
518 10
|
11月前
|
存储 算法 安全
Java集合框架:理解类型多样性与限制
总之,在 Java 题材中正确地应对多样化与约束条件要求开发人员深入理解面向对象原则、范式编程思想以及JVM工作机理等核心知识点。通过精心设计与周密规划能够有效地利用 Java 高级特征打造出既健壮又灵活易维护系统软件产品。
282 7