public class Employee { private final int employeeID; private final String lastName; private final String[] firstNames; private final double hourlyRate; // Note: This code assumes InvalidEmployeeException // is in the same package as this source code. public Employee(final int id, final double hourlyRate, final String[] firstNames, final String lastName) throws InvalidEmployeeException { // set employeeID if (id <= 0) throw new InvalidEmployeeException("ID less than 1"); this.employeeID = id; // set hourlyRate if (hourlyRate < 0.0) throw new InvalidEmployeeException("Employee rate less than 0"); this.hourlyRate = hourlyRate; // set firstNames if (firstNames == null) this.firstNames = new String[0]; else this.firstNames = (String[])firstNames.clone(); // set lastName if (lastName == null) throw new InvalidEmployeeException("Last name is missing"); else this.lastName = lastName; } public Employee(final int id, final double hourlyRate, final String firstName1, final String firstName2, final String lastName) throws InvalidEmployeeException { this(id, hourlyRate, new String[]{firstName1, firstName2}, lastName); } public Employee(final int id, final double hourlyRate, final String firstName, final String lastName) throws InvalidEmployeeException { this(id, hourlyRate, new String[]{firstName}, lastName); } public Employee(final int id, final double hourlyRate, final String lastName) throws InvalidEmployeeException { this(id, hourlyRate, new String[0], lastName); } public int getID() { return employeeID; } public double getHourlyRate() { return hourlyRate; } public String getFirstName(final int pos) { if (pos >= firstNames.length) return null; else return firstNames[pos]; } public String getLastName() { return lastName; } public int getNumberOfFirstNames() { return firstNames.length; } public String toString() { String result = "Employee: #" + employeeID + ", Name: "; for (String s : firstNames) result = result + s + " "; result += lastName + " (Rate: " + String.format("$%.2f", hourlyRate) + ")"; return result; } }