Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.io.*;
- import java.util.*;
- public class MyEmployeeManager
- implements EmployeeManager {
- //takes single line from .csv and return an employee object based on the information
- public static Employee toClass(String line) {
- Employee thisEmployee = new Employee();
- thisEmployee.lastname = getLastname(line);
- thisEmployee.firstname = getFirstname(line);
- thisEmployee.department = getDepartment(line);
- thisEmployee.id = getId(line);
- thisEmployee.birthday = getBirthday(line);
- return thisEmployee;
- }
- //returns list of employees in department given, sorted by last name
- public List<Employee> findEmployeesByDepartment(String department) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
- //tree map will do NATURAL sorting for us
- Map<String, List<String>> map = new TreeMap<String, List<String>>();
- String line = reader.readLine();
- //if null it's at the end
- while ((line = reader.readLine()) != null) {
- //treemap sorts by key
- String key = getLastname(line);
- String dep = getDepartment(line);
- //department check
- if (dep.equals(department)) {
- List<String> l = map.get(key);
- if (l == null) {
- l = new LinkedList<String>();
- map.put(key,l);
- }
- l.add(line);
- }
- }
- reader.close();
- List<Employee> empList = new ArrayList<Employee>();
- for (List<String> list : map.values()) {
- for (String val : list) {
- empList.add(toClass(val));
- }
- }
- return empList;
- }
- //finds employee based on id number and returns it as Employee object
- public Employee findEmployeeById(int id) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
- String line = reader.readLine();
- while ((line = reader.readLine()) != null) {
- if (getId(line).equals(Integer.toString(id))) {
- break;
- }
- }
- reader.close();
- return toClass(line);
- }
- //gets total number of employees
- public int getEmployeeCount() throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
- String line = reader.readLine();
- int ans = 0;
- while ((line = reader.readLine()) != null) {
- ans++;
- }
- return ans;
- }
- public List<Employee> getAllEmployeesSortedByDept() throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
- Map<String, List<String>> map = new TreeMap<String, List<String>>();
- String line = reader.readLine();
- while ((line = reader.readLine()) != null) {
- String key = getDepartment(line);
- List<String> l = map.get(key);
- if (l == null) {
- l = new LinkedList<String>();
- map.put(key,l);
- }
- l.add(line);
- }
- reader.close();
- List<Employee> empList = new ArrayList<Employee>();
- for (List<String> list : map.values()) {
- for (String val : list) {
- empList.add(toClass(val));
- }
- }
- return empList;
- }
- public void addEmployee(Employee employee) throws Exception {
- Writer writer = new BufferedWriter(new FileWriter("Employee_Data.csv", true));
- writer.write(employee.printEmployeeCsv());
- writer.close();
- }
- public void removeEmployee(int id) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
- Map<String, List<String>> map = new TreeMap<String, List<String>>();
- String line = reader.readLine();
- while ((line = reader.readLine()) != null) {
- String key = getId(line);
- List<String> l = map.get(key);
- if (l == null) {
- l = new LinkedList<String>();
- map.put(key,l);
- }
- l.add(line);
- }
- reader.close();
- FileWriter writer = new FileWriter("Employee_Data.csv");
- writer.write("First Name,Last Name,Department,ID,Birthday\n");
- for (List<String> list : map.values()) {
- for (String val : list) {
- if (!getId(val).equals(Integer.toString(id))) {
- writer.write(val);
- writer.write("\n");
- }
- }
- }
- writer.close();
- }
- private static List<Employee> loadFile(String filename) throws Exception {
- BufferedReader reader = new BufferedReader(new FileReader(filename));
- List<Employee> empList = new ArrayList<Employee>();
- String line = reader.readLine();
- while ((line = reader.readLine()) != null) {
- empList.add(toClass(line));
- }
- reader.close();
- return empList;
- }
- private static String getFirstname(String line) {
- return line.split(",")[0];
- }
- private static String getLastname(String line) {
- return line.split(",")[1];
- }
- private static String getDepartment(String line) {
- return line.split(",")[2];
- }
- private static String getId(String line) {
- return line.split(",")[3];
- }
- private static String getBirthday(String line) {
- return line.split(",")[4];
- }
- public static void main(String[] args) throws Exception {
- MyEmployeeManager manager = new MyEmployeeManager();
- Employee me = new Employee("Clayton", "Stuart", "Tech Support", "101", "10/31/92");
- //Read the sample data from the attached CSV file into the EmployeeManager
- System.out.println("This is loading the .csv file, and storing all employees as Employee objects in a list, and then printing it.");
- for(Employee emp : manager.loadFile("Employee_Data.csv")) {
- System.out.println(emp.printEmployee());
- }
- //output the total number of employees
- System.out.println("Print Employee Count\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println(manager.getEmployeeCount());
- //Call the removeEmployee method to remove an employee from the list (remove with ID=49)
- System.out.println("Remove employee with ID# 49 from list");
- manager.removeEmployee(49);
- //output the total number of employees
- System.out.println("Print Employee Count\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println(manager.getEmployeeCount());
- /**Get a list of employees sorted by department, and check whether the list is,
- in fact, sorted correctly (Verify that the first department is Accounting and
- the last department is Tech Support). Output the result of your checks.**/
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println("Get all employees sorted by Department");
- int i = 0;
- for(Employee emp : manager.getAllEmployeesSortedByDept()) {
- i++;
- if (i == 1) {
- System.out.println("This should be accounting");
- System.out.println(emp.getDepartment());
- }
- if (i == manager.getEmployeeCount() - 1) {
- System.out.println("This should be Tech Support.");
- System.out.println(emp.getDepartment());
- }
- System.out.println(emp.printEmployee());
- }
- /**Get a list of employees in the Tech Support department.
- Check to make sure that each employee in the list is in fact
- in the Tech Support department.
- Output the result of your checks.**/
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println("Find Employee by department 'Tech Support'");
- for(Employee emp : manager.findEmployeesByDepartment("Tech Support")) {
- if (emp.getDepartment().equals("Tech Support")) {
- System.out.println("This person is DEFINITELY in Tech support");
- }
- System.out.println(emp.printEmployee());
- }
- //Fetch employee with ID = 92. Output the result of your check.
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println("Find employee with ID# 92");
- System.out.println(manager.findEmployeeById(92).printEmployee());
- System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
- System.out.println("Add employee based on myself to list");
- manager.addEmployee(me);
- System.out.println(manager.findEmployeeById(101).printEmployee());
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement