-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFactory.java
418 lines (339 loc) · 10.6 KB
/
Factory.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
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
import java.util.Scanner;
enum CarType { //Specify the car type
SMALL, SEDAN, LUXURY, SUV
}
abstract class Car1 {//Class for car
public Car1(CarType model) {
this.model = model;
}
// Do subclass level processing in this method
protected abstract void construct();
private CarType model = null;
public CarType getModel() {
return model;
}
public void setModel(CarType model) {
this.model = model;
}
public void build() {
System.out.println("Gathering components");
System.out.println("Building chasis");
System.out.println("Adding seats");
System.out.println("Installing Engine");
System.out.println("Painting");
}
}
class LuxuryCar extends Car1 {//Luxury car class
LuxuryCar() {//Constructor
super(CarType.LUXURY);
construct();
}
@Override
protected void construct() {//Building class
System.out.println("Building luxury car");
// add accessories
}
void showAvailable() {//Show available cars
Scanner sc = new Scanner(System.in);
int choice;//Choice
int x = 1;
do {
System.out.println("\n\nChoose car::");//Luxury cars
System.out.println("\n1. Mercedes-Benz S-class"
+ "\n2. Jaguar XF"
+ "\n3. Land Rover Discovery" +"\n4. Exit");
System.out.println("Enter choice:: ");
choice = sc.nextInt();
switch(choice) {
case 1://1st car specifications
build();
System.out.println("\n\n---Mercedes-Benz S-class Specifications---");
System.out.println("Price:: Rs.1.38 - 2.78 Cr");
System.out.println("Seating Capacity:: 5");
System.out.println("Fuel Economy:: 16km/l");
System.out.println("Engine:: 2.9 L 6-cylinder diesel, 3.0 L V6");
System.out.println("Mileage (upto):: 13.5 kmpl");
System.out.println("Transmission:: Automatic");
System.out.println("Boot Space:: 500");
System.out.println("---------------------------");
break;
case 2://2nd car specifications
build();
System.out.println("\n\n---Jaguar XF Specfications---");
System.out.println("Price:: Rs.55.67 Lakh");
System.out.println("Seating Capacity:: 5");
System.out.println("Fuel Capacity:: 74 L");
System.out.println("Engine:: 2.0 L 4-cylinder");
System.out.println("Mileage (upto):: 10.8 kmpl");
System.out.println("Transmission:: Automatic");
System.out.println("Boot Space:: 505");
System.out.println("---------------------------");
break;
case 3://3rd car specifications
build();
System.out.println("\n\n---Land Rover Discovery Specfications---");
System.out.println("Price:: Rs 75.59 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Mileage (upto):: 12.05 kmpl");
System.out.println("Transmission:: Automatic");
System.out.println("Fuel Type:: Petrol");
System.out.println("---------------------------");
break;
case 4:
System.exit(0);
break;
default:
System.out.println(" Please Enter Valid Choice");
}
}while(x==1);
}
}
class SmallCar extends Car1 {//Small car class
SmallCar() {//Constructor
super(CarType.SMALL);
construct();
}
@Override
protected void construct() {//Building small car
System.out.println("Building small car");
// add accessories
}
void showAvailable() {//Show available cars
Scanner sc = new Scanner(System.in);
int choice;
int x = 1;
do {
System.out.println("\n\nChoose car to build::");//Display cars
System.out.println("\n1. Maruti Alto"
+ "\n2. Kia Forte"
+ "\n3. Volkswagen Polo" +"\n4. Exit");
System.out.println("Enter choice:: ");
choice = sc.nextInt();
switch(choice) {
case 1://Specifications of 1st car
build();
System.out.println("\n\n---Maruti Alto Specfications---");
System.out.println("Price:: Rs 2.95 lakhs - Rs 4.41 lakhs");
System.out.println("Seating Capacity:: 4,5");
System.out.println("Engine:: 0.80 L 3-cylinder, 0.80 L 3-cylinder natural gas");
System.out.println("Mileage (upto):: 22.05 to 31.59 km/kg");
System.out.println("Transmission:: Manual");
System.out.println("---------------------------");
break;
case 2://Specifications of 2nd car
build();
System.out.println("\n\n---Honda Brio Specfications---");
System.out.println("Price:: Rs 4.73 Lakh - 6.81 Lakh");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1198 cc");
System.out.println("Mileage (upto):: 22.0 kmpl");
System.out.println("Transmission:: Manual & Automatic");
System.out.println("---------------------------");
break;
case 3://Specifications of 3rd car
build();
System.out.println("\n\n---Maruti Suzuki Wagon R Specfications---");
System.out.println("Price:: Rs 4.51 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1197 cc");
System.out.println("Mileage (upto):: 20.52 to 32.52 kmpl");
System.out.println("Transmission:: Manual");
System.out.println("---------------------------");
break;
case 4:
System.exit(0);
break;
default:
System.out.println(" Please Enter Valid Choice");
}
}while(x==1);
}
}
class SedanCar extends Car1 {//Sedan car class
SedanCar() {//Constructor
super(CarType.SEDAN);
construct();
}
@Override
protected void construct() {
System.out.println("Building sedan car");
// add accessories
}
void showAvailable() {//Show available cars
Scanner sc = new Scanner(System.in);
int choice;
int x = 1;
do {//Show cars
System.out.println("\n\nChoose car::");
System.out.println("\n1. Maruti Suzuki Ciaz"
+ "\n2. Maruti Dzire"
+ "\n3. Hyundai Verna" + "\n4. Exit");
System.out.println("Enter choice:: ");
choice = sc.nextInt();
switch(choice) {
case 1://Display specifications
build();
System.out.println("\n\n---Honda City Specfications---");
System.out.println("Price:: Rs 10.9 lakhs - 14.8 lakhs");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1498 cc");
System.out.println("Mileage (upto):: 24.1 kmpl");
System.out.println("Transmission:: Manual/Automatic");
System.out.println("---------------------------");
break;
case 2:
build();
System.out.println("\n\n---Maruti Dzire Specfications---");
System.out.println("Price:: Rs 5.89 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1197 cc");
System.out.println("Mileage (upto):: 24.1 kmpl");
System.out.println("Transmission:: Manual/Automatic");
System.out.println("---------------------------");
break;
case 3:
build();
System.out.println("\n\n---Hyundai Verna Specfications---");
System.out.println("Price:: Rs 9.03 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1497 cc");
System.out.println("Mileage (upto):: 17.7 to 25 kmpl");
System.out.println("Transmission:: Manual/Automatic");
System.out.println("---------------------------");
break;
case 4:
System.exit(0);
break;
default:
System.out.println(" Please Enter Valid Choice");
}
}while(x==1);
}
}
class SuvCar extends Car1 {//Suv car class
SuvCar() {//Constructor
super(CarType.SUV);
construct();
}
@Override
protected void construct() {
System.out.println("Building SUV car");
// add accessories
}
void showAvailable() {//Show available cars
Scanner sc = new Scanner(System.in);
int choice;
int x = 1;
do {//Display cars
System.out.println("\n\nChoose car to build::");
System.out.println("\n1. Hyundai Creta"
+ "\n2. KIA Sonet"
+ "\n3. Nissan Magnite" + "\n4. Exit");
System.out.println("Enter choice:: ");
choice = sc.nextInt();
switch(choice) {
case 1://Show specifications
System.out.println("\n\n---Hyundai Creta---");
System.out.println("Price:: Rs 9.03 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Engine:: 1497 cc");
System.out.println("Mileage (upto):: 17.7 to 25 kmpl");
System.out.println("Transmission:: Manual/Automatic");
System.out.println("---------------------------");
break;
case 2:
System.out.println("\n\n---KIA Sonet---");
System.out.println("Price:: Rs 6.71 lakhs onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Fuel Type:: Petrol & Diesel");
System.out.println("Engine:: 1.0 L 3-cylinder, 1.2 L 4-cylinder, 1.5 L 4-cylinder diesel");
System.out.println("Mileage (upto):: 18.2 to 24.1 kmpl");
System.out.println("Transmission:: Manual/Automatic");
System.out.println("---------------------------");
break;
case 3:
System.out.println("\n\n---Nissan Magnite---");
System.out.println("Price:: Rs 4.99 Lakh onwards");
System.out.println("Seating Capacity:: 5");
System.out.println("Fuel Type:: Petrol & Diesel");
System.out.println("Engine:: 999 cc");
System.out.println("Mileage (upto):: 17.7 to 19.42 kmpl");
System.out.println("Transmission:: Manual & Automatic (CVT)");
System.out.println("---------------------------");
break;
case 4:
System.exit(0);
break;
default:
System.out.println(" Please Enter Valid Choice");
}
}while(x==1);
}
}
class CarFactory {//Car factory class
public static Car1 buildCar(CarType model) {
Car1 car = null;
switch (model) {
case SMALL:
//Show small cars
SmallCar small = new SmallCar();
small.showAvailable();
break;
case SEDAN:
//Show sedan cars
SedanCar sedan = new SedanCar();
sedan.showAvailable();
break;
case LUXURY:
//Show luxury cars
LuxuryCar luxury = new LuxuryCar();
luxury.showAvailable();
break;
case SUV:
//Show SUV cars
SuvCar suv = new SuvCar();
suv.showAvailable();
default:
// throw some exception
break;
}
return car;
}
}
public class Factory {
public static void main(String[] args) {
// TODO Auto-generated method stub
int choice;
Scanner sc = new Scanner(System.in);
int x = 1;
do {//Menu driven car
System.out.println("\n\nChoose car::");
System.out.println("1. SMALL"
+ "\n2. SEDAN"
+ "\n3. LUXURY"
+ "\n4. SUV" + "\n5. Exit");
System.out.println("Enter choice:: ");
choice = sc.nextInt();
switch(choice) {
case 1://Small car
System.out.println(CarFactory.buildCar(CarType.SMALL));
break;
case 2://Sedan car
System.out.println(CarFactory.buildCar(CarType.SEDAN));
break;
case 3://Luxury car
System.out.println(CarFactory.buildCar(CarType.LUXURY));
break;
case 4://Suv
System.out.println(CarFactory.buildCar(CarType.SUV));
break;
case 5:
System.exit(0);
break;
default:
System.out.println(" Please Enter Valid Choice");
break;
}
}while(x == 1);
}
}