汽车租赁案例分析

如题,要求:

  1. 定义汽车类(抽象类,类名:Moto),

拥有属性:车牌号(id,品牌(brand,日租金(preRent

拥有方法:计算租金的方法(抽象方法名double  calRent(int days))

  1. 定义轿车类(类名:Car),继承于汽车类。

拥有自己独有属性:型号(type.

拥有方法:重写计算租金的方法。

  1. 定义客车类(类名:Bus),继承于汽车类。

拥有自己独有属性属性:座位数(seatCount.

拥有方法:重写计算租金的方法。

 

  1. 定义汽车业务类,(类名:MotoOperation)

拥有属性:Moto  motos[]=new  Moto[8];该对象数组用于保存汽车公司的所有汽车(包括轿车和,客车)

拥有方法:

  1. 初始化方法(方法名:init())对moto数组中保存的汽车进行初始化,用于保存该系统所拥有的所有汽车(通过构造方法实现对象的初始化,)例如:

motos[0] = new Car("NY28588", "宝马", "X6",800);

motos[4] = new Bus("6566754", "金杯", 16,800);

  1. 租赁汽车的方法(方法名:

public Moto motoLeaseOut(String brand,String type,int seat))

该方法的作用:表示租一辆汽车给客户。

注意:该方法的返回值类型是一个对象(说明:父类对象),功能是根据传递的参数,在初始化的对象数组中找到自己的车型(返回该车对应的子类对象)。

  1. 汽车租赁管理类(类名:TestRent)
    1. 在程序入口方法中要实现该系统汽车的初始化。
    2. 根据提示信息,保存对应的输入值。
    3. 调用租赁汽车的方法,取得租赁的汽车对象
    4. 输出汽车的车牌号及汽车的租金

 

测试项目提示:

提示一:关于客车的测试:

 

提示二:关于轿车的测试:

 

 原理分析:

1.创建抽象父类对象车类,设置父类的特有属性和租赁抽象方法
* 2.创建子类轿车和公交实现类,设置子类特有的属性和重写父类的抽象租赁方法
* 3.创建汽车业务管理类,该类管理的是已有车辆的信息库:用数组存储,和查找车辆的方法,
* 查找车辆的方法即在测试类中用户选择的车辆型号调用业务管理类的方法进行判断匹配,
* 即在该方法中判断用户输入的车型是轿车还是公交,根据用户的输入的车型在父类对象数组中查找想要的车型,
* 再强转成子类对象判断对应轿车(公交)的品牌(座位数)来查寻对应的车辆信息,因为需要查找对应i型号的车辆信息
* 所以强转成子类,因此最后返回的类的信息为子类对象的信息
* 因此在匹配完成后,用户的车辆选择目的达成,该信息用新创建的父类对象空容器接受,而父类为抽象类,
* 所以需要实现类去调用父类的抽象方法去实现,因此最后调用的是继承了抽象父类对象的子类重写父类的抽象方法进行租赁

代码如下:

package carRent;
/**
 * 
 * 汽车租赁抽象类
 *
 */
public abstract class Moto1 {
       private String id;//车牌号
       private String Brand;//品牌
       private Double preRent;//日租金
       //构造方法
    public Moto1(String id, String brand, Double preRent) {
        this.id = id;
        Brand = brand;
        this.preRent = preRent;
    }
    public String getId() {
        return id;
    }
    public void setId(String id) {
        this.id = id;
    }
    public String getBrand() {
        return Brand;
    }
    public void setBrand(String brand) {
        Brand = brand;
    }
    public Double getPreRent() {
        return preRent;
    }
    public void setPreRent(Double preRent) {
        this.preRent = preRent;
    }
//get与set

    //计算日租金的方法:抽象的
    public abstract double carRent(int days);
}
package carRent;
/**
 * 
 * 轿车,实现类
 *
 */
public class Car1 extends Moto1{
       private String type;
       //私有属性
//构造方法
    public Car1(String id, String brand, Double preRent, String type) {
        super(id, brand, preRent);
        this.type = type;
    }
    public String getType() {
        return type;
    }
    public void setType(String type) {
        this.type = type;
    }
//get与set
    @Override
    public double carRent(int days) {
        if(days<=0) {
            System.err.println("请您别开玩笑!至少租一天!");
        }else if(days>0&&days<=7) {
            return this.getPreRent()*days*1;        
            }
         else if(days>7&&days<=30) {
            return this.getPreRent()*days*0.9;        
            }
         else if(days>30&&days<=150) {
                return this.getPreRent()*days*0.8;        
                }
         else if(days>150) {
                return this.getPreRent()*days*0.7;        
        }
        return 0;
    }      
}
 1 package carRent;
 2 /**
 3  * 
 4  * 客车,实现类
 5  *
 6  */
 7 public class Bus1 extends Moto1{
 8     private int seatCount;
 9     //私有属性
10 //构造方法
11     public Bus1(String id, String brand, Double preRent, int seatCount) {
12         super(id, brand, preRent);
13         this.seatCount = seatCount;
14     }
15     
16 public int getSeatCount() {
17         return seatCount;
18     }
19 
20     public void setSeatCount(int seatCount) {
21         this.seatCount = seatCount;
22     }
23 
24     //get与set
25     @Override
26     public double carRent(int days) {
27         if(days<=0) {
28             System.err.println("请您别开玩笑!至少租一天!");
29         }else if(days>=0&&days<3) {
30             return this.getPreRent()*days*1;        
31             }
32          else if(days>=3&&days<=7) {
33             return this.getPreRent()*days*0.9;        
34             }
35          else if(days>=7&&days<30) {
36                 return this.getPreRent()*days*0.8;        
37                 }
38          else if(days>=30&&days<150) {
39                 return this.getPreRent()*days*0.7;        
40         }else if(days>=150) {
41             return this.getPreRent()*days*0.6;        
42     }
43         return 0;
44     }      
45 }

package carRent;
/**
 * 业务层对象,专门进行汽车选购,选择客户心仪的汽车进行租赁。
 *     业务层是用户调用对象,在用户中写main方法就行了,业务层一般都不需要自身运行。
 */
public class MotoOperation1 {

    //创建一个全局的数组对象
    Moto1[] motos = new Moto1[8];
    
    //初始化数组对象
    public void init() {
        motos[0] = new Car1("京NY28588","宝马",800.00,"X6");
        motos[1] = new Car1("京CNY3284","宝马",600.00,"550i");
        motos[2] = new Car1("京NT37465","别克",300.00,"林荫大道");
        motos[3] = new Car1("京NT96968","别克",600.00,"GL8");
        motos[4] = new Bus1("京6566754","金杯",800.00,16);
        motos[5] = new Bus1("京8696997","金龙",800.00,16);
        motos[6] = new Bus1("京9696996","金杯",1500.00,34);
        motos[7] = new Bus1("京8696998","金龙",1500.00,34);
    }
    
    /*
     * 汽车租赁的方法
     *     参数1:品牌
     *     参数2:类型
     *     参数3:座位数
     */
    public Moto1 motoLeaseOut(String brand, String type, int seat) {
        this.init();
        //循环遍历数组
        for(int i = 0; i < motos.length; i++) {
            //判断当前的品牌
            if(brand.equals(motos[i].getBrand())) {
                //如果当前判断的结果,满足,就是我需要的品牌:剩下的两个参数都是子类特有参数,所以我们需要继续强转后判断
                if(motos[i] instanceof Car1) {
                    //强转成小汽车
                    Car1 car = (Car1) motos[i];
                    if(type.equals(car.getType())) {
                        //正式我要的类型
                        return car;
                    }
                }
                if(motos[i] instanceof Bus1) {
                    //强转成大客车
                    Bus1 bus = (Bus1) motos[i];
                    //判断座位数
                    if(seat == bus.getSeatCount()) {
                        //正式我要的客车
                        return bus;
                    }
                }
            }
        }
        //循环搜索如果没有搜索到心仪的汽车,返回空:客户要兰博基尼,我作为汽车租赁公司,还去给他买一个么?
        return null;
    }    
}
 
  
 


1
package carRent;
 2 import java.util.Scanner;
 3 
 4 /**
 5  * 用户租赁汽车的一个窗口
 6  */
 7  *@date:2019年12月12日
 8  *@description:carRent
 9  *@author:博
10  */
11 public class TestRent1 {
12 
13     public static void main(String[] args) {
14         
15         //首先将我们需要的对象初始化
16         MotoOperation1 mo = new MotoOperation1();
17         //将汽车初始化
18         mo.init();
19         
20         //设置一个空容器
21         Moto1 moto = null;
22         
23         //进入系统了
24         System.out.println("**********欢迎光临腾飞汽车租赁公司**********");
25         System.out.println("选择汽车租赁的类型:1、轿车   2、客车");
26         Scanner scanner = new Scanner(System.in);
27         int flag = scanner.nextInt();
28         
29         if(flag == 1) {
30             //轿车
31             System.out.println("请选择您的品牌:1、宝马   2、别克");
32             int brand = scanner.nextInt();
33             if(brand == 1) {
34                 //宝马
35                 System.out.println("请选择您的类型:1、X6  2、550i");
36                 int type = scanner.nextInt();
37                 if(type == 1) {
38                     //X6,调用租赁方法
39                     moto = mo.motoLeaseOut("宝马", "X6", 5);
40                 }else if(type == 2){
41                     //550i
42                     moto = mo.motoLeaseOut("宝马", "550i", 5);
43                 }
44             }else if(brand == 2) {
45                 //别克   
46                 System.out.println("请选择您的类型:1、GL8  2、林荫大道");
47                 int type = scanner.nextInt();
48                 if(type == 1) {
49                     //X6,调用租赁方法
50                     moto = mo.motoLeaseOut("别克", "GL8", 5);
51                 }else if(type == 2){
52                     //550i
53                     moto = mo.motoLeaseOut("别克", "林荫大道", 5);
54                 }
55             }
56         }else if(flag == 2) {
57             //客车
58             System.out.println("请选择您的品牌:1、金杯   2、金龙");
59             int brand = scanner.nextInt();
60             if(brand == 1) {
61                 //金杯
62                 System.out.println("请选择您要的座位数:1、16  2、34");
63                 int seat = scanner.nextInt();
64                 if(seat == 1) {
65                     //16
66                       moto = mo.motoLeaseOut("金杯", "中巴", 16);
67                 }else if(seat == 2) {
68                     //34
69                     moto = mo.motoLeaseOut("金杯", "大巴", 34);
70                 }
71             }else if(brand == 2) {
72                 //金龙
73                 System.out.println("请选择您要的座位数:1、16  2、34");
74                 int seat = scanner.nextInt();
75                 if(seat == 1) {
76                     //16
77                     moto = mo.motoLeaseOut("金龙", "中巴", 16);
78                 }else if(seat == 2) {
79                     //34
80                     moto = mo.motoLeaseOut("金龙", "大巴", 34);
81                 }
82             }
83         }
84         //客户汽车匹配完成
85                 if(moto != null) {
86                     //让用户继续输入租赁的天数
87                     System.out.println("请输入您要租赁的天数:");
88                     int days = scanner.nextInt();
89                     //告诉用户分配的车牌号
90                     System.out.println("给您分配的汽车车牌号为:" + moto.getId());
91                     System.out.println("您当前需要支付的总金额为:" + moto.carRent(days));
92                 }else {
93                     System.out.println("很抱歉先生:当前的公司内,暂无您要的汽车类型!请重新选择!");
94                 }
95     }
96 }

猜你喜欢

转载自www.cnblogs.com/wangbo11/p/12066116.html
今日推荐