/* TestEmployee2.java CIS 160 David Klick 2009-12-01 Employee class implementation using a Vector for storage. */ import java.util.Collections; import java.util.Vector; public class TestEmployee2 { public static void main(String[] args) throws InvalidEmployeeException { Vector employees = new Vector(); employees.add(new Employee("Horton", "Hoo", 35, 12.95)); employees.add(new Employee("Billie", "Brisk", 18, 9.50)); employees.add(new Employee("Helen", "Hoo", 81, 13.25)); employees.add(new Employee("Alginate", "Arkin", 22, 14.50)); employees.add(new Employee("Wyatt", "Burp", 61, 10.05)); employees.add(new Employee("Maria", "Morales", 412, 11.00)); employees.add(new Employee("Eve", "Morning", 818, 9.25)); employees.add(new Employee("Bob", "Weaver", 111, 8.75)); System.out.println("Number of employees: " + employees.size()); System.out.println(employees); System.out.println("\nSearching for employees..."); int pos = findEmployeeByID(employees, 61); if (pos == -1) System.out.println("Employee ID 61 not found"); else System.out.println("Found: " + employees.get(pos)); pos = findEmployeeByID(employees, 64); if (pos == -1) System.out.println("Employee ID 64 not found"); else System.out.println("Found: " + employees.get(pos)); System.out.println("\nSorting employees..."); Collections.sort(employees); System.out.println(employees); System.out.println("\nEmployee list:"); for (Employee e : employees) { System.out.println(e); } Employee e; System.out.println("\nAttempt to create invalid employees"); try { e = new Employee("David", "G", "Klick", -16, 9.30); } catch (InvalidEmployeeException ex) { System.out.println("Error creating employee: " + ex.getMessage()); } try { e = new Employee("David", "G", "Klick", 16, -9.30); } catch (InvalidEmployeeException ex) { System.out.println("Error creating employee: " + ex.getMessage()); } Employee e1 = new Employee("ALGINATE", "ARKIN", 22, 14.50); Employee e2 = new Employee("Alginate", "Arkin", 27, 14.50); System.out.println("\nAttempting to find duplicate employee records"); if (employees.contains(e1)) { System.out.println("Found in vector: " + e1); } else { System.out.println("Not found in vector: " + e1); } if (employees.contains(e2)) { System.out.println("Found in vector: " + e2); } else { System.out.println("Not found in vector: " + e2); } } private static int findEmployeeByID(Vector v, int id) { for (int i=0; i { private String lastName; private String firstName; private String initial; private int id; private double rate; public Employee(String fn, String ln, int id, double rt) throws InvalidEmployeeException { this(fn, "", ln, id, rt); } public Employee(String fn, String mi, String ln, int id, double rt) throws InvalidEmployeeException { setFirstName(fn); setMiddleInitial(mi); setLastName(ln); setID(id); setRate(rt); if ((firstName.length() + lastName.length()) == 0) { throw new InvalidEmployeeException("Invalid employee name"); } } public void setFirstName(String s) { if (s == null) firstName = ""; else firstName = s; } public void setMiddleInitial(String s) { if (s == null) initial = ""; else initial = s; } public void setLastName(String s) { if (s == null) lastName = ""; else lastName = s; } public void setID(int id) throws InvalidEmployeeException { if (id < 0) throw new InvalidEmployeeException("Invalid employee ID"); else this.id = id; } public void setRate(double rate) throws InvalidEmployeeException { if (rate < 0) throw new InvalidEmployeeException("Invalid employee rate"); else this.rate = rate; } public String getFirstName() { return firstName; } public String getMiddleInitial() { return initial; } public String getLastName() { return lastName; } public int getID() { return id; } public double getRate() { return rate; } public String toString() { String fullName = (firstName + " " + initial).trim() + " " + lastName; return fullName + ", id=" + String.format("%07d", id) + ", rate=" + String.format("$%6.2f", rate); } public boolean equals(Object o) { if (! (o instanceof Employee)) return false; Employee e = (Employee) o; // rates are ignored in the comparison return firstName.equalsIgnoreCase(e.firstName) && initial.equalsIgnoreCase(e.initial) && lastName.equalsIgnoreCase(e.lastName) && id == e.id; } public int compareTo(Employee e) { // rates are ignored in the comparison int n = lastName.compareToIgnoreCase(e.lastName); if (n != 0) return n; n = firstName.compareToIgnoreCase(e.firstName); if (n != 0) return n; n = initial.compareToIgnoreCase(e.initial); if (n != 0) return n; return id - e.id; } } class InvalidEmployeeException extends Exception { public InvalidEmployeeException() {} public InvalidEmployeeException(String msg) { super(msg); } } /* Sample run: Number of employees: 8 [Horton Hoo, id=0000035, rate=$ 12.95, Billie Brisk, id=0000018, rate=$ 9.50, Helen Hoo, id=0000081, rate=$ 13.25, Alginate Arkin, id=0000022, rate=$ 14.50, Wyatt Burp, id=0000061, rate=$ 10.05, Maria Morales, id=0000412, rate=$ 11.00, Eve Morning, id=0000818, rate=$ 9.25, Bob Weaver, id=0000111, rate=$ 8.75] Searching for employees... Found: Wyatt Burp, id=0000061, rate=$ 10.05 Employee ID 64 not found Sorting employees... [Alginate Arkin, id=0000022, rate=$ 14.50, Billie Brisk, id=0000018, rate=$ 9.50, Wyatt Burp, id=0000061, rate=$ 10.05, Helen Hoo, id=0000081, rate=$ 13.25, Horton Hoo, id=0000035, rate=$ 12.95, Maria Morales, id=0000412, rate=$ 11.00, Eve Morning, id=0000818, rate=$ 9.25, Bob Weaver, id=0000111, rate=$ 8.75] Employee list: Alginate Arkin, id=0000022, rate=$ 14.50 Billie Brisk, id=0000018, rate=$ 9.50 Wyatt Burp, id=0000061, rate=$ 10.05 Helen Hoo, id=0000081, rate=$ 13.25 Horton Hoo, id=0000035, rate=$ 12.95 Maria Morales, id=0000412, rate=$ 11.00 Eve Morning, id=0000818, rate=$ 9.25 Bob Weaver, id=0000111, rate=$ 8.75 Attempt to create invalid employees Error creating employee: Invalid employee ID Error creating employee: Invalid employee rate Attempting to find duplicate employee records Found in vector: ALGINATE ARKIN, id=0000022, rate=$ 14.50 Not found in vector: Alginate Arkin, id=0000027, rate=$ 14.50 */