Java代碼~~汽車租賃

租車信息:java

輸出結果:算法

 

代碼:數組

一、先定義抽象類(汽車類:Moto)ide

 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     
36     @Override
37     public String toString() {
38         return "Moto [id=" + id + ", brand=" + brand + ", preRent=" + preRent + "]";
39     }
40     
41 }

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 > 7 && days <= 30) {
25             //days大於7天,9折
26             return getPreRent()*days*0.9;
27         }else if (days > 30 && days <= 150) {
28             //days大於30天,8折
29             return getPreRent()*days*0.8;
30         }else if (days > 150) {
31             //days大於150天,7折
32             return getPreRent()*days*0.7;
33         }
34         return 0;
35     }
36 }

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 >= 3 && days < 7) {
24             //days大於3天,9折
25             return getPreRent()*days*0.9;
26         }else if (days >= 7 && days <30) {
27             //days大於3天,8折
28             return getPreRent()*days*0.8;
29         }else if (days >= 30 && days <150) {
30             //days大於30天,7折
31             return getPreRent()*days*0.7;
32         }else if (days >= 150) {
33             //days大於150天,6折
34             return getPreRent()*days*0.6;
35         }
36         return 0;
37     }
38 }

4.定義汽車業務類this

 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 static Moto motoLeaseOut(String brand,String type,int seat) {
21         Moto che = null;
22          //for循環遍歷數組motos
23          for(int i=0;i<motos.length;i++){
24           //判斷Moto類的motos[i]的類型是否和Car同樣
25           if(motos[i] instanceof Car){
26           //Moto類的motos[i]向下轉型變成子類Car
27           Car car = (Car)motos[i];
28           if(car.getBrand().equals(brand) && car.getType().equals(type)){
29            che=car;
30            break;
31           }
32           }else{
33           //Moto類的motos[i]向下轉型變成子類Bus
34           Bus bus = (Bus)motos[i];
35           if(bus.getBrand().equals(brand) && bus.getSeatCount() == seat){
36            che=bus;
37            break;
38           }
39           }
40          }
41          return che;
42     }
43 }

5.定義測試類spa

 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             System.out.println("***********歡迎光臨騰飛汽車租賃公司***********");
16             System.out.println("一、轎車           二、客車");
17             System.out.println("請輸入你要租賃的汽車類型:");
18             String brand = "";//品牌
19             String type = "";//型號
20             int seat = 0;//座位數
21             //獲取用戶輸入
22             Scanner scanner = new Scanner(System.in);
23             int flag = scanner.nextInt();
24             if (flag == 1) {
25                 Car car =null;
26                 System.out.println("請選擇你要租賃的汽車品牌:一、別克   二、寶馬");
27                 int flag1 = scanner.nextInt();
28                 if (flag1 == 1) {
29                     System.out.println("請選擇你要租賃的汽車類型:一、林蔭大道  二、GL8");
30                     int flag2 = scanner.nextInt();
31                     brand = "別克";
32                     if (flag2 == 1) {
33                         type = "林蔭大道";
34                     }else if (flag2 == 2) {
35                         type = "GL8";
36                     }
37                 }else if (flag1 ==2) {
38                     brand = "寶馬";
39                     System.out.println("請選擇你要租賃的汽車類型:一、X6  二、550i");
40                     int flag2 = scanner.nextInt();
41                     if (flag2 == 1) {
42                         type = "X6";
43                     }else if (flag2 == 2) {
44                         type = "550i";
45                     }
46                 }
47             }else if (flag ==2) {
48                 Bus bus = null;
49                 System.out.println("請選擇你要租賃的汽車品牌:一、金龍   二、金盃");
50                 int flag1 = scanner.nextInt();
51                 if (flag1 == 1) {
52                     brand = "金龍";
53                     System.out.println("請選擇你要租賃的汽車座位數:一、16座  二、34座");
54                     int flag2 = scanner.nextInt();
55                     if (flag2 == 1) {
56                         seat = 16;
57                     }else if (flag2 == 2) {
58                         seat = 34;
59                     }
60                 }else if (flag1 ==2) {
61                     brand = "金盃";
62                     System.out.println("請選擇你要租賃的汽車座位數:一、16座  二、34座");
63                     int flag2 = scanner.nextInt();
64                     if (flag2 == 1) {
65                         seat = 16;
66                     }else if (flag2 == 2) {
67                         seat = 34;
68                     }
69                 }
70             }
71             //租車
72             Moto che = MotoOperation.motoLeaseOut(brand,type,seat);
73             //獲取用戶租車天數
74             System.out.println("請輸入您的租車天數:");
75             int days = scanner.nextInt();
76             //計算租車錢
77             double money = che.calRent(days);
78             System.out.println("分配給您的汽車牌號是:"+che.getId());
79             System.out.println("您須要支付的租賃費用是:"+money+"元");
80         }
81         
82 
83 }
相關文章
相關標籤/搜索