CreateEmployeeFile.java Source code

Java tutorial

Introduction

Here is the source code for CreateEmployeeFile.java

Source

import java.io.IOException;
import java.io.RandomAccessFile;

public class CreateEmployeeFile {
    public static void main(String[] args) throws Exception {
        String[] fnames = { "A", "B", "C" };

        String[] lnames = { "a", "b", "c" };

        String[] addresses = { "Box 100", "55 Street", "6 Lane" };

        byte[] ages = { 46, 59, 32 };

        double[] salaries = { 5.0, 6.0, 3.0 };

        RandomAccessFile raf = new RandomAccessFile("employee.dat", "rw");

        EmployeeRecord er = new EmployeeRecord();

        for (int i = 0; i < fnames.length; i++) {
            er.setFirstName(fnames[i]);
            er.setLastName(lnames[i]);
            er.setAddress(addresses[i]);
            er.setAge(ages[i]);
            er.setSalary(salaries[i]);
            er.write(raf);
        }
        raf = new RandomAccessFile("employee.dat", "rw");

        er = new EmployeeRecord();

        int numRecords = (int) raf.length() / er.size();

        for (int i = 0; i < numRecords; i++) {
            er.read(raf);

            System.out.print(er.getFirstName() + " ");
            System.out.print(er.getLastName() + " ");
            System.out.print(er.getAddress() + " ");
            System.out.print(er.getAge() + " ");
            System.out.println(er.getSalary());
        }
        raf.seek(0);
        for (int i = 0; i < numRecords; i++) {
            er.read(raf);
            if (er.getAge() >= 55) {
                er.setSalary(0.0);
                raf.seek(raf.getFilePointer() - er.size());
                er.write(raf);
                raf.seek(raf.getFilePointer() - er.size());
                er.read(raf);
            }
            System.out.print(er.getFirstName() + " ");
            System.out.print(er.getLastName() + " ");
            System.out.print(er.getAddress() + " ");
            System.out.print(er.getAge() + " ");
            System.out.println(er.getSalary());
        }

    }
}

class EmployeeRecord {
    private String lastName;

    private String firstName;

    private String address;

    private byte age;

    private double salary;

    void read(RandomAccessFile raf) throws IOException {
        char[] temp = new char[15];
        for (int i = 0; i < temp.length; i++)
            temp[i] = raf.readChar();
        lastName = new String(temp);
        temp = new char[15];
        for (int i = 0; i < temp.length; i++)
            temp[i] = raf.readChar();
        firstName = new String(temp);
        temp = new char[30];
        for (int i = 0; i < temp.length; i++)
            temp[i] = raf.readChar();

        address = new String(temp);
        age = raf.readByte();
        salary = raf.readDouble();
    }

    void write(RandomAccessFile raf) throws IOException {
        StringBuffer sb;
        if (lastName != null)
            sb = new StringBuffer(lastName);
        else
            sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (firstName != null)
            sb = new StringBuffer(firstName);
        else
            sb = new StringBuffer();

        sb.setLength(15);
        raf.writeChars(sb.toString());

        if (address != null)
            sb = new StringBuffer(address);
        else
            sb = new StringBuffer();

        sb.setLength(30);
        raf.writeChars(sb.toString());
        raf.writeByte(age);
        raf.writeDouble(salary);
    }

    void setAge(byte age) {
        this.age = age;
    }

    byte getAge() {
        return age;
    }

    void setAddress(String address) {
        this.address = address;
    }

    String getAddress() {
        return address;
    }

    void setFirstName(String firstName) {
        this.firstName = firstName;
    }

    String getFirstName() {
        return firstName;
    }

    void setLastName(String lastName) {
        this.lastName = lastName;
    }

    String getLastName() {
        return lastName;
    }

    void setSalary(double salary) {
        this.salary = salary;
    }

    double getSalary() {
        return salary;
    }

    int size() {
        return 2 * (15 + 15 + 30) + 9;
    }
}