-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMainClass.java
More file actions
231 lines (188 loc) · 7.83 KB
/
Copy pathMainClass.java
File metadata and controls
231 lines (188 loc) · 7.83 KB
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
package carbookingsystem1;
import java.util.ArrayList;
import java.util.InputMismatchException;
import java.util.Scanner;
import java.util.*;
public class MainClass {
public static ArrayList<Car> arrayList = new ArrayList<>();
public static ArrayList<Reservation> reservationList = new ArrayList<>();
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int choice = 0;
System.out.println("-- Welcome TO Our Car Booking System -- ");
do {
try {
displayMenu();
choice = scanner.nextInt();
fillList(arrayList);
switch (choice) {
case 1:
displayArrayElements();
break;
case 2:
makeReservation();
break;
case 3:
cancelReservation();
break;
case 4:
displayReservation();
break;
case 5:
writeFile();
break;
case 6:
readFile();
break;
case 7:
GUI.main(null);
break;
case 8:
System.out.println("Exiting the program.");
break;
default:
System.out.println("Invalid choice.\n"
+ "Please select a valid option.");
}
} catch (InputMismatchException e) {
System.err.println("Incorrect type input !");
scanner.next();
} catch (Exception e) {
System.err.println(e.getMessage());
}
} while (choice != 9);
}
private static void displayMenu() {
System.out.println("======= Menu =======");
System.out.println("1.Display list Car");
System.out.println("2.Make a reservation");
System.out.println("3.Cancel a reservation");
System.out.println("4.Display reservation");
System.out.println("5.Write to a text file");
System.out.println("6.Read from a text file");
System.out.println("7.GUI");
System.out.println("8.Exit");
System.out.print("*** Enter the option number : ");
System.out.println("");
}
public static void fillList(ArrayList<Car> arryList) {
SpecialFeature fe1 = new SpecialFeature("Massaging Seats");
SpecialFeature fe2 = new SpecialFeature("Wireless Phone Charging");
SpecialFeature fe3 = new SpecialFeature("Night Mode Camera");
SpecialFeature fe4 = new SpecialFeature("Bluetooth Conectivity");
SpecialFeature[] car1 = {fe2, fe3};
arrayList.add(new Sport(01, "Ferrari SP-8", car1, true, "Black", 600, 500));
arrayList.add(new Sport(02, "McLaren-750S", car1, true, "Blue", 500, 350));
SpecialFeature[] car2 = {fe3, fe4};
arrayList.add(new Compact(true, 03, "Lexus250", car2, false, "Blue"));
arrayList.add(new Compact(false, 04, "Honda Civic", car2, true, "Gray"));
SpecialFeature[] car3 = {fe1, fe2};
arrayList.add(new Luxury(4, 05, "BMW7", car3, true, "White"));
arrayList.add(new Luxury(2, 06, "Rolls-Royce", car3, true, "White"));
}
public static void displayArrayElements() {
System.out.println("---- Our Car list elements: ----");
for (Car element : arrayList) {
System.out.println(element);
element.DisplaySpecialFeatures(); //Polymorphismic method
System.out.printf("The Rental price is: %d SAR %n", element.calculateRentalPrice());
if (element instanceof Sport) {
((Sport) element).sportMode(); //downcasting
}
System.out.println("Total Number of cars: " + Car.getTotalNumberOfCars());
System.out.println("---------------------");
}
}
public static void makeReservation() {
Scanner scanner = new Scanner(System.in);
System.out.print(" Please Enter car ID you want: ");
int id = scanner.nextInt();
Car car = null;
boolean found = false;
for (Car element : arrayList) {
if (element.getCarID() == id) {
car = element;
found = true;
}
}
if (found == false) {
System.out.println("The car with ID " + id + " is not found");
return;
}
System.out.print("How many days you want to rent the car ? ");
int Day = scanner.nextInt();
if (Day < 1 || Day > 30) {
System.out.println("Invalid duration !");
}
System.out.println("Enter your full name : ");
String Name = scanner.next();
System.out.println("Enter your phone Number : ");
String PhoneNum = scanner.next();
System.out.println("Enter your ID : ");
String ID = scanner.next();
System.out.println("Enter your Email : ");
String email = scanner.next();
Customer customer = new Customer(email, ID, PhoneNum, Name);
//Object for customer class
Reservation b = new Reservation(customer, car, Day);
// Object for reservation class
System.out.println("Your cost for " + Day + " Day is " + b.calculateAmount());
System.out.println("Your Reservation ID is : " + b.getReservationID());
b.setReservationStatus("Certain");
System.out.println("Your Reservation status is : " + b.getReservationStatus());
reservationList.add(b);
}
public static void cancelReservation() {
Scanner scanner = new Scanner(System.in);
boolean found;
System.out.print("Enter the Reservation id you want to cancel : ");
int reservationID = scanner.nextInt();
found = false;
for (Reservation e : reservationList) {
if (e.getReservationID() == reservationID) {
e.cancelReservation();
found = true;
}
}
if (found == false) {
System.out.println("Reservation with ID " + reservationID + " is not found.");
}
}
public static void displayReservation() {
Scanner scanner = new Scanner(System.in);
boolean found;
System.out.print("Enter the Reservation id you want to Show: ");
int reservationID = scanner.nextInt();
found = false;
for (Reservation e : reservationList) {
if (e.getReservationID() == reservationID) {
System.out.println(e);
System.out.println("Your total cost: " + e.calculateAmount());
found = true;
}
}
if (found == false) {
System.out.println("Reservation with ID " + reservationID + "is not found.");
}
}
private static void writeFile() {
WriteText wf = new WriteText();
wf.openTextFile("Cars.txt");
if (arrayList.isEmpty()) {
System.out.println("No cars yet.");
return;
} else {
for (Car ele : arrayList) {
wf.writeToFile(ele);
}
}
wf.closeFile();
System.out.println("All cars saved to the text file Cars.txt");
}
private static void readFile() {
ReadText rt = new ReadText();
rt.openTextFile("Cars.txt");
rt.readFromFile();
rt.closeFile();
}
}