Employee
Home ] Up ] [ Employee ] EmployeeDataLoader ] EmployeeDataReader ] EmployeeTree ]

 

 

First, let's create a class that represents an employee, whose data we'll be saving in a random access file:

package randomAccess;

import java.io.IOException;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.RandomAccessFile;

/**
*   Class to represent an employee.
*
*   @author Bryan J. Higgs, 31 January, 2001
*/
public class Employee
{
    public static final int RECORD_SIZE = 4     // for the employeeNo
                                        + 80    // for the name (40 characters)
                                        + 8     // for the salary
                                        + 4;    // for the managedBy

    public static final int NO_MANAGER = -1;    // Means no managedBy

    /**
    *   Constructor (used for constructing an object and
    *   populating it using the read method),
    */
    public Employee()
    {
    }

    /**
    *   Constructor.
    */
    public Employee(int employeeID, String name, double salary, int managedBy)
    {
        m_employeeID = employeeID;
        m_name = name;
        m_salary = salary;
        m_managedBy = managedBy;
    }

    /**
    *   Returns the employee's ID.
    */
    public int getID()
    {
        return m_employeeID;
    }

    /**
    *   Returns the employee's name.
    */
    public String getName()
    {
        return m_name;
    }

    /**
    *   Returns the employee's salary.
    */
    public double getSalary()
    {
        return m_salary;
    }

    /**
    *   Returns the employee's manager's ID.
    */
    public int getManagedBy()
    {
        return m_managedBy;
    }

    /**
    *   Prints out the employee's information.
    */
    public void print()
    {
        PrintStream out = System.out;
        out.println("Employee ID: " + m_employeeID);
        out.println("       Name: " + m_name);
        out.println("     Salary: $" + m_salary);
        out.println(" Managed by: " + m_managedBy);
    }

    /**
    *   Writes employee data to the specified file.
    */
    public void write(RandomAccessFile file)
        throws IOException
    {
        file.writeInt(m_employeeID);
        writeName(file);
        file.writeDouble(m_salary);
        file.writeInt(m_managedBy);
    }

    /**
    *   Reads employee data from the specified file.
    */
    public void read(RandomAccessFile file)
        throws IOException
    {
        m_employeeID = file.readInt();
        m_name = readName(file);
        m_salary = file.readDouble();
        m_managedBy = file.readInt();
    }

    //// Private methods ////

    /**
    *   Writes the employee's name to the specified file.
    *   The name is truncated or padded to MAX_NAME_SIZE characters.
    */
    private void writeName(RandomAccessFile file)
        throws IOException
    {
        StringBuffer buf = new StringBuffer(m_name);
        buf.setLength(MAX_NAME_SIZE);   // Truncate or pad, as necessary
        file.writeChars(buf.toString());
    }

    /**
    *   Reads the employee's name from the specified file.
    *   The name is extracted, any padding removed, and then trimmed down.
    */
    private String readName(RandomAccessFile file)
        throws IOException
    {
        StringBuffer buf = new StringBuffer();
        for (int i = 0; i < MAX_NAME_SIZE; i++)
        {
            buf.append( file.readChar() );
        }
        String name = buf.toString().replace('\0', ' ').trim();
        return name;
    }

    ////// Private data ///////
    private static final int MAX_NAME_SIZE = 40;    // Characters

    private int     m_employeeID = -1;   // Employee id
    private String  m_name = "";         // Employee name
    private double  m_salary = 0.0;      // Employee salary
    private int     m_managedBy = -1;    // Manager's employee id (-1 if no manager)
}

 

The page was last updated February 19, 2008