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+"元");
 }

}

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

(0)

相关推荐

  • Java多线程编程小实例模拟停车场系统

    下面分享的是一个Java多线程模拟停车场系统的小实例(Java的应用还是很广泛的,哈哈),具体代码如下: Park类 public class Park { boolean []park=new boolean[3]; public boolean equals() { return true; } } Car: public class Car { private String number; private int position=0; public Car(String number)

  • JAVA实现简单停车场系统代码

    JAVA项目中正号需要一个停车收费系统,就整理出来给大家分享一下,希望对大家有所帮助. <h1 accuse="qTitle" style="margin: 0px; padding: 0px; font-size: 16px; font-stretch: normal; line-height: 26px; font-family: "PingFang SC", "Lantinghei SC", "Microsoft

  • java实现租车系统

    今天用JAVA编写了一个租车系统,过程中主要遇到的两个问题: 1.输出数组信息问题: 在得到cars[]数组后,要生成租车信息表,目前有两种思路:一是用循环输出:二是用Arrays.toString()输出数组信息. 用toString()方法输出数组输出--@--形式的哈希码地址,这里需要对toString()方法进行重写,在数组涉及到的类中进行重写. 不过用第二种方法输出的其实还是一个数组,形式如图所示.那么问题来了--还有没有更好的输出方法呢? 2.父类方法不能访问子类成员变量: 本来在父

  • java多线程之火车售票系统模拟实例

    1.前言 为了学习多线程共享与通信,我们模拟一个火车售票系统,假设有10张火车票,三个窗口(也就是三个线程)同时进行售票. 2.非同步代码 package com.tl.skyLine.thread; /** * Created by tl on 17/3/6. */ public class SellTicket { public static void main(String[] args) { TicketWindow tw = new TicketWindow(); Thread t1

  • 图书管理系统java代码实现

    本文实例为大家分享了java实现图书管理系统的具体代码,供大家参考,具体内容如下 /* (程序头部注释开始) * 程序的版权和版本声明部分 * Copyright (c) 2011, 烟台大学计算机学院学生 * All rights reserved. * 文件名称:    <图书管理系统--java>                          * 作    者:       刘江波                       * 完成日期:    2012     年  3    

  • Java实现一个达达租车系统的步骤详解

    本文介绍的是利用java编写一个控制台版的"达达租车系统",下面话不多说了,来看看详细实现方法吧. 实现目标 java编写一个控制台版的"达达租车系统" 实现功能 1.展示所有可租车辆 2.选择车型.租车量 3.展示租车清单,包含:总金额.总载货量及其车型.总载人量及其车型 三大分析 数据模型分析 业务模型分析 显示和流程分析 实现效果 租车页面 租车账单 实现思路 首先定义一个Car类,它包含基本功能:车名.载客数.载货量.日租金.接着创建三个小类,分别是客车类.

  • java编写简单的ATM存取系统

    新手练手必备~ 密码账户为: 先创建账户类: 复制代码 代码如下: package cn.Atm; /** * @author 偶my耶 */ import java.io.*; import com.project.project; public class Account {   private String number=null;   private String name=null;   private String password=null;   private double mo

  • Java+Mysql学生管理系统源码

    最近正在学java和数据库,想起以前写的学生管理系统,都是从网上下载,敷衍了事.闲来无事,也就自己写了一个,不过功能实现的不是很多. 开发语言:java: 开发环境:Mysql, java: 开发工具:eclipse 开发此案例,首先得在电脑上有java开发环境和Mysql, java开发环境与Mysql的搭建,就不再叙述了,如果需要,请联系我最下面的联系方式:dingyelf@aliyun.com 此次系统比较简易:数据库中只有一个表:stu;功能:能够对学生增加.删除.修改. 开发步骤:  

  • 图书管理系统java版

    本文的目的就是通过图书管理系统掌握数据库编程技术,能正确连接数据库,能对数据库中信息进行查询.插入.删除.修改. 内容:在数据库中创建一张书目信息表,包括书名.作者.出版社.出版日期.书号.价格字段.设计一个GUI界面进行书目管理.在该界面上有四个选项卡,分别是查询.插入.删除.修改.点击查询选项卡,出现的界面上有书名.作者.出版社.书号四个文本框,一个按钮和一个只读文本区.文本框内容可以为空,输入相应的查询信息后(例如根据书名查询可以仅输入书名),点击界面上的"查询"按钮,可以在界面

  • Java实现简单汽车租赁系统

    本文实例为大家分享了Java实现简单汽车租赁系统的具体代码,供大家参考,具体内容如下 需求如下:  问题分析: 首先应当构建一个MotoVehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌:还包含一个no属性,表示汽车牌号: package cn.jbit.car; public abstract class MotoVehicle { private String no; private String brand; /** * 无参构造方法 */ public

随机推荐