Solution
Two new methods getName() and getDateBirth() were added in the Employee class. These methods return the name and the birthdate of the object Employee respectively.
public String getName(){
// returns the name of an employee;
return this.name;
}
public String getDateBirth(){
// returns the date of birth of an employee in string mm/dd/yyyy;
return (this.birthdate.getMonth()+1) + "/" +
this.birthdate.getDate() + "/" +
(1900+this.birthdate.getYear());
}
The class StaffFile is a subclass of class Staff. A program using
StaffFile should first create an object StaffFile by giving a file
name. Two methods are used to read from and write to the
file. writeToFile() simply writes the employees from the vector to the
file. readFromFile() initializes the vector to an empty vector and
reads the employee data from the file if it exists. It returns false
if the file does not exist and true otherwise.
import java.io.*;
public class StaffFile extends Staff {
private String FileName;
public StaffFile(String name) {
this.FileName=name;
}
public boolean readFromFile() throws Exception {
File aFile;
FileInputStream inputStream;
InputStreamReader aReader;
BufferedReader aBufferedReader;
String aString;
String theID, theName;
Date theDate;
double theSalary;
int theService, theVacation, theAbsence;
Double aDouble;
Integer anInteger;
this.Employees = new Vector();
aFile = new File(this.FileName);
if (!aFile.exists()) return false;
inputStream = new FileInputStream(aFile);
aReader = new InputStreamReader(inputStream);
aBufferedReader = new BufferedReader(aReader);
aString = aBufferedReader.readLine();
while (aString!=null) {
theID=aString;
aString = aBufferedReader.readLine();
theName=aString;
aString = aBufferedReader.readLine();
theDate=new Date(aString);
aString = aBufferedReader.readLine();
aDouble = new Double(aString);
theSalary=aDouble.doubleValue();
aString = aBufferedReader.readLine();
anInteger= new Integer(aString);
theService=anInteger.intValue();
aString = aBufferedReader.readLine();
anInteger= new Integer(aString);
theVacation=anInteger.intValue();
aString = aBufferedReader.readLine();
anInteger= new Integer(aString);
theAbsence=anInteger.intValue();
this.Employees.add(new Employee(theID, theName,
theDate, theService,
theVacation, theSalary,
theAbsence));
aString = aBufferedReader.readLine();
}
aBufferedReader.close();
return true;
}
public void writeToFile() throws Exception {
int i;
Employee JohnDoe;
File aFile;
FileOutputStream outputStream;
PrintStream aPrintStream;
aFile = new File(this.FileName);
outputStream = new FileOutputStream(aFile);
aPrintStream = new PrintStream(outputStream);
for (i=0;i<this.Employees.size();i++) {
JohnDoe=(Employee)this.Employees.elementAt(i);
aPrintStream.println(JohnDoe.getID());
aPrintStream.println(JohnDoe.getName());
aPrintStream.println(JohnDoe.getDateBirth());
aPrintStream.println(JohnDoe.getSalary());
aPrintStream.println(JohnDoe.getYearService());
aPrintStream.println(JohnDoe.getDaysVacation());
aPrintStream.println(JohnDoe.getDaysAbsence());
}
aPrintStream.close();
}
}