欢迎来到代码驿站!

JAVA代码

当前位置:首页 > 软件编程 > JAVA代码

Java实现简单汽车租赁系统

时间:2021-06-14 09:47:08|栏目:JAVA代码|点击:

本文实例为大家分享了Java实现简单汽车租赁系统的具体代码,供大家参考,具体内容如下

需求如下:  

问题分析:

首先应当构建一个MotoVehicle的抽象(abstract)类,类里面包含一个brand属性,表示汽车品牌;还包含一个no属性,表示汽车牌号;

package cn.jbit.car;
 
public abstract class MotoVehicle {
 private String no;
 private String brand;
 /**
 * 无参构造方法
 */
 public MotoVehicle() {
 
 }
 /**
 * 有参构造方法
 * @param no 汽车牌号
 * @param brand 汽车品牌
 */
 public MotoVehicle(String no,String brand) {
 this.no=no;
 this.brand=brand;
 }
 
 public String getNo() {
 return no;
 }
 
 public String getBrand() {
 return brand;
 }
 public abstract int calRent(int days);
}

其次,应有Car类继承自MotoVehicle类,并有一个type属性,表示轿车型号,应有一个计算租金的方法calRent()

package cn.jbit.car;
 
public class Car extends MotoVehicle{
 private String type;
 public Car() {
 
 }
 public Car (String no,String brand,String type) {
 super(no,brand);
 this.type=type;
 }
 
 public String getType() {
 return type;
 }
 
 public void setType(String type) {
 this.type = type;
 }
 @Override
 public int calRent(int days) {
 // TODO Auto-generated method stub
 if("2".equals(type)) {
  return days*500;
 }
 else if ("1".equals(type)) {
  return days*600;
 }
 else {
  return 300*days;
 }
 } 
}

再次,应有Bus类继承自MotoVehicle类,并有一个CountSet属性,表示客车的容量,同样的,应有一个计算租金的方法calRent();

package cn.jbit.car;
 
public class Bus extends MotoVehicle {
 int CountSet;
 public Bus() {
 }
 /**
 * 带参构造函数
 */
 public Bus(String brand,String no,int CountSet) {
 super(brand,no);
 this.CountSet=CountSet; 
 }
 public int getCountSet() {
 return CountSet;
 } 
 public void setCountSet(int countSet) {
 CountSet = countSet;
 }
 
 @Override
 public int calRent(int days) {
 // TODO Auto-generated method stub
 if(CountSet<16) {
  return 800*days;
 }
 else {
  return 1600*days;
 }
 }
 
}

最后,以上三类应在test类中测试;

package cn.jbit.car;
import java.util.Scanner;
 
public class Test {
 public static void main(String[] args) {
 String no,brand,mtype;
 int countSet,days;
 Scanner input=new Scanner(System.in);
 System.out.println("*****欢迎来到汽车租赁公司!******");
 System.out.println("请输入天数:");
 days=input.nextInt();
 System.out.println("请输入车辆类型:");
 System.out.println("1、轿车  2、客车");
 mtype=input.next();
 if("1".equals(mtype)) {
  System.out.println("请输入轿车品牌:");
  System.out.println("1、宝马 2、别克");
  brand=input.next();
  if("1".equals(brand)) {
  System.out.println("2、宝马550i:500");
  System.out.println("请输入轿车型号:");
  mtype=input.next();
  System.out.println("请输入辆数:");
  int count=input.nextInt();
  Car car=new Car("辽B000",brand,mtype);
  System.out.println("您需支付:"+count*car.calRent(days));
  
  }
  else {
  System.out.println("1、别克商务GL8:600  3、别克林荫大道:300");
  mtype=input.next();
  System.out.println("请输入辆数:");
  int count=input.nextInt();
  Car car=new Car("辽B000",brand,mtype);
  System.out.println("您需支付:"+count*car.calRent(days));
  }
 }
 else {
  System.out.println("请输入品牌:");
  System.out.println("1、金杯  2、金龙");
  brand=input.next();
  System.out.println("请输入座位数:");
  countSet=input.nextInt();
  System.out.println("请输入辆数:");
  int count=input.nextInt();
  Bus b=new Bus(brand,"辽B000",countSet);
  System.out.println("您需支付:"+b.calRent(days)*count);
 }
 }
 
}

上一篇:String s = new String('a ') 到底产生几个对象

栏    目:JAVA代码

下一篇:intellij idea使用git stash暂存一次提交的操作

本文标题:Java实现简单汽车租赁系统

本文地址:http://www.codeinn.net/misctech/141833.html

推荐教程

广告投放 | 联系我们 | 版权申明

重要申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:914707363 | 邮箱:codeinn#126.com(#换成@)

Copyright © 2020 代码驿站 版权所有