Solution
A new method getID() was added in the Employee class. This method returns the ID of the object Employee.
public String getID() {
// returns the ID of an employee;
return this.id;
}
Also, a method add(Employee anEmployee) was also defined in the class
Staff (below) to add an employee to the vector.
import java.util.*;
public class Staff {
protected Vector Employees;
/*
Program description.
*/
public Staff() {
this.Employees = new Vector();
}
public void display(){
// display all employees;
int i;
Employee JohnDoe;
for (i=0;i<this.Employees.size();i++) {
JohnDoe=(Employee)this.Employees.elementAt(i);
JohnDoe.display();
}
}
public void display(String ID) {
// display the employee with the given ID;
Employee JohnDoe;
int index=this.search(ID);
if (index!=-1) {
JohnDoe=(Employee)this.Employees.elementAt(index);
JohnDoe.display();
}
}
public void topSalary(double salary) {
// display all the employees and their salaries that
// have a salary higher than the given salary;
int i;
Employee JohnDoe;
for (i=0;i<this.Employees.size();i++) {
JohnDoe=(Employee)this.Employees.elementAt(i);
if (salary<JohnDoe.getSalary()) JohnDoe.display();
}
}
public void absence(){
// display the employees with their days of absence
// in decreasing order of their days of absence;
String IDs[];
int absences[];
int i, index;
int absencetemp;
String IDtemp;
Employee JohnDoe;
IDs = new String[this.Employees.size()];
absences = 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();
absences[i]=JohnDoe.getDaysAbsence();
}
// sorting the absences
for (i=0;i<this.Employees.size();i++) {
index=this.getSmallest(absences,i);
IDtemp=IDs[index];
absencetemp=absences[index];
IDs[index]=IDs[i];
absences[index]=absences[i];
IDs[i]=IDtemp;
absences[i]=absencetemp;
}
//displaying the employees and their absences
for (i=0;i<this.Employees.size();i++) {
index=this.search(IDs[i]);
JohnDoe=(Employee)this.Employees.elementAt(index);
System.out.print(absences[i]+" days : ");
JohnDoe.displayID();
}
}
public void vacation(){
// display the employees and their vacation earned in
// increasing order of their days of vacation earned;
String IDs[];
int vacations[];
int i, index;
int vacationtemp;
String IDtemp;
Employee JohnDoe;
IDs = new String[this.Employees.size()];
vacations = new int[this.Employees.size()];
//copying the IDs and vacations
for (i=0;i<this.Employees.size();i++) {
JohnDoe=(Employee)this.Employees.elementAt(i);
IDs[i]=JohnDoe.getID();
vacations[i]=JohnDoe.getDaysVacation();
}
// sorting the vacations
for (i=0;i<this.Employees.size();i++) {
index=this.getLargest(vacations,i);
IDtemp=IDs[index];
vacationtemp=vacations[index];
IDs[index]=IDs[i];
vacations[index]=vacations[i];
IDs[i]=IDtemp;
vacations[i]=vacationtemp;
}
//displaying the employees and their absences
for (i=0;i<this.Employees.size();i++) {
index=this.search(IDs[i]);
JohnDoe=(Employee)this.Employees.elementAt(index);
System.out.print(vacations[i]+" days : ");
JohnDoe.displayID();
}
}
private int getSmallest(int anArray[], int start){
// Return the index of the smallest element
// of the given array whose index is greater
// than or equal to the given start index.
int smallestIndex;
int index;
smallestIndex = start;
for (index = start + 1; index < anArray.length; index++)
if (anArray[index] < anArray[smallestIndex])
smallestIndex = index;
return smallestIndex;
}
private int getLargest(int anArray[], int start){
// Return the index of the largest element
// of the given array whose index is greater
// than or equal to the given start index.
int largestIndex;
int index;
largestIndex = start;
for (index = start + 1; index < anArray.length; index++)
if (anArray[index] > anArray[largestIndex])
largestIndex = index;
return largestIndex;
}
public boolean add(Employee somebody) {
// add an employee in the Staff vector if the id is unknown
// and return true. Returns false if the id already exists.
int index=search(somebody.getID());
if (index==-1) {
this.Employees.addElement(somebody);
return true;
} else return false;
}
private int search(String anID) {
// searches sequentially for the index of employee with
// given ID.
int i=0;
Employee JohnDoe;
boolean found=false;
while (!found && i<this.Employees.size()) {
JohnDoe=(Employee)this.Employees.elementAt(i);
if (JohnDoe.getID().equals(anID))
found=true;
else i=i+1;
}
if (!found) return -1;
else return i;
}
public boolean remove(String ID) {
// remove the employee with the given ID;
int index=search(ID);
if (index==-1) return false;
else this.Employees.removeElementAt(index);
return true;
}
public boolean change(String ID, int servY, int vacD, int absD,
double salary) {
// change the state of the Employee with the given ID.
Employee JohnDoe;
int index=search(ID);
if (index==-1) return false;
else {
JohnDoe=(Employee)this.Employees.elementAt(index);
JohnDoe.setSalary(salary);
JohnDoe.setYearService(servY);
JohnDoe.setDaysVacation(vacD);
JohnDoe.setDaysAbsence(absD);
this.Employees.setElementAt(JohnDoe,index);
return true;
}
}
}