-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCarUpdate.java
92 lines (82 loc) · 3.71 KB
/
CarUpdate.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
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Scanner;
public class CarUpdate {
@SuppressWarnings("unchecked")
public static void modifyCar(String filePath, Scanner scanner) {
try {
// Read the JSON file
JSONParser parser = new JSONParser();
Object obj = parser.parse(new FileReader(filePath));
// Handle both single object and array scenarios
JSONObject jsonObject;
JSONArray carsArray;
if (obj instanceof JSONObject) {
jsonObject = (JSONObject) obj;
carsArray = (JSONArray) jsonObject.get("Cars"); // Assuming "Cars" is the key for the car data array
} else {
carsArray = (JSONArray) obj; // Treat the entire object as an array
}
// Display available car IDs
System.out.println("Available Car IDs:");
for (Object carObject : carsArray) {
JSONObject car = (JSONObject) carObject;
System.out.println("ID: " + car.get("ID"));
}
System.out.print("Enter the ID of the car to modify: ");
int carId = scanner.nextInt(); // Consume the newline character
scanner.nextLine();
// Find the car to modify
JSONObject carToModify = null;
for (Object carObject : carsArray) {
JSONObject car = (JSONObject) carObject;
if ((long) car.get("ID") == carId) {
carToModify = car;
break;
}
}
// Modify the car if found
if (carToModify != null) {
System.out.println("Car found with ID: " + carId);
System.out.print(
"Enter the field to modify (e.g., Model, Model_Year, Price, Manufacturer, Class, Units): ");
String fieldToModify = scanner.nextLine();
System.out.print("Enter the new value for the field: ");
Object newValue;
if (fieldToModify.equals("ID")) {
newValue = scanner.nextInt();
scanner.nextLine();
} else if (fieldToModify.equals("Model_Year") || fieldToModify.equals("Units")) {
newValue = scanner.nextInt();
scanner.nextLine();
} else if (fieldToModify.equals("Price")) {
newValue = scanner.nextDouble();
scanner.nextLine();
} else {
newValue = scanner.nextLine();
}
// Update the specified field with the new value
carToModify.put(fieldToModify, newValue);
// Write the updated JSON data back to the file
try (FileWriter fileWriter = new FileWriter(filePath)) {
if (carsArray.isEmpty()) {
// Handle empty array case (if the last car was deleted)
fileWriter.write("{}"); // Write an empty object
} else {
fileWriter.write(carsArray.toJSONString());
}
System.out.println("Car with ID " + carId + " modified successfully.");
}
} else {
System.out.println("Car with ID " + carId + " not found.");
}
} catch (IOException | ParseException e) {
System.err.println("Error modifying car: " + e.getMessage());
}
}
}