Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(1) Create a text (data) file containing data for three different employees. Let

ID: 3638400 • Letter: #

Question

(1) Create a text (data) file containing data for three different
employees. Let each data item sit on its own line in the text
file. (The file will therefore be 18 lines long.) For example,
the first six lines of the file might look like:

Sally L. Smith
086244
100000.00
Ramsey Hall 3814
9976
7

(2) In your main method (of course, in a separate "main module"),
do the following:

(a) Create three Employee records. (Make certain to declare
three separate Employee references and use these in
the record creation process.)

(b) Read all the employee data from the data file into the
three respective Employee records that you created.

(c) Print all the data for the three employees (retrieved from
the records) on the screen in a neat format.

(d) Print the average years of service of the three employees
(as a real number).

(e) Print the difference in years of service between
employee 1 and employee 2, the difference in years of
service between employee 1 and employee 3, and the
difference in years of service between employee 2 and
employee 3. Do not print negative numbers. Use the
built-in method Math.abs to get the absolute value of
the difference.

Explanation / Answer

Hi, I sent you an idea of the code via PM. Here are 2 parts: the text file, and the Java code - the java code has 2 classes: Employee and Main.

Let me know if there's something unclear or missing. I added comments and clearly marked the start and end of each step from (a) to (e) in the code.

Make sure you put the data file in the same directory as your compiled java code, and either name it employeeData.txt like I did in my code OR name it whatever you want and update the filename in the java code.

Hope this helps, please remember to rate :)

Sally L. Smith
086244
100000.00
Ramsey Hall 3814
9976
7
Amy V. Stevens
083066
150000.00
West End 1038
8948
9
John C. Parker
079926
140000.00
2129 H Street
8902
12

public class Employee {
   
    /* field/attribute declarations */
    private String Name;
    private String ID;
    private double Salary;
    private String Address;
    private String PhoneExt;
    private int YearsEmployed;
   
    /* class constructor method */
    public Employee(String n, String i, double s, String a, String pe, int ye){
        Name = n;
        ID = i;
        Salary = s;
        Address = a;
        PhoneExt = pe;
        YearsEmployed = ye;       
    }
   
    /* getter/accessor methods to return an employee's info values */
    public String getName(){
        return Name;
    }
    public String getID(){
        return ID;
    }
    public double getSalary(){
        return Salary;
    }
    public String getAddress(){
        return Address;
    }
    public String getPhoneExt(){
        return PhoneExt;
    }
    public int getYearsEmployed(){
        return YearsEmployed;
    }
} /* end of Employee class */

import java.io.*;

public class Main {

    public static void main(String[] args) {

        /*************** step (a) **********************/
        /* 3 variables to hold Employee records */
        Employee employee1 = null;
        Employee employee2 = null;
        Employee employee3 = null;
        /***********************************************/
        /*************** step (b) **********************/
        /* use a reader to read from the input file -
         * the file path is short because placed in
         * the same directory for convenience
         */
        try {
            BufferedReader in = new BufferedReader(new FileReader("employeeData.txt"));

            /* read each line in order, after every 6 lines
            store what was obtained in an employee record
            repeat this 3 times in a loop to read all 3 records
             */
            int nextEmployee = 0; /* keep track of which record to store in */
            for (int i = 0; i < 3; i++) {
                nextEmployee++;
                if (nextEmployee == 1) {
                    String name1 = in.readLine();
                    String id1 = in.readLine();
                    double salary1 = Double.parseDouble(in.readLine());
                    String address1 = in.readLine();
                    String phone1 = in.readLine();
                    int years1 = Integer.parseInt(in.readLine());

                    /* initialize employee1 with these values */
                    employee1 = new Employee(name1, id1, salary1, address1, phone1, years1);
                } else if (nextEmployee == 2) {
                    String name2 = in.readLine();
                    String id2 = in.readLine();
                    double salary2 = Double.parseDouble(in.readLine());
                    String address2 = in.readLine();
                    String phone2 = in.readLine();
                    int years2 = Integer.parseInt(in.readLine());

                    /* initialize employee2 with these values */
                    employee2 = new Employee(name2, id2, salary2, address2, phone2, years2);
                } else if (nextEmployee == 3) {
                    String name3 = in.readLine();
                    String id3 = in.readLine();
                    double salary3 = Double.parseDouble(in.readLine());
                    String address3 = in.readLine();
                    String phone3 = in.readLine();
                    int years3 = Integer.parseInt(in.readLine());

                    /* initialize employee3 with these values */
                    employee3 = new Employee(name3, id3, salary3, address3, phone3, years3);
                } else { /* do nothing */ }

            }/* done reading into the 3 records */

            in.close(); /* close reader when done reading */
        } catch (IOException e) {
            System.out.println("Something went wrong while reading from file");
        }
        /***********************************************/
        /*************** step (c) **********************/
        System.out.println("Employee #1:");
        System.out.println(" " + employee1.getName()
                + " [ID# " + employee1.getID() + "]");
        System.out.println(" Salary : $" + employee1.getSalary());
        System.out.println(" Address : " + employee1.getAddress());
        System.out.println(" Phone : " + employee1.getPhoneExt());
        System.out.println(" Worked for : " + employee1.getYearsEmployed() + " years");

        System.out.println(" *************************** ");

        System.out.println("Employee #2:");
        System.out.println(" " + employee2.getName()
                + " [ID# " + employee2.getID() + "]");
        System.out.println(" Salary : $" + employee2.getSalary());
        System.out.println(" Address : " + employee2.getAddress());
        System.out.println(" Phone : " + employee2.getPhoneExt());
        System.out.println(" Worked for : " + employee2.getYearsEmployed() + " years");

        System.out.println(" *************************** ");

        System.out.println("Employee #3:");
        System.out.println(" " + employee3.getName()
                + " [ID# " + employee3.getID() + "]");
        System.out.println(" Salary : $" + employee3.getSalary());
        System.out.println(" Address : " + employee3.getAddress());
        System.out.println(" Phone : " + employee3.getPhoneExt());
        System.out.println(" Worked for : " + employee3.getYearsEmployed() + " years");

        /***********************************************/
        /*************** step (d) **********************/
        int sum = employee1.getYearsEmployed() + employee2.getYearsEmployed()
                + employee3.getYearsEmployed();

        /* divide by 3.0 instead of 3 to keep precision
         * or cast the sum to double datatype as well
         */
        double average = sum / 3.0;

        System.out.println("Average years of service of the 3 employees is: " + average);

        /***********************************************/
        /*************** step (e) **********************/
        int diff_1_2 = Math.abs(employee1.getYearsEmployed() - employee2.getYearsEmployed());
        int diff_1_3 = Math.abs(employee1.getYearsEmployed() - employee3.getYearsEmployed());
        int diff_2_3 = Math.abs(employee2.getYearsEmployed() - employee3.getYearsEmployed());

        System.out.println("Difference in years of service between 1 & 2: " + diff_1_2);
        System.out.println("Difference in years of service between 1 & 3: " + diff_1_3);
        System.out.println("Difference in years of service between 2 & 3: " + diff_2_3);

        /***********************************************/
    } /* end of main method */

} /* end of Main test class */