0% found this document useful (0 votes)
6 views2 pages

Q30

Uploaded by

rishabhsonkar121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views2 pages

Q30

Uploaded by

rishabhsonkar121
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

import java.io.

*;
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;
}

public Integer getId() {


return id;
}

public String getFirstName() {


return firstName;
}

public String getLastName() {


return lastName;
}

public String getCity() {


return city;
}

public String getEmail() {


return 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;
}
}

public class Main {


public static void main(String[] args) {
File file = new File("input.csv");
try (BufferedReader br = new BufferedReader(new FileReader(file))) {
CustomerBO customerBO = new CustomerBO();
List<Customer> customerList = customerBO.readFromFile(br);

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());
}
}
}

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy