租车信息:

javadvd租借系统 java租车系统源代码_System

输出结果:

javadvd租借系统 java租车系统源代码_构造方法_02


javadvd租借系统 java租车系统源代码_Moto_03

 

代码:

1、先定义抽象类(汽车类:Moto)

1 package cn.aura.demo01;
 2 
 3 public abstract class Moto {
 4     //公共属性
 5     private String id;//车牌号
 6     private String brand;//品牌
 7     private int preRent;//日租金
 8     //构造方法
 9     public Moto(String id, String brand, int preRent) {
10         this.id = id;
11         this.brand = brand;
12         this.preRent = preRent;
13     }
14     //set和get方法
15     public String getId() {
16         return id;
17     }
18     public void setId(String id) {
19         this.id = id;
20     }
21     public String getBrand() {
22         return brand;
23     }
24     public void setBrand(String brand) {
25         this.brand = brand;
26     }
27     public int getPreRent() {
28         return preRent;
29     }
30     public void setPreRent(int preRent) {
31         this.preRent = preRent;
32     }
33     //计算租金方法
34     public abstract double  calRent(int days);
35 }

2.定义轿车类继承汽车类

1 package cn.aura.demo01;
 2 /**
 3  * 轿车类
 4  */
 5 public class Car extends Moto{
 6     //私有属性
 7     private String type;//型号
 8     //set和get方法
 9     public String getType() {
10         return type;
11     }
12     public void setType(String type) {
13         this.type = type;
14     }
15     //构造方法
16     public Car(String id, String brand, String type,int preRent) {
17         super(id, brand, preRent);
18         this.type = type;
19     }
20     
21     //重写计算租金的算法
22     @Override
23     public double calRent(int days) {
24         if(days >0 && days <= 7) {
25             //days小于7天,不打折
26             return getPreRent()*days;
27         }else if (days > 7 && days <= 30) {
28             //days大于7天,9折
29             return getPreRent()*days*0.9;
30         }else if (days > 30 && days <= 150) {
31             //days大于30天,8折
32             return getPreRent()*days*0.8;
33         }else if (days > 150) {
34             //days大于150天,7折
35             return getPreRent()*days*0.7;
36         }
37         return 0;
38     }
39 }

3.定义客车类

1 package cn.aura.demo01;
 2 /**
 3  * 客车类
 4  */
 5 public class Bus extends Moto{
 6     //私有属性
 7     private int seatCount;//座位数
 8     //set和get方法
 9     public int getSeatCount() {
10         return seatCount;
11     }
12     public void setSeatCount(int seatCount) {
13         this.seatCount = seatCount;
14     }
15     //构造方法
16     public Bus(String id, String brand, int seatCount, int preRent) {
17         super(id, brand, preRent);
18         this.seatCount = seatCount;
19     }
20     //重写计算租金的算法
21     @Override
22     public double calRent(int days) {
23         if(days >0 && days <3) {
24             //days小于3天,不打折
25             return getPreRent()*days;
26         }else if (days >= 3 && days < 7) {
27             //days大于3天,9折
28             return getPreRent()*days*0.9;
29         }else if (days >= 7 && days <30) {
30             //days大于3天,8折
31             return getPreRent()*days*0.8;
32         }else if (days >= 30 && days <150) {
33             //days大于30天,7折
34             return getPreRent()*days*0.7;
35         }else if (days >= 150) {
36             //days大于150天,6折
37             return getPreRent()*days*0.6;
38         }
39         return 0;
40     }
41 }

4.定义汽车业务类

1 package cn.aura.demo01;
 2 /**
 3  * 定义汽车业务类
 4  */
 5 public class MotoOperation {
 6     //初始化数组
 7     public static Moto[] motos = new Moto[8];
 8     //创建8个汽车对象
 9     public  void init() {
10         motos[0] = new Car("京NY28558", "宝马", "X6", 800);
11         motos[1] = new Car("京CNY3284", "宝马", "550i", 600);
12         motos[2] = new Car("京NT37465", "别克", "林荫大道", 300);
13         motos[3] = new Car("京NT96968", "别克", "GL8", 600);
14         motos[4] = new Bus("京6566754", "金杯", 16, 800);
15         motos[5] = new Bus("京8696997", "金龙", 16, 800);
16         motos[6] = new Bus("京9696996", "金杯", 34, 1500);
17         motos[7] = new Bus("京8696998", "金龙", 34, 1500);
18     }
19     //租赁汽车的方法
20     public  Moto motoLeaseOut(String brand,String type,int seat) {
21          //for循环遍历数组motos
22          for(int i=0;i<motos.length;i++){
23              if (brand.equals(motos[i].getBrand())) {
24                 // 判断Moto类的motos[i]的类型是否和Car一样
25                 if (motos[i] instanceof Car) {
26                     // 强转成小汽车Car
27                     Car car = (Car) motos[i];
28                     if (type.equals(car.getType())) {
29                         return car;
30                     }
31                 }
32                 if(motos[i] instanceof Bus){
33                     // 强转成大客车Bus
34                     Bus bus = (Bus) motos[i];
35                     if (seat == bus.getSeatCount()) {
36                         return bus;
37                     }
38                 }
39             }
40           
41          }
42          //如果没有就返回空
43          return null;
44     }
45 }

5.定义测试类

1 package cn.aura.demo01;
 2 
 3 import java.util.Scanner;
 4 
 5 /**
 6  * 汽车租赁管理类
 7  */
 8 public class TestRent {
 9     //主函数
10     public static void main(String[] args) {
11         MotoOperation motoOperation = new MotoOperation();
12         // 初始化汽车
13         motoOperation.init();
14         //设置一个空容器
15         Moto moto = null;
16         // 提示信息
17         System.out.println("***********欢迎光临腾飞汽车租赁公司***********");
18         System.out.println("1、轿车           2、客车");
19         System.out.println("请输入你要租赁的汽车类型:");
20         // 获取用户输入
21         Scanner scanner = new Scanner(System.in);
22         int flag = scanner.nextInt();
23         if (flag == 1) {
24             //轿车
25             System.out.println("请选择你要租赁的汽车品牌:1、别克   2、宝马");
26             int brand = scanner.nextInt();
27             if (brand == 1) {
28                 System.out.println("请选择你要租赁的汽车类型:1、林荫大道  2、GL8");
29                 int type = scanner.nextInt();
30                 if (type == 1) {
31                     moto = motoOperation.motoLeaseOut("别克", "林荫大道", 4);
32                 } else if (type == 2) {
33                     moto = motoOperation.motoLeaseOut("别克", "GL8", 4);
34                 }
35             } else if (brand == 2) {
36                 System.out.println("请选择你要租赁的汽车类型:1、X6  2、550i");
37                 int type = scanner.nextInt();
38                 if (type == 1) {
39                     moto = motoOperation.motoLeaseOut("宝马", "X6", 4);
40                 } else if (type == 2) {
41                     moto = motoOperation.motoLeaseOut("别克", "550i", 4);
42                 }
43             }
44         } else if (flag == 2) {
45             //客车
46             System.out.println("请选择你要租赁的汽车品牌:1、金龙   2、金杯");
47             int brand = scanner.nextInt();
48             if (brand == 1) {
49                 System.out.println("请选择你要租赁的汽车座位数:1、16座  2、34座");
50                 int type = scanner.nextInt();
51                 if (type == 1) {
52                     moto = motoOperation.motoLeaseOut("金龙", "", 16);
53                 } else if (type == 2) {
54                     moto = motoOperation.motoLeaseOut("金龙", "", 34);
55                 }
56             } else if (brand == 2) {
57                 System.out.println("请选择你要租赁的汽车座位数:1、16座  2、34座");
58                 int type = scanner.nextInt();
59                 if (type == 1) {
60                     moto = motoOperation.motoLeaseOut("金杯", "", 16);
61                 } else if (type == 2) {
62                     moto = motoOperation.motoLeaseOut("金杯", "", 34);
63                 }
64             }
65         }
66         //客户汽车匹配完成
67         if (moto != null) {
68             // 获取用户租车天数
69             System.out.println("请输入您的租车天数:");
70             int days = scanner.nextInt();
71             // 计算租车钱
72             double money = moto.calRent(days);
73             System.out.println("分配给您的汽车牌号是:" + moto.getId());
74             System.out.println("您需要支付的租赁费用是:" + money + "元");
75         }else {
76             System.out.println("很抱歉先生:当前的公司内,暂无您要的汽车类型!请重新选择!");
77         }
78         
79     }
80         
81 
82 }