首页 > 编程 > Java > 正文

Java实现简单汽车租赁系统

2019-11-26 09:18:30
字体:
来源:转载
供稿:网友

本文实例为大家分享了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); } } }

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表