-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathServiceInt.java
More file actions
340 lines (296 loc) · 12.5 KB
/
ServiceInt.java
File metadata and controls
340 lines (296 loc) · 12.5 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
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
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.*;
class User {
private String username;
private String passwordHash;
public User(String username, String password) {
this.username = username;
this.passwordHash = hashPassword(password);
}
public String getUsername() {
return username;
}
public boolean validatePassword(String password) {
return passwordHash.equals(hashPassword(password));
}
private String hashPassword(String password) {
try {
MessageDigest md = MessageDigest.getInstance("SHA-256");
byte[] hash = md.digest(password.getBytes());
StringBuilder hexString = new StringBuilder();
for (byte b : hash) {
hexString.append(String.format("%02x", b));
}
return hexString.toString();
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("Error hashing password");
}
}
}
class CarOperations {
private String userName;
private String registrationNumber;
private String carModel;
private String ownerName;
private int modelYear;
private String registrationDate;
public CarOperations(String userName, String regNo, String carModel, String ownerName, int modelYear, String registrationDate) {
this.userName = userName;
this.registrationNumber = regNo;
this.carModel = carModel;
this.ownerName = ownerName;
this.modelYear = modelYear;
this.registrationDate = registrationDate;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public String getUserName() {
return userName;
}
public void displayDetails() {
System.out.println("\u001B[32mCar Details:\u001B[0m");
System.out.println("Car Model: " + carModel);
System.out.println("Owner: " + ownerName);
System.out.println("Model Year: " + modelYear);
System.out.println("Registration Date: " + registrationDate);
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
CarOperations car = (CarOperations) obj;
return registrationNumber.equals(car.registrationNumber);
}
@Override
public int hashCode() {
return Objects.hash(registrationNumber);
}
}
class ServiceOperations {
private String registrationNumber;
private String serviceDate;
private String serviceType;
private String serviceDescription;
private double serviceCost;
public ServiceOperations(String regNo, String serviceDate, String serviceType, String serviceDescription, double serviceCost) {
this.registrationNumber = regNo;
this.serviceDate = serviceDate;
this.serviceType = serviceType;
this.serviceDescription = serviceDescription;
this.serviceCost = serviceCost;
}
public String getRegistrationNumber() {
return registrationNumber;
}
public void displayServiceDetails() {
System.out.println("\u001B[32mService Details:\u001B[0m");
System.out.println("Service Date: " + serviceDate);
System.out.println("Service Type: " + serviceType);
System.out.println("Service Description: " + serviceDescription);
System.out.println("Service Cost: $" + serviceCost);
}
}
public class ServiceInt {
static Scanner scanner = new Scanner(System.in);
static Set<CarOperations> carDetails = new HashSet<>();
static ArrayList<ServiceOperations> serviceRecords = new ArrayList<>();
static HashMap<String, User> users = new HashMap<>();
static String currentUser = null;
public static void main(String[] args) {
System.out.println("\u001B[34m===============================");
System.out.println(" Welcome to SmartCarCare System ");
System.out.println("===============================\u001B[0m");
if (!authenticateUser()) {
return;
}
boolean exit = false;
while (!exit) {
System.out.println("\n\u001B[33m1. Register Car");
System.out.println("2. Check Car Details");
System.out.println("3. Schedule Service");
System.out.println("4. View Service History");
System.out.println("5. Exit");
System.out.print("Enter your choice: \u001B[0m");
int choice = validateIntegerInput();
switch (choice) {
case 1 -> registerCar();
case 2 -> checkCarDetails();
case 3 -> scheduleService();
case 4 -> viewServiceHistory();
case 5 -> {
System.out.println("\u001B[32mThank you for using SmartCarCare. Goodbye!\u001B[0m");
exit = true;
}
default -> System.out.println("\u001B[31mInvalid choice. Please try again.\u001B[0m");
}
}
}
private static boolean authenticateUser() {
while (true) {
System.out.println("\u001B[33m1. Register");
System.out.println("2. Login");
System.out.println("3. Exit");
System.out.print("Choose an option: \u001B[0m");
int choice = validateIntegerInput();
switch (choice) {
case 1 -> registerUser();
case 2 -> {
if (loginUser()) return true;
}
case 3 -> {
System.out.println("\u001B[32mExiting SmartCarCare System. Goodbye!\u001B[0m");
return false;
}
default -> System.out.println("\u001B[31mInvalid choice. Please try again.\u001B[0m");
}
}
}
private static void registerUser() {
System.out.print("\u001B[33mEnter New Username (or type 'exit' to cancel): \u001B[0m");
String username = scanner.nextLine();
if (username.equalsIgnoreCase("exit")) return;
if (users.containsKey(username)) {
System.out.println("\u001B[31mUsername already exists! Try another.\u001B[0m");
return;
}
System.out.print("\u001B[33mEnter New Password: \u001B[0m");
String password = scanner.nextLine();
users.put(username, new User(username, password));
System.out.println("\u001B[32mRegistration Successful!\u001B[0m");
}
private static boolean loginUser() {
System.out.print("\u001B[33mEnter Username (or type 'exit' to cancel): \u001B[0m");
String username = scanner.nextLine();
if (username.equalsIgnoreCase("exit")) return false;
System.out.print("\u001B[33mEnter Password: \u001B[0m");
String password = scanner.nextLine();
User user = users.get(username);
if (user != null && user.validatePassword(password)) {
System.out.println("\u001B[32mLogin Successful!\u001B[0m");
currentUser = username;
return true;
} else {
System.out.println("\u001B[31mInvalid Credentials. Try again.\u001B[0m");
return false;
}
}
private static void registerCar() {
System.out.print("\u001B[33mEnter Car Registration Number: \u001B[0m");
String regNo = scanner.nextLine();
System.out.print("\u001B[33mEnter Car Model: \u001B[0m");
String carModel = scanner.nextLine();
System.out.print("\u001B[33mEnter Owner Name: \u001B[0m");
String ownerName = scanner.nextLine();
System.out.print("\u001B[33mEnter Model Year: \u001B[0m");
int modelYear = validateIntegerInput();
System.out.print("\u001B[33mEnter Registration Date (YYYY-MM-DD): \u001B[0m");
String registrationDate = scanner.nextLine();
CarOperations newCar = new CarOperations(currentUser, regNo, carModel, ownerName, modelYear, registrationDate);
if (carDetails.add(newCar)) {
System.out.println("\u001B[32mCar successfully registered!\u001B[0m");
newCar.displayDetails();
} else {
System.out.println("\u001B[31mCar is already registered!\u001B[0m");
}
}
private static void checkCarDetails() {
System.out.print("\u001B[33mEnter Car Registration Number to check details: \u001B[0m");
String regNo = scanner.nextLine();
boolean found = false;
for (CarOperations car : carDetails) {
if (car.getRegistrationNumber().equalsIgnoreCase(regNo) && car.getUserName().equals(currentUser)) {
car.displayDetails();
found = true;
break;
}
}
if (!found) {
System.out.println("\u001B[31mCar not found or you don't have access to it.\u001B[0m");
}
}
private static void scheduleService() {
System.out.print("\u001B[33mEnter Car Registration Number: \u001B[0m");
String regNo = scanner.nextLine();
boolean carExists = false;
for (CarOperations car : carDetails) {
if (car.getRegistrationNumber().equalsIgnoreCase(regNo) && car.getUserName().equals(currentUser)) {
carExists = true;
break;
}
}
if (!carExists) {
System.out.println("\u001B[31mCar not found or you don't have access to it.\u001B[0m");
return;
}
while (true) { // Loop until the user chooses to exit
System.out.println("\u001B[33mSelect a Service:\u001B[0m");
System.out.println("1. Oil Change - $50");
System.out.println("2. Tire Rotation - $30");
System.out.println("3. Car Wash - $15");
System.out.println("4. Battery Replacement - $100");
System.out.println("5. Brake Inspection - $40");
System.out.println("6. Exit");
System.out.print("Enter your choice: ");
int choice = validateIntegerInput();
if (choice == 6) {
System.out.println("\u001B[32mExiting service selection.\u001B[0m");
return;
}
String serviceType = "";
String serviceDescription = "";
double serviceCost = 0;
switch (choice) {
case 1 -> {
serviceType = "Oil Change";
serviceDescription = "Complete engine oil replacement.";
serviceCost = 50;
}
case 2 -> {
serviceType = "Tire Rotation";
serviceDescription = "Tires rotated for even wear.";
serviceCost = 30;
}
case 3 -> {
serviceType = "Car Wash";
serviceDescription = "Exterior and interior cleaning.";
serviceCost = 15;
}
case 4 -> {
serviceType = "Battery Replacement";
serviceDescription = "Old battery replaced with a new one.";
serviceCost = 100;
}
case 5 -> {
serviceType = "Brake Inspection";
serviceDescription = "Brake system checked for safety.";
serviceCost = 40;
}
default -> {
System.out.println("\u001B[31mInvalid choice. Please try again.\u001B[0m");
continue;
}
}
System.out.print("\u001B[33mEnter Service Date (YYYY-MM-DD): \u001B[0m");
String serviceDate = scanner.nextLine();
ServiceOperations newService = new ServiceOperations(regNo, serviceDate, serviceType, serviceDescription, serviceCost);
serviceRecords.add(newService);
System.out.println("\u001B[32mService scheduled successfully!\u001B[0m");
newService.displayServiceDetails();
return; // Exit after scheduling a service
}
}
private static void viewServiceHistory() {
System.out.println("\u001B[33mFeature to be implemented: View service history\u001B[0m");
}
private static int validateIntegerInput() {
while (!scanner.hasNextInt()) {
scanner.next();
System.out.println("\u001B[31mInvalid input. Please enter a number.\u001B[0m");
}
int input = scanner.nextInt();
scanner.nextLine();
return input;
}
}