package java112.employee; import java.util.*; import java.io.*; /** * This class returns a tab delimited file that can be written to the employee * file. * *@author Rasmussen *@created November 16, 2006 */ public class EmployeeDirectory { private List empList = new ArrayList(); private Map searchEmpId = new HashMap(); private Map searchLastName = new HashMap(); private BufferedReader in = null; String tab = "\\t"; private String empFile = ""; private Employee addEmp; int empIdGenerator; private PrintWriter out = null; /** * Description of the Method * *@param inputFile Description of the Parameter */ public void createEmployeesCollection(String inputFile) { empFile = inputFile; try { in = new BufferedReader(new FileReader(inputFile)); while (in.ready()) { System.out.println("start Create Employees Collection"); String tokens[] = in.readLine().split(tab); Employee emp = new Employee(tokens[0], tokens[1], tokens[2], tokens[3], tokens[4], tokens[5], tokens[6], tokens[7]); int empInt = Integer.parseInt(tokens[0]); if (empInt > empIdGenerator) { empIdGenerator = empInt; } System.out.print(empIdGenerator); empList.add(emp); searchEmpId.put(emp.getEmployeeId(), emp); System.out.println(" start putting emp in LastName"); String lname = emp.getLastName(); if (searchLastName.containsKey(lname)) { System.out.println(lname); } System.out.println("putting emp in LastName list worked!!!"); System.out.print(searchLastName); System.out.println(searchEmpId); } } catch (FileNotFoundException fnfe) { System.out.println("Failed to read input file"); fnfe.printStackTrace(); } catch (IOException ioe) { System.out.println("Failed to read input file"); ioe.printStackTrace(); } finally { try { if (in != null) { in.close(); } } catch (Exception e) { System.out.println("Failed to read input file"); e.printStackTrace(); return; } } } /** * Adds a feature to the NewEmployee attribute of the EmployeeDirectory * object * *@param firstName The feature to be added to the NewEmployee attribute *@param lastName The feature to be added to the NewEmployee attribute *@param position The feature to be added to the NewEmployee attribute *@param ssn The feature to be added to the NewEmployee attribute *@param department The feature to be added to the NewEmployee attribute *@param roomNumber The feature to be added to the NewEmployee attribute *@param phoneNumber The feature to be added to the NewEmployee attribute */ public void addNewEmployee(String firstName, String lastName, String position, String ssn, String department, String roomNumber, String phoneNumber) { System.out.println("starting addNewEmployee"); String empId = String.valueOf(++empIdGenerator); addEmp = new Employee(empId, firstName, lastName, position, ssn, department, roomNumber, phoneNumber); empList.add(addEmp); searchEmpId.put(addEmp.getEmployeeId(), addEmp); String lname = addEmp.getLastName(); if (searchLastName.containsKey(lname)) { List temp = (List) searchLastName.get(lname); temp.add(addEmp); searchLastName.put(lname, temp); } else { searchLastName.put(lname, addEmp); } writeEmployee(); System.out.print(searchLastName); System.out.println(searchEmpId); } /** * Description of the Method */ public synchronized void writeEmployee() { try { out = new PrintWriter(new BufferedWriter(new FileWriter(empFile, true))); out.println(addEmp.employeeRecord()); } catch (FileNotFoundException fnfe) { System.out.println("Failed to write output employee.txt file"); fnfe.printStackTrace(); return; } catch (IOException ioe) { System.out.println("Failed to write output employee.txt file"); ioe.printStackTrace(); return; } catch (Exception e) { System.out.println("Failed to write output employee.txt file"); e.printStackTrace(); return; } finally { out.close(); } } /** * Description of the Method * *@param id Description of the Parameter *@return Description of the Return Value */ public List searchForId(String id) { List returnList = new ArrayList(); if (searchEmpId.containsKey(id)) { returnList.add(searchEmpId.get(id)); } return returnList; } //* The method will search for the Employee with that employee id. //* If the Employee is found then return the Employee object in an instance of an ArrayList. This is done so the both searches return the same data type. //* If the Employee is not found then return a new empty ArrayList. /** * Description of the Method * *@param lname Description of the Parameter *@return Description of the Return Value */ public List searchForLastName(String lname) { if (searchLastName.containsKey(lname)) { return (ArrayList) searchLastName.get(lname); } else { return new ArrayList(); } //* The method will have an argument that is a last name. //* The method will search for all employees with that last name. //* If the employees with that last name are found then return the List of Employees that have the same last name. //* If the Employee is not found then return a new empty ArrayList. } }