本文實例為大家分享了java實現汽車租賃系統的具體代碼,供大家參考,具體內容如下
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
//車類 public abstract class vehicle { //車牌號 品牌 日租金 private string id; private string brand; private int perrent; public vehicle(){} //vehicle的帶參構造方法 public vehicle(string id, string brand, int perrent) { this .id = id; this .brand = brand; this .perrent = perrent; } public void setid(string id){ this .id=id ; } public string getid(){ return id; } public void setbrand(string brand){ this .brand=brand; } public string getbrand(){ return brand; } public void setperrent( int perrent){ this .perrent=perrent; } public int getperrent(){ return perrent; } //抽象方法計算租金 public abstract double calcrent( int days); } //轎車類 public class car extends vehicle{ //型號 private string type; public void settype(string type){ this .type=type; } public string gettype(){ return type; } public car(){} //car的帶參構造方法 public car(string id, string brand, int perrent,string type) { super (id,brand,perrent); this .type = type; } //重寫父類的計算租金方法:根據自己的計算租金規則 public double calcrent( int days) { double price = this .getperrent()*days; if (days> 7 && days<= 30 ){ price *= 0.9 ; } else if (days> 30 && days<= 150 ){ price *= 0.8 ; } else if (days> 150 ){ price *= 0.7 ; } return price; } } //客車類 public class bus extends vehicle{ //座位數 private int seatcount; public void setseatcount( int seatcount){ this .seatcount=seatcount; } public int getseatcount(){ return seatcount; } public bus(){} //bus的帶參構造方法 public bus(string id,string brand, int perrent, int seatcount){ super (id,brand,perrent); this .seatcount = seatcount; } //重寫父類的計算租金方法:根據自己的計算租金規則 public double calcrent( int days) { double price = this .getperrent()*days; if (days>= 3 && days< 7 ){ price *= 0.9 ; } else if (days>= 7 && days< 30 ){ price *= 0.8 ; } else if (days>= 30 && days< 150 ){ price *= 0.7 ; } else if (days> 150 ){ price *= 0.6 ; } return price; } } //汽車業務類 public class operation { public vehicle[] vehicle = new vehicle[ 8 ]; //初始化汽車信息 public void init(){ vehicle[ 0 ] = new car( "京ny28588" , "寶馬" , 800 , "x6" ); //vehicle v = new car(); vehicle[ 1 ] = new car( "京cny32584" , "寶馬" , 600 , "550i" ); //vehicle v = new car(); vehicle[ 2 ] = new car( "京nt37465" , "別克" , 300 , "林蔭大道" ); //vehicle v = new car(); vehicle[ 3 ] = new car( "京nt96968" , "別克" , 600 , "gl8" ); //vehicle v = new car(); vehicle[ 4 ] = new bus( "京6566754" , "金杯" , 800 , 16 ); //vehicle v = new bus(); vehicle[ 5 ] = new bus( "京8696997" , "金龍" , 800 , 16 ); //vehicle v = new bus(); vehicle[ 6 ] = new bus( "京9696996" , "金杯" , 1500 , 34 ); //vehicle v = new bus(); vehicle[ 7 ] = new bus( "京8696998" , "金龍" , 1500 , 34 ); //vehicle v = new bus(); } //租車:根據用戶提供的條件去汽車數組中查找相應車輛并返回 //如果租賃的是轎車 需要條件:品牌 型號 //如果租賃的是客車 需要條件:品牌 座位數 //簡單工廠模式 public vehicle zuche(string brand,string type, int seatcount){ vehicle che = null ; //for循環遍歷數組vehicle for (vehicle myche : vehicle){ //判斷vehicle類的myche的類型是否和car一樣 if (myche instanceof car){ //vehicle類的myche向下轉型變成子類car car car = (car)myche; if (car.getbrand().equals(brand) && car.gettype().equals(type)){ che=car; break ; } } else { //vehicle類的myche向下轉型變成子類bus bus bus = (bus)myche; if (bus.getbrand().equals(brand) && bus.getseatcount() == seatcount){ che=bus; break ; } } } return che; } } //汽車租賃 public class rent { public static void main(string[] args) { scanner input = new scanner(system.in); operation operation = new operation(); //租賃公司界面 operation.init(); system.out.println( "**********歡迎光臨租賃公司**********" ); system.out.println( "1.轎車\t\t2.客車" ); system.out.println( "請選擇您要租賃的汽車類型:(1.轎車 2.客車)" ); int chetype = input.nextint(); string brand = "" ; //品牌 string type = "" ; //型號 int seatcount = 0 ; //座位數 //收集用戶條件 if (chetype == 1 ){ //租賃轎車 system.out.println( "請選擇您要租賃的轎車品牌:(1.別克 2.寶馬)" ); int choose = input.nextint(); if (choose == 1 ){ brand= "別克" ; system.out.println( "請選擇您要租賃的汽車型號:(1.林蔭大道 2.gl8)" ); type=(input.nextint() == 1 )? "林蔭大道" : "gl8" ; } else if (choose == 2 ){ brand= "寶馬" ; system.out.println( "請選擇您要租賃的汽車型號:(1.x6 2.550i)" ); type=(input.nextint() == 1 )? "x6" : "550i" ; } } else if (chetype == 2 ){ //租賃客車 type= "" ; system.out.println( "請選擇您要租賃的客車品牌:(1.金杯 2.金龍)" ); brand=(input.nextint()== 1 )? "金杯" : "金龍" ; system.out.println( "請選擇您要租賃的客車座位數:(1.16座 2.32座)" ); seatcount=(input.nextint() == 1 )? 16 : 34 ; } //租車 vehicle che = operation.zuche(brand, type, seatcount); system.out.println( "請輸入您的租賃天數:" ); int days = input.nextint(); double money = che.calcrent(days); system.out.println( "租車成功,請按照如下車牌號提車:" +che.getid()); system.out.println( "您需要支付:" +money+ "元" ); } } |
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持服務器之家。
原文鏈接:https://blog.csdn.net/baidu_29343517/article/details/81153722