-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMain.java
More file actions
291 lines (271 loc) · 11.1 KB
/
Main.java
File metadata and controls
291 lines (271 loc) · 11.1 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
package com.carsolutions;
/*
* Name: <Buğra TUTUMLU>
* Number: <8230800>
* Class: <LEI_PP>
*/
import org.json.simple.parser.ParseException;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Scanner;
/**
* Main class for the Car Solutions application.
* Handles user interaction and main application logic.
*/
public class Main {
private static ServiceOrderManagement serviceOrderManagement = new ServiceOrderManagement();
private static JSONHandler jsonHandler = new JSONHandler(serviceOrderManagement);
private static IStatistics statistics = new Statistics(serviceOrderManagement); // IStatistics interface is using
private static final SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
/**
* Main method to run the application.
*
* @param args command-line arguments
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.println("1. Create Service Order");
System.out.println("2. Update Service Order");
System.out.println("3. Delete Service Order");
System.out.println("4. View Service Orders");
System.out.println("5. Export to JSON");
System.out.println("6. Import from JSON");
System.out.println("7. View Statistics");
System.out.println("8. Remove Item from Service Order");
System.out.println("0. Exit");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
createServiceOrder(scanner);
break;
case 2:
updateServiceOrder(scanner);
break;
case 3:
deleteServiceOrder(scanner);
break;
case 4:
viewServiceOrders();
break;
case 5:
exportToJSON(scanner);
break;
case 6:
importFromJSON(scanner);
break;
case 7:
viewStatistics();
break;
case 8:
removeItemFromServiceOrder(scanner);
break;
case 0:
System.exit(0);
default:
System.out.println("Invalid choice, please try again.");
}
}
}
/**
* Creates a new service order based on user input.
*
* @param scanner Scanner object for user input
*/
private static void createServiceOrder(Scanner scanner) {
System.out.println("Service Order ID:");
String id = scanner.nextLine();
Date creationDate = new Date();
System.out.println("Estimated Completion Date (yyyy-MM-dd):");
String estimatedDateStr = scanner.nextLine();
Date estimatedCompletionDate;
try {
estimatedCompletionDate = dateFormat.parse(estimatedDateStr);
} catch (java.text.ParseException e) {
System.out.println("Invalid date format.");
return;
}
System.out.println("Vehicle Registration Number:");
String vehicleRegistration = scanner.nextLine();
System.out.println("Enter client type (business/individual):");
String clientType = scanner.nextLine();
Client client = null;
if (clientType.equalsIgnoreCase("business")) {
System.out.println("Enter Business Client ID:");
String clientId = scanner.nextLine();
System.out.println("Enter Business Name:");
String name = scanner.nextLine();
System.out.println("Enter Tax ID:");
String taxId = scanner.nextLine();
client = new Business(clientId, name, taxId);
} else if (clientType.equalsIgnoreCase("individual")) {
System.out.println("Enter Individual Client ID:");
String clientId = scanner.nextLine();
System.out.println("Enter Individual Name:");
String name = scanner.nextLine();
System.out.println("Enter Personal ID:");
String personalId = scanner.nextLine();
client = new Individual(clientId, name, personalId);
} else {
System.out.println("Invalid client type.");
return;
}
ServiceOrder order = new ServiceOrder(id, creationDate, estimatedCompletionDate, vehicleRegistration, client);
while (true) {
System.out.println("Add an item to the service order? (yes/no):");
String addItem = scanner.nextLine();
if (addItem.equalsIgnoreCase("no")) {
break;
}
System.out.println("Enter item type (service/part):");
String itemType = scanner.nextLine();
System.out.println("Enter item description:");
String description = scanner.nextLine();
if (itemType.equalsIgnoreCase("service")) {
System.out.println("Enter execution time in minutes:");
int executionTime = scanner.nextInt();
scanner.nextLine();
order.addItem(new Service(description, executionTime));
} else if (itemType.equalsIgnoreCase("part")) {
System.out.println("Enter quantity:");
int quantity = scanner.nextInt();
scanner.nextLine();
order.addItem(new Part(description, quantity));
} else {
System.out.println("Invalid item type.");
}
}
serviceOrderManagement.createServiceOrder(order);
System.out.println("Service order created.");
}
/**
* Updates an existing service order based on user input.
*
* @param scanner Scanner object for user input
*/
private static void updateServiceOrder(Scanner scanner) {
System.out.println("Service Order ID to update:");
String id = scanner.nextLine();
ServiceOrder order = serviceOrderManagement.getServiceOrder(id);
if (order != null) {
System.out.println("New Estimated Completion Date (yyyy-MM-dd):");
String estimatedDateStr = scanner.nextLine();
Date estimatedCompletionDate;
try {
estimatedCompletionDate = dateFormat.parse(estimatedDateStr);
} catch (java.text.ParseException e) {
System.out.println("Invalid date format.");
return;
}
System.out.println("New Vehicle Registration Number:");
String vehicleRegistration = scanner.nextLine();
order.setEstimatedCompletionDate(estimatedCompletionDate);
order.setVehicleRegistration(vehicleRegistration);
serviceOrderManagement.updateServiceOrder(id, order);
System.out.println("Service order updated.");
} else {
System.out.println("Service order not found.");
}
}
/**
* Deletes a service order based on user input.
*
* @param scanner Scanner object for user input
*/
private static void deleteServiceOrder(Scanner scanner) {
System.out.println("Service Order ID to delete:");
String id = scanner.nextLine();
serviceOrderManagement.deleteServiceOrder(id);
System.out.println("Service order deleted.");
}
/**
* Displays all service orders.
*/
private static void viewServiceOrders() {
for (ServiceOrder order : serviceOrderManagement.getAllServiceOrders()) {
System.out.println("ID: " + order.getId());
System.out.println("Creation Date: " + order.getCreationDate());
System.out.println("Estimated Completion Date: " + order.getEstimatedCompletionDate());
System.out.println("Vehicle Registration: " + order.getVehicleRegistration());
System.out.println("Client Name: " + order.getClient().getName());
System.out.println("Items:");
for (OrderItem item : order.getItems()) {
if (item != null) { // Check for null to avoid NullPointerException
System.out.println(" " + item.getDescription());
}
}
System.out.println();
}
}
/**
* Exports service orders to a JSON file based on user input.
*
* @param scanner Scanner object for user input
*/
private static void exportToJSON(Scanner scanner) {
System.out.println("JSON file path:");
String filePath = scanner.nextLine();
try {
jsonHandler.exportToJSON(filePath);
System.out.println("Data exported to JSON.");
} catch (IOException e) {
System.out.println("Error exporting to JSON: " + e.getMessage());
e.printStackTrace(); // Print stack trace for more detailed debugging
}
}
/**
* Imports service orders from a JSON file based on user input.
*
* @param scanner Scanner object for user input
*/
private static void importFromJSON(Scanner scanner) {
System.out.println("JSON file path:");
String filePath = scanner.nextLine();
try {
jsonHandler.importFromJSON(filePath);
System.out.println("Data imported from JSON.");
} catch (IOException | ParseException e) {
System.out.println("Error importing from JSON: " + e.getMessage());
e.printStackTrace(); // Print stack trace for more detailed debugging
}
}
/**
* Displays statistics about the service orders.
*/
private static void viewStatistics() {
System.out.println("Total Service Orders: " + statistics.getTotalServiceOrders());
System.out.println("Total Services: " + statistics.getTotalServices());
System.out.println("Total Parts: " + statistics.getTotalParts());
}
/**
* Removes an item from a service order based on user input.
*
* @param scanner Scanner object for user input
*/
private static void removeItemFromServiceOrder(Scanner scanner) {
System.out.println("Service Order ID:");
String orderId = scanner.nextLine();
ServiceOrder order = serviceOrderManagement.getServiceOrder(orderId);
if (order != null) {
System.out.println("Enter description of the item to remove:");
String description = scanner.nextLine();
OrderItem itemToRemove = null;
for (OrderItem item : order.getItems()) {
if (item != null && item.getDescription().equals(description)) {
itemToRemove = item;
break;
}
}
if (itemToRemove != null) {
order.removeItem(itemToRemove);
System.out.println("Item removed from service order.");
} else {
System.out.println("Item not found in service order.");
}
} else {
System.out.println("Service order not found.");
}
}
}