In: Computer Science
The purpose of this homework is to test your knowledge of File IO and Collections and the language would be java.
Suppose that we have a file Employee.csv which contains employee information of a certain company.
The first line of the file contains the following header:
employeeId,firstName,lastName,dob,gender,position
After the header line, each employee information is written on a separate line which consists of 6 comma-separated fields, for example:1234567, John, Smith,20/12/1989, M, Software Engineer
Question 1. Write code to read the file and return a list of employee objects.
Question 2. Given a list of employee objects, write code to sort the list by first name and print the sorted list on the console.
This project consists of three classes: 1) Employee Class : This class is a model class for the employees and fileds to store all their information 2) FirstNameSorter : It is a class which implements the Comparator interface which in turn allows us to sort our employee list. It overrides the compare method of the Comparator interface. 3) CSVReader class : This class provides two functions a) readFile() : This function parses the csv file and reads each line of the csv file(skipping the header). It then splits the line using ',' and creates an employee object with the values retrieved. Finally it adds the newly created object to the employee list. b) sortEmployeeList() : This function calls the sort method on the employee list with an object of FirstNameSort as an argument. Finally it prints the sorted list. For any clarifications refer to the code. Each line has been commented for your reference. ////////////////////////////////////////// ////////////////////////////////////////// Employee Class //////////////////////////////////////////
import java.util.Comparator; public class Employee{ private String employeeId; private String firstName; private String lastName; private String dob; private String gender; private String position; public Employee(String employeeId, String firstName, String lastName, String dob, String gender, String position) { this.employeeId = employeeId; this.firstName = firstName; this.lastName = lastName; this.dob = dob; this.gender = gender; this.position = position; } public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getDob() { return dob; } public void setDob(String dob) { this.dob = dob; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } public String getPosition() { return position; } public void setPosition(String position) { this.position = position; } }
//////////////////////////////////////////
//////////////////////////////////////////
Comparator Class
//////////////////////////////////////////
import java.util.Comparator; public class FirstNameSort implements Comparator<Employee> { @Override public int compare(Employee o1, Employee o2) { return o1.getFirstName().compareToIgnoreCase(o2.getFirstName()); } }
////////////////////////////////////////// ////////////////////////////////////////// Main Class ////////////////////////////////////////// import java.io.BufferedReader; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Collections; import java.util.List; public class CSVReader { public static void main(String[] args) { //oath to the file that has to be parsed...replace '\' in the path with '\\' //Enter your file path here with double \ String csvFilePath = "D:\\New Text Document.csv"; //Employee list to store all the records List<Employee> employeeList = new ArrayList<>(); //readFile function reads the contents of the csv file and adds all the records to the employee list readFile(csvFilePath, employeeList); //function to sort the employee list sortEmployeeList(employeeList); } private static void sortEmployeeList(List<Employee> employeeList) { //call the sort method on the arraylist with a comparator argument //the comparator argument will be the object of the class FirstNameSort employeeList.sort(new FirstNameSort()); //print all the elements in the employee list for (Employee e : employeeList) { System.out.println(e.getEmployeeId() + " ," + e.getFirstName() + " ," + e.getLastName() + " ," + e.getDob() + " ," + e.getGender() + " ," + e.getPosition() + "\n"); } } private static void readFile(String csvFilePath, List<Employee> employeeList) { //declare a buffered reader BufferedReader br = null; //line will store each record read from the file String line = ""; //since it's a csv file we have to split the string using ',' to get the individual values String splitByCharacter = ","; try { //initialise the buffered reader br = new BufferedReader(new FileReader(csvFilePath)); //since the first lien is the header so we skip it br.readLine(); //if the line read is not null while ((line = br.readLine()) != null) { //skip the empty lines if (line.trim().isEmpty()) continue; //if the line is not empty, use comma as separator String[] record = line.split(splitByCharacter); //create the employee object from the values read fromt he file Employee empObject = new Employee(record[0], record[1], record[2], record[3], record[4], record[5]); //add the object to the employee list employeeList.add(empObject); } } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } finally { if (br != null) { try { //always close the buffered reader br.close(); } catch (Exception e) { e.printStackTrace(); } } } } }