Advertisement
Guest User

Untitled

a guest
Jan 25th, 2015
165
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.76 KB | None | 0 0
  1. import java.io.*;
  2. import java.util.*;
  3.  
  4. public class MyEmployeeManager
  5.     implements EmployeeManager {
  6.    
  7.     //takes single line from .csv and return an employee object based on the information
  8.     public static Employee toClass(String line) {
  9.     Employee thisEmployee = new Employee();
  10.     thisEmployee.lastname = getLastname(line);
  11.     thisEmployee.firstname = getFirstname(line);
  12.     thisEmployee.department = getDepartment(line);
  13.     thisEmployee.id = getId(line);
  14.     thisEmployee.birthday = getBirthday(line);
  15.     return thisEmployee;
  16.     }
  17.    
  18.     //returns list of employees in department given, sorted by last name
  19.     public List<Employee> findEmployeesByDepartment(String department) throws Exception {
  20.     BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
  21.     //tree map will do NATURAL sorting for us
  22.     Map<String, List<String>> map = new TreeMap<String, List<String>>();
  23.     String line = reader.readLine();
  24.     //if null it's at the end
  25.     while ((line = reader.readLine()) != null) {
  26.         //treemap sorts by key
  27.         String key = getLastname(line);
  28.         String dep = getDepartment(line);
  29.         //department check
  30.         if (dep.equals(department)) {
  31.         List<String> l = map.get(key);
  32.         if (l == null) {
  33.             l = new LinkedList<String>();
  34.             map.put(key,l);
  35.         }
  36.         l.add(line);
  37.         }
  38.     }
  39.     reader.close();
  40.     List<Employee> empList = new ArrayList<Employee>();
  41.     for (List<String> list : map.values()) {
  42.         for (String val : list) {
  43.         empList.add(toClass(val));
  44.         }
  45.     }
  46.     return empList;
  47.     }
  48.  
  49.     //finds employee based on id number and returns it as Employee object
  50.     public Employee findEmployeeById(int id) throws Exception {
  51.     BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
  52.     String line = reader.readLine();
  53.     while ((line = reader.readLine()) != null) {
  54.         if (getId(line).equals(Integer.toString(id))) {
  55.         break;
  56.         }
  57.     }
  58.     reader.close();
  59.     return toClass(line);
  60.     }
  61.  
  62.     //gets total number of employees
  63.     public int getEmployeeCount() throws Exception {
  64.     BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
  65.     String line = reader.readLine();
  66.     int ans = 0;
  67.     while ((line = reader.readLine()) != null) {
  68.         ans++;
  69.     }
  70.     return ans;
  71.     }
  72.  
  73.     public List<Employee> getAllEmployeesSortedByDept() throws Exception {
  74.     BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
  75.     Map<String, List<String>> map = new TreeMap<String, List<String>>();
  76.     String line = reader.readLine();
  77.     while ((line = reader.readLine()) != null) {
  78.         String key = getDepartment(line);
  79.         List<String> l = map.get(key);
  80.         if (l == null) {
  81.         l = new LinkedList<String>();
  82.         map.put(key,l);
  83.         }
  84.         l.add(line);
  85.     }
  86.     reader.close();
  87.     List<Employee> empList = new ArrayList<Employee>();
  88.     for (List<String> list : map.values()) {
  89.         for (String val : list) {
  90.         empList.add(toClass(val));
  91.         }
  92.     }
  93.     return empList;  
  94.     }
  95.  
  96.     public void addEmployee(Employee employee) throws Exception {
  97.     Writer writer = new BufferedWriter(new FileWriter("Employee_Data.csv", true));
  98.     writer.write(employee.printEmployeeCsv());
  99.     writer.close();
  100.     }
  101.  
  102.     public void removeEmployee(int id) throws Exception {
  103.     BufferedReader reader = new BufferedReader(new FileReader("Employee_Data.csv"));
  104.     Map<String, List<String>> map = new TreeMap<String, List<String>>();
  105.     String line = reader.readLine();
  106.     while ((line = reader.readLine()) != null) {
  107.         String key = getId(line);
  108.         List<String> l = map.get(key);
  109.         if (l == null) {
  110.         l = new LinkedList<String>();
  111.         map.put(key,l);
  112.         }
  113.         l.add(line);       
  114.     }
  115.     reader.close();
  116.     FileWriter writer = new FileWriter("Employee_Data.csv");
  117.     writer.write("First Name,Last Name,Department,ID,Birthday\n");
  118.     for (List<String> list : map.values()) {
  119.         for (String val : list) {
  120.         if (!getId(val).equals(Integer.toString(id))) {
  121.         writer.write(val);
  122.         writer.write("\n");
  123.         }
  124.         }
  125.     }
  126.         writer.close();
  127.     }
  128.  
  129.     private static List<Employee> loadFile(String filename) throws Exception {
  130.     BufferedReader reader = new BufferedReader(new FileReader(filename));
  131.     List<Employee> empList = new ArrayList<Employee>();
  132.     String line = reader.readLine();
  133.     while ((line = reader.readLine()) != null) {
  134.         empList.add(toClass(line));
  135.     }
  136.     reader.close();
  137.     return empList;
  138.     }
  139.    
  140.     private static String getFirstname(String line) {
  141.     return line.split(",")[0];
  142.     }
  143.  
  144.     private static String getLastname(String line) {
  145.     return line.split(",")[1];
  146.     }
  147.  
  148.     private static String getDepartment(String line) {
  149.     return line.split(",")[2];
  150.     }
  151.  
  152.     private static String getId(String line) {
  153.     return line.split(",")[3];
  154.     }
  155.  
  156.     private static String getBirthday(String line) {
  157.     return line.split(",")[4];
  158.     }
  159.    
  160.     public static void main(String[] args) throws Exception {
  161.     MyEmployeeManager manager = new MyEmployeeManager();
  162.     Employee me = new Employee("Clayton", "Stuart", "Tech Support", "101", "10/31/92");
  163.     //Read the sample data from the attached CSV file into the EmployeeManager
  164.     System.out.println("This is loading the .csv file, and storing all employees as Employee objects in a list, and then printing it.");
  165.     for(Employee emp : manager.loadFile("Employee_Data.csv")) {
  166.         System.out.println(emp.printEmployee());
  167.     }
  168.     //output the total number of employees
  169.     System.out.println("Print Employee Count\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  170.     System.out.println(manager.getEmployeeCount());
  171.     //Call the removeEmployee method to remove an employee from the list (remove with ID=49)
  172.     System.out.println("Remove employee with ID# 49 from list");
  173.     manager.removeEmployee(49);
  174.     //output the total number of employees
  175.     System.out.println("Print Employee Count\n~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  176.     System.out.println(manager.getEmployeeCount());
  177.     /**Get a list of employees sorted by department, and check whether the list is,
  178.        in fact, sorted correctly (Verify that the first department is Accounting and
  179.        the last department is Tech Support). Output the result of your checks.**/
  180.     System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  181.     System.out.println("Get all employees sorted by Department");
  182.     int i = 0;
  183.     for(Employee emp : manager.getAllEmployeesSortedByDept()) {
  184.         i++;
  185.         if (i == 1) {
  186.         System.out.println("This should be accounting");
  187.         System.out.println(emp.getDepartment());
  188.         }
  189.  
  190.         if (i == manager.getEmployeeCount() - 1) {
  191.         System.out.println("This should be Tech Support.");
  192.         System.out.println(emp.getDepartment());
  193.         }
  194.  
  195.         System.out.println(emp.printEmployee());
  196.     }
  197.     /**Get a list of employees in the Tech Support department.  
  198.        Check to make sure that each employee in the list is in fact
  199.        in the Tech Support department.
  200.        Output the result of your checks.**/
  201.     System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  202.     System.out.println("Find Employee by department 'Tech Support'");
  203.     for(Employee emp : manager.findEmployeesByDepartment("Tech Support")) {
  204.         if (emp.getDepartment().equals("Tech Support")) {
  205.         System.out.println("This person is DEFINITELY in Tech support");
  206.         }
  207.         System.out.println(emp.printEmployee());
  208.     }
  209.     //Fetch employee with ID = 92. Output the result of your check.
  210.     System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  211.     System.out.println("Find employee with ID# 92");
  212.     System.out.println(manager.findEmployeeById(92).printEmployee());
  213.     System.out.println("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
  214.     System.out.println("Add employee based on myself to list");
  215.     manager.addEmployee(me);
  216.     System.out.println(manager.findEmployeeById(101).printEmployee());
  217.    
  218.    
  219.     }
  220.    
  221. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement