This section presents the design and code for a comprehensive example application that utilizes all the patterns discussed in this chapter. The application presented here allows you to create, edit, and delete hourly and salaried employees. It also lets you sort employees and save and retrieve employee data to and from disk. Figure 25-4 shows the UML class diagram for this application.
Figure 25-4: Employee ManagementApplication UML Class Diagram
This section gives the complete code listing for the application classes organized by package name.
Example 25.16: BaseCommand.java
1 package com.pulpfreepress.commands; 2 3 import com.pulpfreepress.interfaces.*; 4 5 public abstract class BaseCommand { 6 protected static iModel its_model = null; 7 protected static iView its_view = null; 8 9 public void setModel(iModel model){ 10 if(its_model == null){ 11 its_model = model; 12 } 13 } 14 15 public void setView(iView view){ 16 if(its_view == null){ 17 its_view = view; 18 } 19 } 20 21 public abstract void execute(); // must be implemented in derived classes 22 23 } // end BaseCommand class definition
Example 25.17: ApplicationExitCommand.java
1 package com.pulpfreepress.commands; 2 3 public class ApplicationExitCommand extends BaseCommand { 4 public void execute(){ 5 System.exit(0); 6 } 7 }
Example 25.18: DeleteEmployeeCommand.java
1 package com.pulpfreepress.commands; 2 3 public class DeleteEmployeeCommand extends BaseCommand { 4 5 public void execute(){ 6 if((its_model != null) && (its_view != null)){ 7 String employee_number = its_view.getDeleteEmployeeInfo(); 8 if(employee_number != null) { 9 its_model.deleteEmployee(its_view.getDeleteEmployeeInfo()); 10 } 11 } 12 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 13 } // end execute() method 14 } // end DeleteEmployeeCommand class definition
Example 25.19: EditEmployeeCommand.java
1 package com.pulpfreepress.commands; 2 3 public class EditEmployeeCommand extends BaseCommand { 4 5 public void execute(){ 6 if((its_model != null) && (its_view != null)){ 7 String[] emp_info = its_view.getEditEmployeeInfo(); 8 try{ 9 its_model.editEmployeeInfo(emp_info[0], emp_info[1], emp_info[2], 10 Integer.parseInt(emp_info[4]), Integer.parseInt(emp_info[5]), 11 Integer.parseInt(emp_info[6]), emp_info[3], emp_info[7]); 12 }catch(Throwable t){ 13 System.out.println("Invalid employee information - employee edit cancled!"); 14 } 15 } 16 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 17 } // end execute() method 18 } // end EditEmployeeCommand class definition
Example 25.20: ListEmployeesCommand.java
1 package com.pulpfreepress.commands; 2 3 public class ListEmployeesCommand extends BaseCommand { 4 public void execute(){ 5 if(its_model != null){ 6 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 7 } 8 } 9 } // end ListEmployeesCommand class definition
Example 25.21: LoadEmployeesCommand.java
1 package com.pulpfreepress.commands; 2 3 public class LoadEmployeesCommand extends BaseCommand { 4 5 public void execute(){ 6 if((its_model != null) && (its_view != null)){ 7 its_model.loadEmployeesFromFile(its_view.getLoadFile()); 8 9 } 10 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 11 } // end execute() method 12 } // end DeleteEmployeeCommand class definition
Example 25.22: NewHourlyEmployeeCommand.java
1 package com.pulpfreepress.commands; 2 3 public class NewHourlyEmployeeCommand extends BaseCommand { 4 public void execute(){ 5 if(its_model != null){ 6 String[] emp_info = its_view.getNewHourlyEmployeeInfo(); 7 try{ 8 its_model.createHourlyEmployee(emp_info[0], emp_info[1], emp_info[2], 9 Integer.parseInt(emp_info[4]), Integer.parseInt(emp_info[5]), 10 Integer.parseInt(emp_info[6]), emp_info[3], emp_info[7], 11 Double.parseDouble(emp_info[8]), Double.parseDouble(emp_info[9])); 12 }catch(Throwable t){ 13 System.out.println("Invalid employee information - employee not created!"); 14 } 15 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 16 } 17 } // end execute() method 18 } // end NewHourlyEmployeeCommand class definition
Example 25.23: NewSalariedEmployeeCommand.java
1 package com.pulpfreepress.commands; 2 3 public class NewSalariedEmployeeCommand extends BaseCommand { 4 public void execute(){ 5 if(its_model != null){ 6 String[] emp_info = its_view.getNewSalariedEmployeeInfo(); 7 try{ 8 its_model.createSalariedEmployee(emp_info[0], emp_info[1], emp_info[2], 9 Integer.parseInt(emp_info[4]), Integer.parseInt(emp_info[5]), 10 Integer.parseInt(emp_info[6]), emp_info[3], emp_info[7], 11 Double.parseDouble(emp_info[10])); 12 }catch(Throwable t){ 13 System.out.println("Invalid employee information - employee not created!"); 14 } 15 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 16 } 17 } // end execute() method 18 } // end NewSalariedEmployeeCommand class definition
Example 25.24: SaveEmployeesCommand.java
1 package com.pulpfreepress.commands; 2 3 public class SaveEmployeesCommand extends BaseCommand { 4 5 public void execute(){ 6 if((its_model != null) && (its_view != null)){ 7 its_model.saveEmployeesToFile(its_view.getSaveFile()); 8 9 } 10 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 11 } // end execute() method 12 } // end DeleteEmployeeCommand class definition
Example 25.25: SortEmployeesCommand.java
1 package com.pulpfreepress.commands; 2 3 public class SortEmployeesCommand extends BaseCommand { 4 public void execute(){ 5 if(its_model != null){ 6 its_model.sortEmployees(); 7 its_view.displayEmployeeInfo(its_model.getAllEmployeesInfo()); 8 } 9 } // end execute() method 10 } // end SortEmployeesCommand class definition
Example 25.26: Controller.java
1 package com.pulpfreepress.controller; 2 3 import com.pulpfreepress.utils.*; 4 import com.pulpfreepress.commands.*; 5 import com.pulpfreepress.exceptions.*; 6 import com.pulpfreepress.model.*; 7 import com.pulpfreepress.view.*; 8 import com.pulpfreepress.interfaces.*; 9 import java.awt.event.*; 10 11 public class Controller implements ActionListener { 12 13 private CommandFactory command_factory = null; 14 private iModel its_model; 15 private iView its_view; 16 17 public Controller(){ 18 command_factory = CommandFactory.getInstance(); 19 its_model = new Model(); 20 its_view = new View(this); 21 } 22 23 24 public void actionPerformed(ActionEvent ae){ 25 try{ 26 BaseCommand command = command_factory.getCommand(ae.getActionCommand()); 27 command.setModel(its_model); 28 command.setView(its_view); 29 command.execute(); 30 }catch(CommandNotFoundException cnfe){ 31 System.out.println("Command not found!"); 32 } 33 } 34 35 public static void main(String[] args){ 36 new Controller(); 37 } // end main() method 38 } // end Controller class definition
Example 25.27: CommandNotFoundException.java
1 package com.pulpfreepress.exceptions; 2 3 public class CommandNotFoundException extends Exception { 4 5 public CommandNotFoundException(String message, Throwable ex){ 6 super(message, ex); 7 } 8 9 public CommandNotFoundException(String message){ 10 super(message); 11 } 12 13 14 public CommandNotFoundException(){ 15 this("Command Not Found Exception"); 16 } 17 } // end CommandNotFoundException class definition
Example 25.28: iModel.java
1 package com.pulpfreepress.interfaces; 2 3 import java.util.*; 4 import java.io.*; 5 6 public interface iModel { 7 public void createHourlyEmployee(String fname, String mname, String lname, 8 int dob_year, int dob_month, int dob_day, 9 String gender, String employee_number, 10 double hours_worked, double hourly_rate); 11 12 public void createSalariedEmployee(String fname, String mname, String lname, 13 int dob_year, int dob_month, int dob_day, 14 String gender, String employee_number, 15 double salary); 16 17 public void editEmployeeInfo(String fname, String mname, String lname, 18 int dob_year, int dob_month, int dob_day, 19 String gender, String employee_number); 20 21 22 public String[] getAllEmployeesInfo(); 23 public String getEmployeeInfo(String employee_number); 24 public void sortEmployees(); 25 public void deleteEmployee(String employee_number); 26 public void saveEmployeesToFile(File file); 27 public void loadEmployeesFromFile(File file); 28 } // end iModel interface definition
Example 25.29: iView.java
1 package com.pulpfreepress.interfaces; 2 import java.io.*; 3 4 public interface iView { 5 public void displayEmployeeInfo(String[] employees_info); 6 public String[] getNewHourlyEmployeeInfo(); 7 public String[] getNewSalariedEmployeeInfo(); 8 public String[] getEditEmployeeInfo(); 9 public String getDeleteEmployeeInfo(); 10 public File getSaveFile(); 11 public File getLoadFile(); 12 13 } // end iView interface definition
Example 25.30: IEmployee.java
1 package com.pulpfreepress.model; 2 3 import java.util.*; 4 5 public interface IEmployee { 6 int getAge(); 7 String getFullName(); 8 String getNameAndAge(); 9 String getFirstName(); 10 String getMiddleName(); 11 String getLastName(); 12 String getGender(); 13 String getEmployeeNumber(); 14 void setBirthday(int year, int month, int day); 15 Calendar getBirthday(); 16 void setFirstName(String f_name); 17 void setMiddleName(String m_name); 18 void setLastName(String l_name); 19 void setGender(String gender); 20 void setEmployeeNumber(String emp_no); 21 void setPayInfo(PayInfo pi); 22 double getPay(); 23 String toString(); 24 }
Example 25.31: Employee.java
1 package com.pulpfreepress.model; 2 3 import java.util.*; 4 import java.io.*; 5 6 public abstract class Employee implements IEmployee, Cloneable, Comparable, Serializable { 7 private Person _person = null; 8 private String _employee_number = null; 9 10 protected Employee(String f_name, String m_name, String l_name, int dob_year, int dob_month, 11 int dob_day, String gender, String employee_number){ 12 _person = new Person(f_name, m_name, l_name, dob_year, dob_month, dob_day, gender); 13 _employee_number = employee_number; 14 } // end constructor 15 16 public int getAge() { return _person.getAge(); } 17 public String getFullName() { return _person.getFullName(); } 18 public String getNameAndAge() { return _person.getNameAndAge(); } 19 public String getFirstName() { return _person.getFirstName(); } 20 public String getMiddleName() { return _person.getMiddleName(); } 21 public String getLastName() { return _person.getLastName(); } 22 public String getGender() { return _person.getGender(); } 23 public String getEmployeeNumber() { return _employee_number; } 24 public void setBirthday(int year, int month, int day) { _person.setBirthday(year, month, day); } 25 public Calendar getBirthday() { return _person.getBirthday(); } 26 public void setFirstName(String f_name) { _person.setFirstName(f_name); } 27 public void setMiddleName(String m_name) { _person.setMiddleName(m_name); } 28 public void setLastName(String l_name) { _person.setLastName(l_name); } 29 public void setGender(String gender) { _person.setGender(gender); } 30 public void setEmployeeNumber(String emp_no) { _employee_number = emp_no; } 31 32 public String toString(){ return _person.toString() + " " + _employee_number; } 33 34 public boolean equals(Object o){ 35 if(o == null) return false; 36 boolean is_equal = false; 37 if(o instanceof Employee){ 38 if(this.toString().equals(o.toString())){ 39 is_equal = true; 40 } 41 } 42 return is_equal; 43 } 44 45 public int hashCode(){ 46 return this.toString().hashCode(); 47 } 48 49 50 public int compareTo(Object o){ 51 return this.toString().compareTo(o.toString()); 52 } 53 54 public abstract void setPayInfo(PayInfo pi); // defer implementation 55 public abstract double getPay(); // defer implementation 56 57 } // end Employee class definition
Example 25.32: Person.java
1 package com.pulpfreepress.model; 2 3 import java.util.*; 4 import java.io.*; 5 6 public class Person implements Cloneable, Comparable, Serializable { 7 private String first_name = null; 8 private String middle_name = null; 9 private String last_name = null; 10 private Calendar birthday = null; 11 private String gender = null; 12 13 public static final String MALE = "Male"; 14 public static final String FEMALE = "Female"; 15 16 public Person(String f_name, String m_name, String l_name, int dob_year, int dob_month, 17 int dob_day, String gender){ 18 first_name = f_name; 19 middle_name = m_name; 20 last_name = l_name; 21 this.gender = gender; 22 23 birthday = Calendar.getInstance(); 24 birthday.set(dob_year, dob_month, dob_day); 25 } 26 27 public int getAge(){ 28 Calendar today = Calendar.getInstance(); 29 int now = today.get(Calendar.YEAR); 30 int then = birthday.get(Calendar.YEAR); 31 return (now - then); 32 } 33 34 public String getFullName(){ return (first_name + " " + middle_name + " " + last_name); } 35 36 public String getFirstName(){ return first_name; } 37 public void setFirstName(String f_name) { first_name = f_name; } 38 39 public String getMiddleName(){ return middle_name; } 40 public void setMiddleName(String m_name){ middle_name = m_name; } 41 42 public String getLastName(){ return last_name; } 43 public void setLastName(String l_name){ last_name = l_name; } 44 45 public String getNameAndAge(){ return (getFullName() + " " + getAge()); } 46 47 public String getGender(){ return gender; } 48 public void setGender(String gender){ this.gender = gender; } 49 50 public void setBirthday(int year, int month, int day){ birthday.set(year, month, day); } 51 public Calendar getBirthday(){ return birthday; } 52 53 public String toString(){ 54 return this.getFullName() + " " + gender + " " + birthday.get(Calendar.DATE) + "/" 55 + birthday.get(Calendar.MONTH) + "/" + birthday.get(Calendar.YEAR); 56 } 57 58 public boolean equals(Object o){ 59 if(o == null) return false; 60 boolean is_equal = false; 61 if(o instanceof Person){ 62 if(this.first_name.equals(((Person)o).first_name) && 63 this.middle_name.equals(((Person)o).middle_name) && 64 this.last_name.equals(((Person)o).last_name) && this.gender.equals(((Person)o).gender) && 65 (this.birthday.get(Calendar.YEAR) == ((Person)o).birthday.get(Calendar.YEAR)) && 66 (this.birthday.get(Calendar.MONTH) == ((Person)o).birthday.get(Calendar.MONTH)) && 67 (this.birthday.get(Calendar.DATE) == ((Person)o).birthday.get(Calendar.DATE)) ){ 68 is_equal = true; 69 } 70 } 71 return is_equal; 72 } 73 74 public int hashCode(){ 75 return this.toString().hashCode(); 76 } 77 78 public Object clone() throws CloneNotSupportedException { 79 super.clone(); 80 return new Person(new String(first_name), new String(middle_name), new String(last_name), 81 birthday.get(Calendar.YEAR), birthday.get(Calendar.MONTH), 82 birthday.get(Calendar.DATE), new String(gender)); 83 } 84 85 public int compareTo(Object o){ 86 return this.toString().compareTo(o.toString()); 87 } 88 89 } //end Person class
Example 25.33: HourlyEmployee.java
1 package com.pulpfreepress.model; 2 3 import java.util.*; 4 import java.io.*; 5 import java.text.*; 6 7 public class HourlyEmployee extends Employee implements Cloneable, Comparable, Serializable{ 8 private double _hours_worked = 0; 9 private double _hourly_rate = 0.0; 10 11 public HourlyEmployee(String f_name, String m_name, String l_name, int dob_year, int dob_month, 12 int dob_day, String gender, String employee_number){ 13 super(f_name, m_name, l_name, dob_year, dob_month, dob_day, gender, employee_number); 14 } 15 16 public void setPayInfo(PayInfo pi){ 17 _hours_worked = pi.getHoursWorked(); 18 _hourly_rate = pi.getHourlyRate(); 19 } 20 21 public double getPay() { return _hours_worked * _hourly_rate; } 22 23 public String toString() { 24 NumberFormat pay_format = NumberFormat.getInstance(); 25 return super.toString() + " $" + pay_format.format(getPay()); 26 27 } 28 29 public Object clone() throws CloneNotSupportedException { 30 super.clone(); 31 return new HourlyEmployee(new String(getFirstName()), new String(getMiddleName()), 32 new String(getLastName()), getBirthday().get(Calendar.YEAR), 33 getBirthday().get(Calendar.MONTH), getBirthday().get(Calendar.DATE), 34 new String(getGender()), new String(getEmployeeNumber())); 35 } 36 37 public boolean equals(Object o){ 38 if(o == null) return false; 39 boolean is_equal = false; 40 if(o instanceof HourlyEmployee){ 41 if(this.toString().equals(o.toString())){ 42 is_equal = true; 43 } 44 } 45 return is_equal; 46 } 47 48 public int hashCode(){ 49 return this.toString().hashCode(); 50 } 51 52 53 public int compareTo(Object o){ 54 return this.toString().compareTo(o.toString()); 55 } 56 57 58 } // end HourlyEmployee class definition
Example 25.34: SalariedEmployee.java
1 package com.pulpfreepress.model; 2 3 import java.util.*; 4 import java.io.*; 5 import java.text.*; 6 7 public class SalariedEmployee extends Employee implements Cloneable, Comparable, Serializable { 8 private double _salary = 0; 9 10 public SalariedEmployee(String f_name, String m_name, String l_name, int dob_year, int dob_month, 11 int dob_day, String gender, String employee_number){ 12 super(f_name, m_name, l_name, dob_year, dob_month, dob_day, gender, employee_number); 13 } 14 15 public void setPayInfo(PayInfo pi){ 16 _salary = pi.getSalary(); 17 } 18 19 public double getPay() { return (_salary/12.0)/2.0; } 20 21 public String toString() { 22 NumberFormat pay_format = NumberFormat.getInstance(); 23 return super.toString() + " $" + pay_format.format(getPay()); 24 } 25 26 27 public Object clone() throws CloneNotSupportedException { 28 super.clone(); 29 return new SalariedEmployee(new String(getFirstName()), new String(getMiddleName()), 30 new String(getLastName()), getBirthday().get(Calendar.YEAR), 31 getBirthday().get(Calendar.MONTH), getBirthday().get(Calendar.DATE), 32 new String(getGender()), new String(getEmployeeNumber())); 33 } 34 35 public boolean equals(Object o){ 36 if(o == null) return false; 37 boolean is_equal = false; 38 if(o instanceof SalariedEmployee){ 39 if(this.toString().equals(o.toString())){ 40 is_equal = true; 41 } 42 } 43 return is_equal; 44 } 45 46 public int hashCode(){ 47 return this.toString().hashCode(); 48 } 49 50 51 public int compareTo(Object o){ 52 return this.toString().compareTo(o.toString()); 53 } 54 } // end SalariedEmployee class definition
Example 25.35: PayInfo.java
1 package com.pulpfreepress.model; 2 3 public class PayInfo { 4 private double _salary = 0; 5 private double _hours_worked = 0; 6 private double _hourly_rate = 0; 7 8 public PayInfo(double salary){ 9 _salary = salary; 10 } 11 12 public PayInfo(double hours_worked, double hourly_rate){ 13 _hours_worked = hours_worked; 14 _hourly_rate = hourly_rate; 15 } 16 17 public PayInfo(){ } 18 19 public double getHoursWorked(){ return _hours_worked; } 20 public double getHourlyRate(){ return _hourly_rate; } 21 public double getSalary(){ return _salary; } 22 23 } // end PayInfo class definition
Example 25.36: IEmployeeFactory.java
1 package com.pulpfreepress.model; 2 3 public interface IEmployeeFactory { 4 IEmployee getNewSalariedEmployee(String f_name, String m_name, String l_name, int dob_year, 5 int dob_month, int dob_day, String gender, String employee_number); 6 IEmployee getNewHourlyEmployee(String f_name, String m_name, String l_name, int dob_year, 7 int dob_month, int dob_day, String gender, String employee_number); 8 }
Example 25.37: EmployeeFactory.java
1 package com.pulpfreepress.model; 2 3 public class EmployeeFactory implements IEmployeeFactory { 4 5 public IEmployee getNewSalariedEmployee(String f_name, String m_name, String l_name, 6 int dob_year, int dob_month, int dob_day, String gender, 7 String employee_number){ 8 return new SalariedEmployee(f_name, m_name, l_name, dob_year, dob_month, dob_day, gender, 9 employee_number); 10 } 11 12 public IEmployee getNewHourlyEmployee(String f_name, String m_name, String l_name, int dob_year, 13 int dob_month, int dob_day, String gender, 14 String employee_number){ 15 return new HourlyEmployee(f_name, m_name, l_name, dob_year, dob_month, dob_day, gender, 16 employee_number); 17 } 18 } // end EmployeeFactory class definition
Example 25.38: Model.java
1 package com.pulpfreepress.model; 2 3 import com.pulpfreepress.interfaces.*; 4 import java.util.*; 5 import java.io.*; 6 7 public class Model implements iModel { 8 9 private List employee_list = null; 10 private IEmployeeFactory employee_factory = null; 11 12 public Model(){ 13 employee_list = new LinkedList(); 14 employee_factory = new EmployeeFactory(); 15 } 16 17 public void createHourlyEmployee(String fname, String mname, String lname, 18 int dob_year, int dob_month, int dob_day, 19 String gender, String employee_number, 20 double hours_worked, double hourly_rate){ 21 22 PayInfo pay_info = new PayInfo(hours_worked, hourly_rate); 23 IEmployee employee = employee_factory.getNewHourlyEmployee(fname, mname, lname, dob_year, 24 dob_month, dob_day, gender, 25 employee_number); 26 employee.setPayInfo(pay_info); 27 employee_list.add(employee); 28 29 30 } 31 32 public void createSalariedEmployee(String fname, String mname, String lname, 33 int dob_year, int dob_month, int dob_day, 34 String gender, String employee_number, 35 double salary){ 36 37 PayInfo pay_info = new PayInfo(salary); 38 IEmployee employee = employee_factory.getNewSalariedEmployee(fname, mname, lname, dob_year, 39 dob_month, dob_day, gender, 40 employee_number); 41 employee.setPayInfo(pay_info); 42 employee_list.add(employee); 43 44 45 } 46 47 public void editEmployeeInfo(String fname, String mname, String lname, 48 int dob_year, int dob_month, int dob_day, 49 String gender, String employee_number){ 50 IEmployee employee = null; 51 for(Iterator it = employee_list.iterator(); it.hasNext();){ 52 employee = (IEmployee)it.next(); 53 if(employee.getEmployeeNumber().equals(employee_number)) break; 54 } 55 56 employee.setFirstName(fname); 57 employee.setMiddleName(mname); 58 employee.setLastName(lname); 59 employee.setBirthday(dob_year, dob_month, dob_day); 60 61 } 62 63 64 65 public String[] getAllEmployeesInfo(){ 66 String[] emp_info = new String[employee_list.size()]; 67 Iterator it = employee_list.iterator(); 68 for(int i = 0; it.hasNext();){ 69 emp_info[i++] = it.next().toString(); 70 } 71 return emp_info; 72 } 73 74 public String getEmployeeInfo(String employee_number){ 75 IEmployee employee = null; 76 for(Iterator it = employee_list.iterator(); it.hasNext();){ 77 employee = (IEmployee)it.next(); 78 if(employee.getEmployeeNumber().equals(employee_number)) break; 79 } 80 return employee.toString(); 81 } 82 83 public void sortEmployees(){ 84 Collections.sort(employee_list); 85 } 86 87 public void deleteEmployee(String employee_number){ 88 IEmployee employee = null; 89 for(Iterator it = employee_list.iterator(); it.hasNext();){ 90 employee = (IEmployee)it.next(); 91 if(employee.getEmployeeNumber().equals(employee_number)){ 92 employee_list.remove(employee); 93 break; 94 } 95 } 96 } 97 98 public void saveEmployeesToFile(File file){ 99 if(file == null){ 100 file = new File("employees.dat"); 101 } 102 103 FileOutputStream fos = null; 104 ObjectOutputStream oos = null; 105 106 try{ 107 fos = new FileOutputStream(file); 108 oos = new ObjectOutputStream(fos); 109 oos.writeObject(employee_list); 110 oos.close(); 111 }catch(Exception e){ 112 System.out.println("Problem saving employees file to disk!"); 113 } 114 } 115 116 public void loadEmployeesFromFile(File file){ 117 if(file == null){ 118 file = new File("employees.dat"); 119 } 120 121 FileInputStream fis = null; 122 ObjectInputStream ois = null; 123 124 try{ 125 fis = new FileInputStream(file); 126 ois = new ObjectInputStream(fis); 127 employee_list = (LinkedList)ois.readObject(); 128 ois.close(); 129 }catch(Exception e){ 130 System.out.println("Problem saving employees file to disk!"); 131 } 132 133 } 134 135 } // end Model class definition
Example 25.39: CommandFactory.java
1 package com.pulpfreepress.utils; 2 3 import com.pulpfreepress.commands.*; 4 import com.pulpfreepress.exceptions.*; 5 6 public class CommandFactory { 7 8 private static CommandFactory command_factory_instance = null; 9 private static CommandProperties command_properties = null; 10 11 static { 12 command_properties = CommandProperties.getInstance(); 13 } 14 15 private CommandFactory(){ } 16 17 public static CommandFactory getInstance(){ 18 if(command_factory_instance == null){ 19 command_factory_instance = new CommandFactory(); 20 } 21 return command_factory_instance; 22 } 23 24 public BaseCommand getCommand(String command_string) throws CommandNotFoundException { 25 BaseCommand command = null; 26 if(command_string == null){ 27 throw new CommandNotFoundException( command_string + " command class not found!"); 28 } else{ try{ 29 String command_classname = command_properties.getProperty(command_string); 30 Class command_class = Class.forName(command_classname); 31 Object command_object = command_class.newInstance(); 32 command = (BaseCommand) command_object; 33 }catch(Throwable t){ 34 t.printStackTrace(); 35 throw new CommandNotFoundException(t.toString(), t); 36 } 37 } // end else 38 return command; 39 } // end getCommand() method 40 } // end CommandFactory class definition
Example 25.40: CommandProperties.java
1 package com.pulpfreepress.utils; 2 3 import java.util.*; 4 import java.io.*; 5 6 public class CommandProperties extends Properties { 7 8 // class constants - default key strings 9 public static final String PROPERTIES_FILE = "PROPERTIES_FILE"; 10 public static final String NEWHOURLYEMPLOYEE_COMMAND = "NewHourlyEmployee"; 11 public static final String NEWSALARIEDEMPLOYEE_COMMAND = "NewSalariedEmployee"; 12 public static final String EXIT_COMMAND = "Exit"; 13 public static final String LIST_COMMAND = "List"; 14 public static final String SORT_COMMAND = "Sort"; 15 public static final String SAVE_COMMAND = "Save"; 16 public static final String EDITEMPLOYEE_COMMAND = "EditEmployee"; 17 public static final String DELETEEMPLOYEE_COMMAND = "DeleteEmployee"; 18 public static final String LOAD_COMMAND = "Load"; 19 20 // class constants - default value strings 21 private static final String PROPERTIES_FILE_VALUE = "Command.properties"; 22 private static final String NEWHOURLYEMPLOYEE_COMMAND_CLASSNAME= 23 "com.pulpfreepress.commands.NewHourlyEmployeeCommand"; 24 private static final String NEWSALARIEDEMPLOYEE_COMMAND_CLASSNAME= 25 "com.pulpfreepress.commands.NewSalariedEmployeeCommand"; 26 private static final String EXIT_COMMAND_CLASSNAME= 27 "com.pulpfreepress.commands.ApplicationExitCommand"; 28 private static final String LIST_COMMAND_CLASSNAME= 29 "com.pulpfreepress.commands.ListEmployeesCommand"; 30 private static final String SORT_COMMAND_CLASSNAME= 31 "com.pulpfreepress.commands.SortEmployeesCommand"; 32 private static final String SAVE_COMMAND_CLASSNAME= 33 "com.pulpfreepress.commands.SaveEmployeesCommand"; 34 private static final String EDITEMPLOYEE_COMMAND_CLASSNAME= 35 "com.pulpfreepress.commands.EditEmployeeCommand"; 36 private static final String DELETEEMPLOYEE_COMMAND_CLASSNAME= 37 "com.pulpfreepress.commands.DeleteEmployeeCommand"; 38 private static final String LOAD_COMMAND_CLASSNAME= 39 "com.pulpfreepress.commands.LoadEmployeesCommand"; 40 41 // class variables 42 private static CommandProperties _properties_object = null; 43 44 private CommandProperties( String properties_file ){ 45 try{ 46 FileInputStream fis = new FileInputStream(properties_file); 47 load(fis); 48 } catch(Exception e) { 49 System.out.println("Problem opening properties file!"); 50 System.out.println("Bootstrapping properties..."); 51 try{ 52 FileOutputStream fos = new FileOutputStream(PROPERTIES_FILE_VALUE); 53 setProperty(PROPERTIES_FILE, PROPERTIES_FILE_VALUE); 54 setProperty(NEWHOURLYEMPLOYEE_COMMAND, NEWHOURLYEMPLOYEE_COMMAND_CLASSNAME); 55 setProperty(NEWSALARIEDEMPLOYEE_COMMAND, NEWSALARIEDEMPLOYEE_COMMAND_CLASSNAME); 56 setProperty(EXIT_COMMAND, EXIT_COMMAND_CLASSNAME); 57 setProperty(LIST_COMMAND, LIST_COMMAND_CLASSNAME); 58 setProperty(SORT_COMMAND, SORT_COMMAND_CLASSNAME); 59 setProperty(SAVE_COMMAND, SAVE_COMMAND_CLASSNAME); 60 setProperty(EDITEMPLOYEE_COMMAND, EDITEMPLOYEE_COMMAND_CLASSNAME); 61 setProperty(DELETEEMPLOYEE_COMMAND, DELETEEMPLOYEE_COMMAND_CLASSNAME); 62 setProperty(LOAD_COMMAND, LOAD_COMMAND_CLASSNAME); 63 64 super.store(fos, "CommandProperties File - Edit Carefully"); 65 fos.close(); 66 }catch(Exception e2){ System.out.println("Uh ohh...Bigger problems exist!"); } 67 } 68 } 69 70 /************************************************************************************************** 71 * Private default constructor. Applications will get an instance via the getInstance() method. 72 * @see getInstance() 73 ***************************************************************************************************/ 74 private CommandProperties(){ 75 this(PROPERTIES_FILE_VALUE); 76 } 77 78 /************************************************************************************************** 79 * The store() method attempts to persist its properties collection. 80 ***************************************************************************************************/ 81 public void store(){ 82 try{ 83 FileOutputStream fos = new 84 FileOutputStream(getProperty(PROPERTIES_FILE)); 85 super.store(fos, "CommandProperties File"); 86 fos.close(); 87 }catch(Exception e){ System.out.println("Trouble storing properties!"); } 88 } 89 90 /************************************************************************************************** 91 * getInstance() returns a singleton instance of the CommandProperties object. 92 ****************************************************************************************************/ 93 public static CommandProperties getInstance(){ 94 if(_properties_object == null){ 95 _properties_object = new CommandProperties(); 96 } 97 return _properties_object; 98 } 99 } // end CommandProperties class definition
Example 25.41: View.java
1 package com.pulpfreepress.view; 2 3 import com.pulpfreepress.interfaces.*; 4 import java.awt.event.*; 5 import javax.swing.*; 6 import java.util.*; 7 import java.io.*; 8 9 public class View extends JFrame implements iView { 10 11 private JPanel panel1 = null; 12 private JTextArea textarea1 = null; 13 private int initial_frame_position_x = 50; 14 private int initial_frame_position_y = 50; 15 private int initial_frame_width = 400; 16 private int initial_frame_height = 400; 17 private int initial_textarea_rows = 20; 18 private int initial_textarea_columns = 20; 19 private EditEmployeeDialog edit_employee_dialog = null; 20 private String[] current_employee_list = null; 21 private JMenuItem delete_employee_menuitem = null; 22 private JMenuItem edit_employee_menuitem = null; 23 24 public View(ActionListener al){ 25 this.setupGUI(al); 26 } 27 28 public void displayEmployeeInfo(String[] employees_info){ 29 textarea1.setText(""); 30 for(int i = 0; i<employees_info.length; i++){ 31 textarea1.append(employees_info[i] + "\n"); 32 } 33 current_employee_list = employees_info; 34 if(current_employee_list.length == 0){ 35 this.delete_employee_menuitem.setEnabled(false); 36 this.edit_employee_menuitem.setEnabled(false); 37 }else{ 38 this.delete_employee_menuitem.setEnabled(true); 39 this.edit_employee_menuitem.setEnabled(true); 40 } 41 } 42 43 public String[] getNewHourlyEmployeeInfo(){ 44 edit_employee_dialog.setTitle("New Hourly Employee"); 45 edit_employee_dialog.clearTextFields(); 46 edit_employee_dialog.showHourlyInfo(); 47 edit_employee_dialog.hideSalaryInfo(); 48 edit_employee_dialog.enableEmployeeNumberTextField(); 49 edit_employee_dialog.setVisible(true); 50 51 return edit_employee_dialog.getEmployeeInfo(); 52 } 53 54 55 public String[] getNewSalariedEmployeeInfo(){ 56 edit_employee_dialog.setTitle("New Salaried Employee"); 57 edit_employee_dialog.clearTextFields(); 58 edit_employee_dialog.showSalaryInfo(); 59 edit_employee_dialog.hideHourlyInfo(); 60 edit_employee_dialog.enableEmployeeNumberTextField(); 61 edit_employee_dialog.setVisible(true); 62 return edit_employee_dialog.getEmployeeInfo(); 63 } 64 65 public String[] getEditEmployeeInfo(){ 66 edit_employee_dialog.setTitle("Edit Employee Information"); 67 edit_employee_dialog.clearTextFields(); 68 edit_employee_dialog.hideHourlyAndSalaryInfo(); 69 edit_employee_dialog.disableEmployeeNumberTextField(); 70 String emp_info_string = current_employee_list[getSelectedLineNumber()]; 71 String[] emp_string_array = employeeInfoStringToStringArray(emp_info_string); 72 edit_employee_dialog.populateTextFields(emp_string_array); 73 edit_employee_dialog.setVisible(true); 74 return edit_employee_dialog.getEditedEmployeeInfo(); 75 76 } 77 78 public String getDeleteEmployeeInfo(){ 79 String emp_number = ""; 80 if(current_employee_list != null){ 81 String emp_info_string = current_employee_list[getSelectedLineNumber()]; 82 String[] emp_string_array = employeeInfoStringToStringArray(emp_info_string); 83 if(emp_string_array != null){ 84 emp_number = emp_string_array[7]; 85 } 86 } 87 return emp_number; 88 } 89 90 private JMenuBar setupMenuBar(ActionListener al){ 91 /*** create menubar ***/ 92 JMenuBar menubar = new JMenuBar(); 93 94 /*** create file menu and menu items ***/ 95 JMenu file_menu = new JMenu("File"); 96 JMenuItem file_loadEmployees_menuitem = new JMenuItem("Load Employees..."); 97 file_loadEmployees_menuitem.setActionCommand("Load"); 98 file_loadEmployees_menuitem.addActionListener(al); 99 JMenuItem file_saveEmployees_menuitem = new JMenuItem("Save Employees..."); 100 file_saveEmployees_menuitem.setActionCommand("Save"); 101 file_saveEmployees_menuitem.addActionListener(al); 102 JMenuItem file_exit_menuitem = new JMenuItem("Exit"); 103 file_exit_menuitem.addActionListener(al); 104 105 file_menu.add(file_loadEmployees_menuitem); 106 file_menu.add(file_saveEmployees_menuitem); 107 file_menu.add(file_exit_menuitem); 108 109 menubar.add(file_menu); 110 111 /*** create employees menu and menu items ***/ 112 JMenu employees_menu = new JMenu("Employees"); 113 JMenuItem employees_list_menuitem = new JMenuItem("List"); 114 employees_list_menuitem.addActionListener(al); 115 JMenuItem employees_sort_menuitem = new JMenuItem("Sort"); 116 employees_sort_menuitem.addActionListener(al); 117 JMenuItem employees_newHourlyEmployee_menuitem = new JMenuItem("New Hourly..."); 118 employees_newHourlyEmployee_menuitem.setActionCommand("NewHourlyEmployee"); 119 employees_newHourlyEmployee_menuitem.addActionListener(al); 120 JMenuItem employees_newSalariedEmployee_menuitem = new JMenuItem("New Salaried..."); 121 employees_newSalariedEmployee_menuitem.setActionCommand("NewSalariedEmployee"); 122 employees_newSalariedEmployee_menuitem.addActionListener(al); 123 JMenuItem employees_editEmployee_menuitem = new JMenuItem("Edit Employee..."); 124 employees_editEmployee_menuitem.setActionCommand("EditEmployee"); 125 employees_editEmployee_menuitem.addActionListener(al); 126 JMenuItem employees_deleteEmployee_menuitem = new JMenuItem("Delete Employee"); 127 employees_deleteEmployee_menuitem.setActionCommand("DeleteEmployee"); 128 employees_deleteEmployee_menuitem.addActionListener(al); 129 this.delete_employee_menuitem = employees_deleteEmployee_menuitem; 130 this.edit_employee_menuitem = employees_editEmployee_menuitem; 131 if(current_employee_list == null){ 132 this.delete_employee_menuitem.setEnabled(false); 133 this.edit_employee_menuitem.setEnabled(false); 134 } 135 136 employees_menu.add(employees_list_menuitem); 137 employees_menu.add(employees_sort_menuitem); 138 employees_menu.add(employees_newHourlyEmployee_menuitem); 139 employees_menu.add(employees_newSalariedEmployee_menuitem); 140 employees_menu.add(employees_editEmployee_menuitem); 141 employees_menu.add(employees_deleteEmployee_menuitem); 142 143 menubar.add(employees_menu); 144 145 return menubar; 146 } 147 148 private void setupGUI(ActionListener al){ 149 edit_employee_dialog = new EditEmployeeDialog(this); 150 textarea1 = new JTextArea(initial_textarea_rows, initial_textarea_columns); 151 JScrollPane scrollpane = new JScrollPane(textarea1); 152 this.getContentPane().add(scrollpane); 153 this.setJMenuBar(this.setupMenuBar(al)); 154 this.setSize(initial_frame_height, initial_frame_width); 155 this.setLocation(initial_frame_position_x, initial_frame_position_y); 156 this.pack(); 157 this.show(); 158 } 159 160 private int getSelectedLineNumber(){ 161 int caret_position = textarea1.getCaretPosition(); 162 int line_number = 0; 163 int string_length = 0; 164 try{ 165 StringTokenizer st = new StringTokenizer(textarea1.getText(), "\n"); 166 while(caret_position >= (string_length += st.nextToken().length()) ){ 167 line_number++; 168 } 169 }catch(NoSuchElementException ignored){ } 170 171 if(line_number >= current_employee_list.length){ 172 line_number = current_employee_list.length-1; 173 } 174 return line_number; 175 } 176 177 private String[] employeeInfoStringToStringArray(String emp_string){ 178 StringTokenizer st = new StringTokenizer(emp_string); 179 String fname = st.nextToken(); 180 String mname = st.nextToken(); 181 String lname = st.nextToken(); 182 String gender = st.nextToken(); 183 String bday = st.nextToken(); 184 String emp_no = st.nextToken(); 185 186 StringTokenizer st2 = new StringTokenizer(bday,"/"); 187 String day = st2.nextToken(); 188 String month = st2.nextToken(); 189 String year = st2.nextToken(); 190 191 String[] string_array = {fname, mname, lname, gender, year, month, day, emp_no}; 192 return string_array; 193 } 194 195 public File getSaveFile(){ 196 File file = null; 197 JFileChooser file_chooser = new JFileChooser(); 198 int result = file_chooser.showSaveDialog(this); 199 switch(result){ 200 case JFileChooser.APPROVE_OPTION: { file = file_chooser.getSelectedFile(); 201 break; 202 } 203 case JFileChooser.CANCEL_OPTION: break; 204 205 } // end switch 206 return file; 207 } 208 209 public File getLoadFile(){ 210 File file = null; 211 JFileChooser file_chooser = new JFileChooser(); 212 int result = file_chooser.showOpenDialog(this); 213 switch(result){ 214 case JFileChooser.APPROVE_OPTION: { file = file_chooser.getSelectedFile(); 215 break; 216 } 217 case JFileChooser.CANCEL_OPTION: break; 218 219 220 } // end switch 221 return file; 222 } 223 } // end View class definition
Example 25.42: EditEmployeeDialog.java
1 package com.pulpfreepress.view; 2 3 import javax.swing.*; 4 import java.awt.*; 5 import java.awt.event.*; 6 import java.util.regex.*; 7 8 public class EditEmployeeDialog extends JDialog implements ActionListener { 9 private JLabel label1 = null; 10 private JLabel label2 = null; 11 private JLabel label3 = null; 12 private JLabel label4 = null; 13 private JLabel label5 = null; 14 private JLabel label6 = null; 15 private JLabel label7 = null; 16 private JLabel label8 = null; 17 private JLabel label9 = null; 18 private JLabel label10 = null; 19 private JLabel label11 = null; 20 21 private JTextField textfield1 = null; 22 private JTextField textfield2 = null; 23 private JTextField textfield3 = null; 24 private JTextField textfield4 = null; 25 private JTextField textfield5 = null; 26 private JTextField textfield6 = null; 27 private JTextField textfield7 = null; 28 private JTextField textfield8 = null; 29 private JTextField textfield9 = null; 30 private JTextField textfield10 = null; 31 private JTextField textfield11 = null; 32 33 34 public EditEmployeeDialog(JFrame frame){ 35 super(frame, "Employee Information Dialog", true); // modal dialog 36 this.setupGUI(); 37 this.setVisible(false); 38 } 39 40 public void actionPerformed(ActionEvent ae){ 41 if(ae.getActionCommand().equals("Submit")){ 42 boolean bool_val = verifyFieldValues(); 43 if(bool_val){ 44 this.setVisible(false); 45 } 46 } 47 } 48 49 private void setupGUI(){ 50 label1 = new JLabel("First Name:"); 51 label2 = new JLabel("Middle Name:"); 52 label3 = new JLabel("Last Name:"); 53 label4 = new JLabel("Gender:"); 54 label5 = new JLabel("Birth Year:"); 55 label6 = new JLabel("Birth Month:"); 56 label7 = new JLabel("Birth Day:"); 57 label8 = new JLabel("Employee Number:"); 58 label9 = new JLabel("Hours Worked:"); 59 label10 = new JLabel("Hourly Rate"); 60 label11 = new JLabel("Salary:"); 61 62 textfield1 = new JTextField(20); 63 textfield2 = new JTextField(20); 64 textfield3 = new JTextField(20); 65 textfield4 = new JTextField(20); 66 textfield5 = new JTextField(20); 67 textfield6 = new JTextField(20); 68 textfield7 = new JTextField(20); 69 textfield8 = new JTextField(20); 70 textfield9 = new JTextField(20); 71 textfield10 = new JTextField(20); 72 textfield11 = new JTextField(20); 73 74 75 JButton button1 = new JButton("Submit"); 76 button1.addActionListener(this); 77 78 this.getContentPane().setLayout(new GridLayout(12, 2, 0, 0)); 79 this.getContentPane().add(label1); 80 this.getContentPane().add(textfield1); 81 this.getContentPane().add(label2); 82 this.getContentPane().add(textfield2); 83 this.getContentPane().add(label3); 84 this.getContentPane().add(textfield3); 85 this.getContentPane().add(label4); 86 this.getContentPane().add(textfield4); 87 this.getContentPane().add(label5); 88 this.getContentPane().add(textfield5); 89 this.getContentPane().add(label6); 90 this.getContentPane().add(textfield6); 91 this.getContentPane().add(label7); 92 this.getContentPane().add(textfield7); 93 this.getContentPane().add(label8); 94 this.getContentPane().add(textfield8); 95 this.getContentPane().add(label9); 96 this.getContentPane().add(textfield9); 97 this.getContentPane().add(label10); 98 this.getContentPane().add(textfield10); 99 this.getContentPane().add(label11); 100 this.getContentPane().add(textfield11); 101 this.getContentPane().add(new JPanel()); 102 this.getContentPane().add(new JPanel().add(button1)); 103 104 this.pack(); 105 } 106 107 public String[] getEmployeeInfo(){ 108 String[] emp_info = new String[11]; 109 110 emp_info[0] = textfield1.getText(); 111 emp_info[1] = textfield2.getText(); 112 emp_info[2] = textfield3.getText(); 113 emp_info[3] = textfield4.getText(); 114 emp_info[4] = textfield5.getText(); 115 emp_info[5] = textfield6.getText(); 116 emp_info[6] = textfield7.getText(); 117 emp_info[7] = textfield8.getText(); 118 emp_info[8] = textfield9.getText(); 119 emp_info[9] = textfield10.getText(); 120 emp_info[10] = textfield11.getText(); 121 122 return emp_info; 123 124 } 125 126 public void hideSalaryInfo(){ 127 label11.setVisible(false); 128 textfield11.setVisible(false); 129 } 130 131 public void showSalaryInfo(){ 132 label11.setVisible(true); 133 textfield11.setVisible(true); 134 } 135 136 public void hideHourlyInfo(){ 137 label9.setVisible(false); 138 textfield9.setVisible(false); 139 label10.setVisible(false); 140 textfield10.setVisible(false); 141 } 142 143 public void showHourlyInfo(){ 144 label9.setVisible(true); 145 textfield9.setVisible(true); 146 label10.setVisible(true); 147 textfield10.setVisible(true); 148 } 149 150 public void clearTextFields(){ 151 textfield1.setText(""); 152 textfield2.setText(""); 153 textfield3.setText(""); 154 textfield4.setText(""); 155 textfield5.setText("yyyy"); 156 textfield6.setText("mm"); 157 textfield7.setText("dd"); 158 textfield8.setText(""); 159 textfield9.setText("0.00"); 160 textfield10.setText("0.00"); 161 textfield11.setText("0.00"); 162 } 163 164 public void hideHourlyAndSalaryInfo(){ 165 hideSalaryInfo(); 166 hideHourlyInfo(); 167 } 168 169 public void disableEmployeeNumberTextField(){ 170 textfield8.setEnabled(false); 171 } 172 173 public void enableEmployeeNumberTextField(){ 174 textfield8.setEnabled(true); 175 } 176 177 178 public void populateTextFields(String[] emp_info){ 179 textfield1.setText(emp_info[0]); 180 textfield2.setText(emp_info[1]); 181 textfield3.setText(emp_info[2]); 182 textfield4.setText(emp_info[3]); 183 textfield5.setText(emp_info[4]); 184 if(Integer.parseInt(emp_info[5]) < 10) { 185 textfield6.setText("0" + emp_info[5]); 186 } else { 187 textfield6.setText(emp_info[5]); 188 } 189 if(Integer.parseInt(emp_info[6]) < 10) { 190 textfield7.setText("0" + emp_info[6]); 191 } else { 192 textfield7.setText(emp_info[6]); 193 } 194 textfield8.setText(emp_info[7]); 195 196 } 197 198 public String[] getEditedEmployeeInfo(){ 199 String[] emp_info = new String[8]; 200 201 emp_info[0] = textfield1.getText(); 202 emp_info[1] = textfield2.getText(); 203 emp_info[2] = textfield3.getText(); 204 emp_info[3] = textfield4.getText(); 205 emp_info[4] = textfield5.getText(); 206 emp_info[5] = textfield6.getText(); 207 emp_info[6] = textfield7.getText(); 208 emp_info[7] = textfield8.getText(); 209 210 211 return emp_info; 212 } 213 214 private boolean verifyFieldValues(){ 215 boolean ok = true; 216 217 if(Pattern.matches("[A-Za-z]{1,64}", textfield1.getText())){ 218 // do nothing 219 } else { 220 textfield1.setText("-----Invalid First Name-----"); 221 ok = false; 222 } 223 224 if(Pattern.matches("[A-Za-z]{1,64}", textfield2.getText())){ 225 // do nothing 226 } else { 227 textfield2.setText("-----Invalid Middle Name-----"); 228 ok = false; 229 } 230 231 if(Pattern.matches("[A-Za-z]{1,64}", textfield3.getText())){ 232 // do nothing 233 } else { 234 textfield3.setText("-----Invalid Last Name-----"); 235 ok = false; 236 } 237 238 239 if(textfield4.getText().equals("Male") || textfield4.getText().equals("Female")){ 240 // do nothing 241 } else { 242 textfield4.setText("-----Male or Female-----"); 243 ok = false; 244 } 245 246 if(Pattern.matches("[1|2]{1}[0-9]{3}", textfield5.getText())){ 247 // do nothing 248 } else { 249 textfield5.setText("----invalid Birth Year----"); 250 ok = false; 251 } 252 253 if(Pattern.matches("[0|1]{1}[0-9]{1}", textfield6.getText())){ 254 if((Integer.parseInt(textfield6.getText()) < 1) || 255 (Integer.parseInt(textfield6.getText()) > 12) ){ 256 textfield6.setText("----invalid Birth Month----"); 257 ok = false; 258 } 259 260 } else { 261 textfield6.setText("----invalid Birth Month----"); 262 ok = false; 263 } 264 265 if(Pattern.matches("[0-3]{1}[0-9]{1}", textfield7.getText())){ 266 if((Integer.parseInt(textfield7.getText()) < 1) || 267 (Integer.parseInt(textfield7.getText()) > 31) ){ 268 textfield7.setText("----invalid Birth Day----"); 269 ok = false; 270 } 271 } else { 272 textfield7.setText("----invalid Birth Day----"); 273 ok = false; 274 } 275 276 if(Pattern.matches("[0-9]{6}", textfield8.getText())){ 277 // do nothing 278 } else { 279 textfield8.setText("----invalid Employee Number----"); 280 ok = false; 281 } 282 283 if(Pattern.matches("[0-9]{1,2}[.]{1}[0-9]{2}", textfield9.getText())){ 284 // do nothing 285 } else { 286 textfield9.setText("----invalid Hours Worked----"); 287 ok = false; 288 } 289 290 if(Pattern.matches("[0-9]{1,3}[.]{1}[0-9]{2}", textfield10.getText())){ 291 // do nothing 292 } else { 293 textfield10.setText("----invalid Hourly Rate----"); 294 ok = false; 295 } 296 297 if(Pattern.matches("[0-9]{1,7}[.]{1}[0-9]{2}", textfield11.getText())){ 298 // do nothing 299 } else { 300 textfield11.setText("----invalid Salary----"); 301 ok = false; 302 } 303 304 return ok; 305 } 306 307 } // end EditEmployeeDialog class definition
Figure 25-5 shows several screen-shots of the user interface of this application in action.
Figure 25-5: Interacting with the Employee Management Application