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: 665523 • Letter: J

Question

Java Object Oriented Programming:

Program for reading employee data from the text file and store it in the data structure.

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 menu for searching the data.

Search by first name.

Search by employee id.

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:

Mark | Brown | 10/08/1990 | 100001 | France | 123-456-789 | $10,000 | Engineering

Jerry | Michael | 08/08/1991 | 100002 | Germany | 123-456-987 | $11,000 | Engineering

Jessica | Wales | 10/10/1990 | 100003 | France | 441-456-788 | $10,500 | Engineering

Explanation / Answer

//Employee.java
public class Employee
{
   //private members of class Employee
   private String firstName;
   private String lastName;
   private String dateOfBirth;
   private int id;
  
   //default costructor
   public Employee()
   {
       firstName="";
       lastName="";
       dateOfBirth="";
       id=0;
   }
  
   //Parameter constructor
   public Employee(String firstName,String lastName,String dateOfBirth,
           int id)
   {
       this.firstName=firstName;
       this.lastName=lastName;
       this.dateOfBirth=dateOfBirth;
       this.id=id;
   }
  
  
   public String getFirstName()
   {
       return firstName;
   }
   public String getLastName()
   {
       return lastName;
   }
   public String getDOB()
   {
       return dateOfBirth;
   }
   public int getID()
   {
       return id ;
   }
      
}


--------------------------------------------------------------------------------------------------------------------


//The class extends the super class Employee
//Address.java
public class Address extends Employee
{
   protected String address;
   protected String phoneNumber;
   //parameter constructor costructor
   public Address(String firstName,String lastName,String dateOfBirth,
           int id,String address, String phoneNumber)
   {
       super(firstName, lastName, dateOfBirth, id);
       this.address=address;
       this.phoneNumber=phoneNumber;
   }
  
   public String getAddress()
   {
       return address;      
   }
  
   public String getPhoneNumber()
   {
       return phoneNumber;      
   }  
}


------------------------------------------------------------------------------------------------------------------------

//The class inherits from Address class
//Major.java
public class Major extends Address
{
   private String salary;
   private String major;
  
   //Parameter constructor
   Major(String firstName,String lastName,String dateOfBirth,
           int id,String address, String phoneNumber,String salary,
           String major)
   {
       super(firstName, lastName, dateOfBirth, id, major, major);
       this.salary=salary;
       this.major=major;
   }
  
  
   public String getSalary()
   {
       return salary;
   }
  
   public String getMajor()
   {
       return major;
   }
  
}


--------------------------------------------------------------------------------------------------------------------------------

//Driver.java
import java.io.File;
import java.util.Scanner;
public class Driver
{
   public static void main(String[] args)
   {
      
      
       //size of the employees
       int size=10;
       //create a Scanner class
       Scanner scanner=new Scanner(System.in);
      
       //Create an array of Major class
       Major[] employees=new Major[size];
      
       //input file name
       String fileName="input.txt";
       //Create a file class object
       File file=new File(fileName);

       String firstName;
       String lastName;
       String dateOfBirth;
       int id;
       String address;
       String phoneNumber;
       String salary;
       String major;
       int index=0;


       try
       {

           Scanner fileReader=new Scanner(file);

           //read fileReader class object
           while(fileReader.hasNextLine())
           {

               String line=fileReader.nextLine().trim();
               String[] tokens=line.split(" | ");
               //read values in the line by splitting the line by |
               //and get alternative strings by ignoreing | letter
               firstName=tokens[0];
               lastName=tokens[2];
               dateOfBirth=tokens[4];
               id=Integer.parseInt(tokens[6]);
               address=tokens[8];
               phoneNumber=tokens[10];
               salary=tokens[12];
               major=tokens[14];
              
               //construct a Major object and set the values
               employees[index]=new Major(firstName, lastName, dateOfBirth, id,
                       address, phoneNumber, salary, major);
              
               index++;

           }
          
           //close the file reader
           fileReader.close();

       } catch (Exception e) {
           System.err.println(e);
       }
      
      
      
      
       System.out.println("1.Search by first name");
       System.out.println("2.Search by employee id");
      
      
       //prompt for user choice
       System.out.println("Enter your choice : ");
       int userChoice=scanner.nextInt();
      
       scanner.nextLine();
       if(userChoice==1)
       {
           System.out.println("Enter first name");
           String fName=scanner.next();
          
           //search by first name
           int position=searchByFirstName(employees,fName);
          
           if(position!=-1)
           {
               System.out.println("First Name : "+employees[position].getFirstName());
               System.out.println("Last Name : "+employees[position].getLastName());
              
               int digit1=employees[position].getID()%10;
               int temp=employees[position].getID()/10;
               int digit2=temp%10;
              
               System.out.println("ID : xxxx"+digit1+""+digit2);
              
              
               String dob=employees[position].getDOB();
              
               String[] tokens=dob.split("/");
              
               System.out.println("Date of Birth : "+tokens[2]);
              
              
              
           }
          
       }
       else if(userChoice==2)
       {
           System.out.println("Enter employee id ");
           int empId=scanner.nextInt();
          
           //search by employee id
           int position=searchByID(employees,empId);
          
           System.out.println("First Name : "+employees[position].getFirstName());
           System.out.println("Last Name : "+employees[position].getLastName());
          
           int digit1=employees[position].getID()%10;
           int temp=employees[position].getID()/10;
           int digit2=temp%10;
          
           System.out.println("ID : xxxx"+digit1+""+digit2);
          
          
           String dob=employees[position].getDOB();
          
           String[] tokens=dob.split("/");
          
           System.out.println("Date of Birth : "+tokens[2]);
          
       }
      


   }

  
  
   //Returns the index of the employee whose id mateched with empId
   //otherwiser returs -1
   private static int searchByID(Major[] employees, int empId)
   {
      
       int position=-1;
       boolean found=false;
      
       for (int index = 0; index < employees.length && !found; index++)
       {
           if(employees[index].getID()==empId)
           {
               found=true;
               position=index;
           }
       }
      
      
       return position;
   }
   //Returns the index of the employee whose firstName mateched with fName
       //otherwiser returs -1
   private static int searchByFirstName(Major[] employees, String fName) {
      
       int position=-1;
       boolean found=false;
      
       for (int index = 0; index < employees.length && !found; index++)
       {
           if(employees[index].getFirstName().equals(fName))
           {
               found=true;
               position=index;
           }
       }
          
       return position;
   }
}


--------------------------------------------------------------------------------------------------------------------------------

Sample input.txt file

Mark | Brown | 10/08/1990 | 100001 | France | 123-456-789 | $10,000 | Engineering
Jerry | Michael | 08/08/1991 | 100002 | Germany | 123-456-987 | $11,000 | Engineering
Jessica | Wales | 10/10/1990 | 100003 | France | 441-456-788 | $10,500 | Engineering

--------------------------------------------------------------------------------------------------------------------------------------------

Sample output:

1.Search by first name
2.Search by employee id
Enter your choice :
1
Enter first name
Mark
First Name : Mark
Last Name : Brown
ID : xxxx10
Date of Birth : xx/xx/1990


sample run2

1.Search by first name
2.Search by employee id
Enter your choice :
2
Enter employee id
100003
First Name : Jessica
Last Name : Wales
ID : xxxx30
Date of Birth : xx/xx/1990


Hope this helps you

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