/* TestMap.java CIS 160 David Klick 2009-12-01 Demonstration of HashMap class using Employee class. */ import java.util.HashMap; import java.util.Map; public class TestMap { public static void main(String[] args) throws InvalidEmployeeException { HashMap employees = new HashMap(); employees.put(35, new Employee("Horton", "Hoo", 35, 12.95)); employees.put(18, new Employee("Billie", "Brisk", 18, 9.50)); employees.put(81, new Employee("Helen", "Hoo", 81, 13.25)); employees.put(22, new Employee("Alginate", "Arkin", 22, 14.50)); employees.put(61, new Employee("Wyatt", "Burp", 61, 10.05)); employees.put(412, new Employee("Maria", "Morales", 412, 11.00)); employees.put(818, new Employee("Eve", "Morning", 818, 9.25)); employees.put(111, 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..."); Employee e = employees.get(61); if (e == null) System.out.println("Employee ID 61 not found"); else System.out.println("Found: " + e.getFullName()); e = employees.get(64); if (e == null) System.out.println("Employee ID 64 not found"); else System.out.println("Found: " + e.getFullName()); System.out.println("\nList of employees:"); for (Map.Entry e2 : employees.entrySet()) { System.out.println(e2.getValue()); } } } class Employee implements Comparable { 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 String getFullName() { return (firstName + " " + initial).trim() + " " + lastName; } 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 {81=Helen Hoo, id=0000081, rate=$ 13.25, 18=Billie Brisk, id=0000018, rate=$ 9.50, 818=Eve Morning, id=0000818, rate=$ 9.25, 35=Horton Hoo, id=0000035, rate=$ 12.95, 22=Alginate Arkin, id=0000022, rate=$ 14.50, 412=Maria Morales, id=0000412, rate=$ 11.00, 61=Wyatt Burp, id=0000061, rate=$ 10.05, 111=Bob Weaver, id=0000111, rate=$ 8.75} Searching for employees... Found: Wyatt Burp Employee ID 64 not found List of employees: Helen Hoo, id=0000081, rate=$ 13.25 Billie Brisk, id=0000018, rate=$ 9.50 Eve Morning, id=0000818, rate=$ 9.25 Horton Hoo, id=0000035, rate=$ 12.95 Alginate Arkin, id=0000022, rate=$ 14.50 Maria Morales, id=0000412, rate=$ 11.00 Wyatt Burp, id=0000061, rate=$ 10.05 Bob Weaver, id=0000111, rate=$ 8.75 */