Q30
Q30
*;
import java.util.*;
class Customer {
private Integer id;
private String firstName;
private String lastName;
private String city;
private String email;
public Customer(Integer id, String firstName, String lastName, String city, String email) {
this.id = id;
this.firstName = firstName;
this.lastName = lastName;
this.city = city;
this.email = email;
}
@Override
public String toString() {
return String.format("%-15d %-20s %-15s %-10s%s", id, firstName, lastName, city, email);
}
}
class CustomerBO {
public List<Customer> readFromFile(BufferedReader br) {
List<Customer> customerList = new ArrayList<>();
try {
String line;
while ((line = br.readLine()) != null) {
String[] details = line.split(",");
if (details.length == 5) {
Integer id = Integer.parseInt(details[0].trim());
String firstName = details[1].trim();
String lastName = details[2].trim();
String city = details[3].trim();
String email = details[4].trim();
customerList.add(new Customer(id, firstName, lastName, city, email));
}
}
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
} catch (NumberFormatException e) {
System.out.println("Invalid data format in file.");
}
return customerList;
}
}
if (customerList.isEmpty()) {
System.out.println("The list is empty");
} else {
System.out.printf("%-15s %-20s %-15s %-10s%s\n", "Id", "FirstName", "LastName", "City",
"Email");
for (Customer customer : customerList) {
System.out.println(customer);
}
}
} catch (FileNotFoundException e) {
System.out.println("File not found: " + e.getMessage());
} catch (IOException e) {
System.out.println("Error reading file: " + e.getMessage());
}
}
}