import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class UserDataStorage {
public static void main(String[] args) {
List<UserData> userDataList = new ArrayList<>();
String dataFile = "./files/cargarage.csv";
try {
BufferedReader reader = new BufferedReader(new FileReader(dataFile));
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 4) {
String name = parts[0];
String car1 = parts[1];
String car2 = parts[2];
String car3 = parts[3];
UserData userData = new UserData(name, car1, car2, car3);
userDataList.add(userData);
}
}
reader.close();
} catch (IOException e) {
}
Scanner scanner = new Scanner(System.in);
while (true) {
System.out.print("Please enter your name: ");
String name = scanner.nextLine();
System.out.print("Enter car 1: ");
String car1 = scanner.nextLine();
System.out.print("Enter car 2: ");
String car2 = scanner.nextLine();
System.out.print("Enter car 3: ");
String car3 = scanner.nextLine();
UserData userData = new UserData(name, car1, car2, car3);
userDataList.add(userData);
System.out.print("Do you want to input data for another user? (yes/no): ");
String anotherUser = scanner.nextLine().toLowerCase();
if (!anotherUser.equals("yes")) {
break;
}
}
// Save user data to the file
try {
BufferedWriter writer = new BufferedWriter(new FileWriter(dataFile));
for (UserData userData : userDataList) {
writer.write(userData.toString());
writer.newLine();
}
writer.close();
} catch (IOException e) {
e.printStackTrace();
}
// Print the user data
System.out.println("User Data Table:");
System.out.println("Name\t\tCar 1\t\tCar 2\t\tCar 3");
for (UserData userData : userDataList) {
System.out.println(userData);
}
}
}
class UserData {
private String name;
private String car1;
private String car2;
private String car3;
public UserData(String name, String car1, String car2, String car3) {
this.name = name;
this.car1 = car1;
this.car2 = car2;
this.car3 = car3;
}
@Override
public String toString() {
return name + "," + car1 + "," + car2 + "," + car3;
}
}
UserDataStorage.main(null)
Please enter your name: Enter car 1: Enter car 2: Enter car 3: Do you want to input data for another user? (yes/no): User Data Table:
Name Car 1 Car 2 Car 3
Edwin,Jeep Grand Wagoneer,Lexus LC 500C,Jeep Wrangler Rubicon