Source of EmployeeRecords.java


  1: //EmployeeRecords.java

  3: import java.util.Scanner;
  4: import java.util.ArrayList;
  5: import java.util.Collections;

  7: public class EmployeeRecords
  8: {

 10:     public static void main(String[] args)
 11:     {
 12:         Scanner scnr = new Scanner(System.in);
 13:         // Stores all employee data
 14:         ArrayList<EmployeeData> emplList = new ArrayList<EmployeeData>();
 15:         EmployeeData emplData; // Stores info for one employee
 16:         String userCommand;    // User defined add/print/quit command
 17:         String emplFirstName;  // User defined employee first name
 18:         String emplLastName;   // User defined employee last name
 19:         Integer emplID;        // User defined employee ID
 20:         Integer deptNum;       // User defined employee Dept
 21:         int i;                 // Loop counter

 23:         do
 24:         {
 25:             // Prompt user for input
 26:             System.out.print
 27:             (
 28:                 "\nEnter command 'a' to add new employee, "
 29:                 + "\n'p' to print all employees, 'q' to quit: "
 30:             );
 31:             userCommand = scnr.next();

 33:             // Add new employee entry
 34:             if (userCommand.equals("a"))
 35:             {
 36:                 System.out.print("First Name: ");
 37:                 emplFirstName = scnr.next();
 38:                 System.out.print("Last Name: ");
 39:                 emplLastName = scnr.next();
 40:                 System.out.print("ID: ");
 41:                 emplID = scnr.nextInt();
 42:                 System.out.print("Department Number: ");
 43:                 deptNum = scnr.nextInt();
 44:                 emplData = new EmployeeData
 45:                 (
 46:                     emplFirstName,
 47:                     emplLastName,
 48:                     emplID,
 49:                     deptNum
 50:                 );
 51:                 emplList.add(emplData);
 52:             }
 53:             // Print all entries
 54:             else if (userCommand.equals("p"))
 55:             {

 57:                 // Sort employees by department number first
 58:                 // and name second
 59:                 Collections.sort(emplList);

 61:                 System.out.println("");
 62:                 System.out.println("Employees: ");
 63:                 // Access employee records
 64:                 for (i = 0; i < emplList.size(); ++i)
 65:                 {
 66:                     System.out.println(emplList.get(i).toString());
 67:                 }
 68:                 System.out.println("");
 69:             }
 70:         }
 71:         while (!userCommand.equals("q"));
 72:     }
 73: }