Solution
A new method service() was added in the Staff class. This method displays the employees with their years of service in descending order.
public void service(){ // display the employees with their years of service // in decreasing order of their days of service; String IDs[]; int service[]; int i, index; int servicetemp; String IDtemp; Employee JohnDoe; IDs = new String[this.Employees.size()]; service = new int[this.Employees.size()]; //copying the IDs and absences for (i=0;i<this.Employees.size();i++) { JohnDoe=(Employee)this.Employees.elementAt(i); IDs[i]=JohnDoe.getID(); service[i]=JohnDoe.getYearsService(); } // sorting the service array for (i=0;i<this.Employees.size();i++) { index=this.getSmallest(service,i); IDtemp=IDs[index]; servicetemp=service[index]; IDs[index]=IDs[i]; service[index]=service[i]; IDs[i]=IDtemp; service[i]=servicetemp; } //displaying the employees and their years of service for (i=0;i<this.Employees.size();i++) { index=this.search(IDs[i]); JohnDoe=(Employee)this.Employees.elementAt(index); System.out.print(service[i]+" years : "); JohnDoe.displayID(); } }The following program is an updated version of exercise 3.
import java.util.*; public class exer5 { /* Displays a menu and reads a choice before calling appropriate method */ public static void main(String args[]) { /* Program statements go here. */ Staff myStaff; String theID; double theSalary; String theName, theDate; int theService, theVacation, theAbsence; Integer choice; boolean endOfProgram=false; boolean done; myStaff = new Staff(); while (!endOfProgram) { System.out.println(); System.out.println("1 - Add employees"); System.out.println("2 - Display all employees"); System.out.println("3 - Display Given employee"); System.out.println("4 - Display employees with salary higher bound"); System.out.println("5 - Display employees with absence"); System.out.println("6 - Display employees with earned vacation"); System.out.println("7 - Display employees with service"); System.out.println("8 - Remove employees"); System.out.println("9 - Change employees"); System.out.println("0 - Quit"); do { System.out.print("Please enter your choice (0..9):"); choice = Keyboard.in.readInteger(); } while (choice==null || choice.intValue() < 0 || choice.intValue() >9); switch(choice.intValue()){ case 1: System.out.print("Please enter employee ID to be added:"); theID = Keyboard.in.readString(); System.out.print("Please enter employee name:"); theName = Keyboard.in.readString(); System.out.print("Please enter date of birth (mm/dd/yyyy):"); theDate = Keyboard.in.readString(); System.out.print("Please enter a salary:"); theSalary=Keyboard.in.readFloat().doubleValue(); System.out.print("Please enter Years of service:"); theService = Keyboard.in.readInteger().intValue(); System.out.print("Please enter Days of vacation earned:"); theVacation = Keyboard.in.readInteger().intValue(); System.out.print("Please enter Days of absence:"); theAbsence = Keyboard.in.readInteger().intValue(); done=myStaff.add(new Employee(theID, theName, new Date(theDate), theService, theVacation, theSalary, theAbsence)); if (done) System.out.println("new employee added\n"); else System.out.println("Employee already exists!\n"); break; case 2: System.out.println("All Employees"); myStaff.display(); System.out.println(); break; case 3: System.out.print("Please enter employee ID:"); theID = Keyboard.in.readString(); myStaff.display(theID); System.out.println(); break; case 4: System.out.print("Please enter a salary as upper bound:"); theSalary=Keyboard.in.readFloat().doubleValue(); System.out.println("Employees with salary higher than $"+theSalary); myStaff.topSalary(theSalary); System.out.println(); break; case 5: System.out.println("Employees ordered by days of absence (reverse order)"); myStaff.absence(); System.out.println(); break; case 6: System.out.println("Employees ordered by earned days of vacation"); myStaff.vacation(); System.out.println(); break; case 7: System.out.println("Employees ordered by years of service"); myStaff.service(); System.out.println(); break; case 8: System.out.print("Please enter employee ID to be removed:"); theID = Keyboard.in.readString(); done=myStaff.remove(theID); if (done) System.out.println("Employee removed\n"); else System.out.println("Employee not found!\n"); break; case 9: System.out.print("Please enter employee ID to be updated:"); theID = Keyboard.in.readString(); System.out.print("Please enter a salary:"); theSalary=Keyboard.in.readFloat().doubleValue(); System.out.print("Please enter Years of service:"); theService = Keyboard.in.readInteger().intValue(); System.out.print("Please enter Days of vacation earned:"); theVacation = Keyboard.in.readInteger().intValue(); System.out.print("Please enter Days of absence:"); theAbsence = Keyboard.in.readInteger().intValue(); done=myStaff.change(theID, theService, theVacation, theAbsence, theSalary); if (done) System.out.println("Employee updated\n"); else System.out.println("Employee not found!\n"); break; case 0: System.out.println("Thank you! See you later."); } endOfProgram=(choice.intValue()==0); } } }