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

Java Object Oriented Programming: Program for reading employee data from the tex

ID: 665568 • Letter: J

Question

Java Object Oriented Programming:

Program for reading employee data from the text file and creates employee objects (All the Objects will need to be stored in a data structure).

The superclass contains employee first name, last name. It also contains private date of employment and the employee id. The program also consists of two subclasses which are in the same package and also a testing class which is in second package.

The first subclass contains the protected address and the phone number.

The second subclass contains public employee salary and major.

The test class (In different Package) shows the menu, the example of the menu is given below

Please select the option:

Displays the sorted first names of all employees (Sorted List)

Displays the sorted employee ids of all the employees (Sorted List)

Display all the sorted addresses (Sorted List)

Display all sorted last names of all employees (Sorted List)

If the name is in the file, the program displays the first name, last name. as well as the employee id (but only last two digits) example XXXX01, and the birthdate but only the year (XX/XX/1990).

The text File:

First | Last    | DOE         | Phone #          | Major        |zipcode| id      | address

Tom | Brown | 08/08/1980 | 111-444-4501 | Engineering | 94101 | 1001 | France

John | Michael | 06/04/1990 | 111-333-4255 | Science | 94102 | 1002 | Germany

Maria | Walley | 10/08/1984 | 111-323-1456 | Biology | 94103 | 1003 | London

Explanation / Answer

working java code

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.io.Serializable;

public class empstorage {

    public void storeObject(Employee emp){
       
        OutputStream ops = null;
        ObjectOutputStream objOps = null;
        try {
            ops = new FileOutputStream("MyEmpFile.txt");
            objOps = new ObjectOutputStream(ops);
            objOps.writeObject(emp);
            objOps.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally{
            try{
                if(objOps != null) objOps.close();
            } catch (Exception ex){
               
            }
        }
       
    }
   
    public void displayObjects(){
       
        InputStream fileIs = null;
        ObjectInputStream objIs = null;
        try {
            fileIs = new FileInputStream("MyEmpFile.txt");
            objIs = new ObjectInputStream(fileIs);
            Employee emp = (Employee) objIs.readObject();
            System.out.println(emp);
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        } finally {
            try {
                if(objIs != null) objIs.close();
            } catch (Exception ex){
               
            }
        }
       
    }
   
    public static void main(String a[]){
        empstorage mof = new empstorage();
        Employee e1 = new Employee("Tony",1,"1000");
        mof.storeObject(e1);
        mof.displayObjects();
    }
}

class Employee implements Serializable{
   
    private String name;
    private int id;
    private String salary;
   
    public Employee(String name, int id, String salary){
        this.name = name;
        this.id = id;
        this.salary = salary;
    }
   
    public String toString(){
        return name +"=="+id+"=="+salary;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getSalary() {
        return salary;
    }

    public void setSalary(String salary) {
        this.salary = salary;
    }
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote