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

Java Object Oriented Programming (Event Handling): Program for reading employee

ID: 665527 • Letter: J

Question

Java Object Oriented Programming (Event Handling):

Program for reading employee data from the text file and store it in the data structure. The program should execute in such a way that employee objects are created.

The superclass contains employee first name, last name. It also contains private date of birth and the employee id.

The subclass contains the protected address and the phone number.

The second subclass contains public employee salary and major.

The test class shows the GUI (Graphical User Interface) menu for searching the data.

Button 1 – Searching the employee by ID

Button 2 – Searching the employee by first name

Button 3 – displaying all employee (sorted list by first name)

Button 4 – displaying data for all employees (Sorted by date of birth)

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:

John | Hales | 10/08/1980 | 111001 | France | 111-000-100 | $20,000 | Engineering

Tom | Michael | 08/08/1981 | 101002 | Germany | 111-000-200 | $22,000 | Computer Science

Lisa | Brown | 10/10/1982 | 110003 | France | 111-000-202 | $24,500 | Business

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