0

Im currently learning java and im trying to create an Employee management program that stores employee records into an array and allow management to manipulate the array and print any employee reports. So I create a menu and ten employee objects which got stored in an array...now what I want to do is allow the user to input employee details through the menu and that information will be stored inside the objects and I want the user to be able to search for an employee by their title and also display only male of female employees.

I first started with my employee class where has my modifers

public class Employee{
  int emp_id=0;
  String fname="pebbles";
  String lname=null;
  String dob=null;
  String gender=null;
  String address=null;
  String title=null;
  String dateHired=null;
  String department=null;
  int hoursWorked=0;
  double rateOfpay=0.0;
  int leaveDays=0;
  double carAllowance=0.0;
  double monthlyGratuity=0.0;
  double taxRate=0.0; 
  
  public Employee(){}
  
  public Employee(int emp_id,String fname,String lname,String dob,String gender,String address,String title,String dateHired,String
  department,int hoursWorked,double rateOfpay,int leaveDays,double carAllowance,double monthlyGratuity,double taxRate){
     this.emp_id=emp_id;
     this.fname=fname;
     this.lname=lname;
     this.dob=dob;
     this.gender=gender;
     this.address=address;
     this.title=title;
     this.dateHired=dateHired;
     this.department=department;
     this.hoursWorked=hoursWorked;
     this.rateOfpay=rateOfpay;
     this.leaveDays=leaveDays;
     this.carAllowance=carAllowance;
     this.monthlyGratuity=monthlyGratuity;
     this.taxRate=taxRate;
  }
  
  public Employee(int emp_id,String fname,String lname){
     this.emp_id=emp_id;
     this.fname=fname;
     this.lname=lname;
  }
  
  public void setEmployeeId(int emp_id){
     this.emp_id=emp_id;
  }

  public void setFirstName(String fname){
     this.fname=fname;
  }
  
  public void setLastName(String lname){
     this.lname=lname;
  }
  
  public void setDateOfBirth(String dob){
     this.dob=dob;
  }   
  
  public void setGender(String gender){
     this.gender=gender;
  }
  
  public void setAddress(String address){
     this.address=address;
  }
  
  public void setTitle(String title){
     this.title=title;
  }
  
  public void setDateHired(String dateHired){
     this.dateHired=dateHired;
  }
  
  public void setDepartment(String department){
     this.department=department;
  }
  
  public void setHoursWorked(int hoursWorked){
     this.hoursWorked=hoursWorked;
  }
  
  public void setRateOfPay(double rateOfpay){
     this.rateOfpay=rateOfpay;
  }
  
  public void setLeaveDays(int leaveDays){
     this.leaveDays=leaveDays;
  }
  
  public void setCarAllowance(double carAllowance){
     this.carAllowance=carAllowance;
  }
  
  public void setMonthlyGratuity(double monthlyGratuity){
     this.monthlyGratuity=monthlyGratuity;
  }
  
  public void setTaxRate(double taxRate){
     this.taxRate=taxRate;
  }
  
  public int getEmployeeId(){
     return emp_id;
  }
  
  public String getFirstName(){
     return fname;
  }
  
  public String getLastName(){
     return lname;
  }
  
  public String getDateOfBirth(){
     return dob;
  }
  
  public String getGender(){
     return gender;
  }
  
  public String getAddress(){
     return address;
  }
  
  public String getTitle(){
     return title;
  }
  
  public String getDateHired(){
     return dateHired;
  }
  
  public String getDepartment(){
     return department;
  }
  
  public int getHoursWorked(){
     return hoursWorked;
  }
  
  public double getRateOfPay(){
     return rateOfpay;
  }
  
  public int getLeaveDays(){
     return leaveDays;
  }
  
  public double getCarAllowance(){
     return carAllowance;
  }
  
  public double getMonthlyGratuity(){
     return monthlyGratuity;
  }
  
  public double getTaxRate(){
     return taxRate;
  }


}

Then created the objects and arrays in the class below

import java.util.Scanner;
public class employeeArray extends Employee{

  private static Employee employees[] = new Employee[10];
  

     
  static Scanner input=null;
  String fname="pebbles";
  String lname=null;
  String dob=null;
  String gender=null;
  int emp_id=0;
  String address=null;
  String title=null;
  String dateHired=null;
  String department=null;
  int hoursWorked=0;
  double rateOfpay=0.0;
  int leaveDays=0;
  double carAllowance=0.0;
  double monthlyGratuity=0.0;
  double taxRate=0.0;
  
  
  public static Employee e1=new Employee(12345,"Aobakwe","Mothabi","26/07/1990","Male","Private 009 Masunga","CEO",
        "06/02/2008","Finance",500,50.0,10,100.0,1500,5.1);
        
  public static Employee e2=new Employee();
  
  public static Employee e3=new Employee();
  
  public static Employee e4=new Employee();
  
  public static Employee e5=new Employee();
  
  public static Employee e6=new Employee();
  
  public static Employee e7=new Employee();
  
  public static Employee e8=new Employee();
  
  public static Employee e9=new Employee();
  
  public static Employee e10=new Employee();
  




  
  public static void main(String args[]){
     input=new Scanner(System.in);
  
     Employee e1=new Employee(12345," ","Mothabi","26/07/1990","Male","Private 009 Masunga","CEO",
        "06/02/2008","Finance",500,50.0,10,100.0,1500,5.1);
        
     Employee e2=new Employee();
  
     Employee e3=new Employee();
  
     Employee e4=new Employee();
  
     Employee e5=new Employee();
  
     Employee e6=new Employee();
  
     Employee e7=new Employee();
  
     Employee e8=new Employee();
  
     Employee e9=new Employee();
  
     Employee e10=new Employee();
  
        
    
     
     employees[0]=e1;
     employees[1]=e2;
     employees[2]=e3;
     employees[3]=e4;
     employees[4]=e5;
     employees[5]=e6;
     employees[6]=e7;
     employees[7]=e8;
     employees[8]=e9;
     employees[9]=e10;
     


   //ENTER NAME
     System.out.println("Enter name");
     String fname=input.next();
     e2.setFirstName(fname);
     
     //ENTER LAST NAME
     System.out.println("Enter surname");
     String lname=input.next();
     e2.setLastName(lname);
     
     //ENTER DATE OF BIRTH
     System.out.println("Enter date of birth");
     String dob=input.next();
     e2.setDateOfBirth(dob);
     
     //ENTER GENDER
     System.out.println("Enter Gender");
     String gender=input.next();
     e2.setGender(gender);
     
     //ENTER EMPLOYEE ID
     System.out.println("Enter Employee Id");
     int emp_id=input.nextInt();
     e2.setEmployeeId(emp_id);
     
      //ENTER ADDRESS
     System.out.println("Enter Address");
     String address=input.next();
     e2.setAddress(address);
     
      //ENTER TITLE
     System.out.println("Enter Employee Title");
     String title=input.next();
     e2.setTitle(title);
     
     //ENTER DATE HIRED
     System.out.println("Enter Date Hired");
     String dateHired=input.next();
     e2.setDateHired(dateHired);
     
      //ENTER DEPARTMENT
     System.out.println("Enter Department");
     String department=input.next();
     e2.setDepartment(department);
     
     //ENTER HOURS WORKED
     System.out.println("Enter Hours Worked");
     int hoursWorked=input.nextInt();
     e2.setHoursWorked(hoursWorked);
     
      //ENTER RATE OF PAY
     System.out.println("Enter Rate Of Pay");
     double rateOfpay=input.nextDouble();
     e2.setRateOfPay(rateOfpay);
     
     //ENTER LEAVE DAYS
     System.out.println("Enter Leave Days");
     int leaveDays=input.nextInt();
     e2.setLeaveDays(leaveDays);
     
      //ENTER CAR ALLOWANCE
     System.out.println("Enter Car Allowance");
     double carAllowance=input.nextDouble();
     e2.setCarAllowance(carAllowance);
     
      //ENTER MONTHLY GRATUITY
     System.out.println("Enter Monthly Gratuity");
     double monthlyGratuity=input.nextDouble();
     e2.setMonthlyGratuity(monthlyGratuity);
     
       //ENTER TAX RATE
     System.out.println("Enter Tax Rate");
     double taxRate=input.nextDouble();
     e2.setTaxRate(taxRate);
     
     
     //DISPLAY RESULTS
     System.out.println(e2.getFirstName());
     System.out.println(e2.getLastName());
     System.out.println(e2.getDateOfBirth());
     System.out.println(e2.getGender());
     System.out.println(e2.getEmployeeId());
     System.out.println(e2.getAddress());
     System.out.println(e2.getTitle());
     System.out.println(e2.getDateHired());
     System.out.println(e2.getDepartment());
     System.out.println(e2.getHoursWorked());
     System.out.println(e2.getRateOfPay());
     System.out.println(e2.getLeaveDays());
     System.out.println(e2.getCarAllowance());
     System.out.println(e2.getMonthlyGratuity());
     System.out.println(e2.getTaxRate());*/
                  
  }


  static void mainMenu(){
  
     System.out.println(
        "Select an option\n"+
        "1)Add Employee Records\n"+
        "2) display only male of female employees\n"+
        "3) display all employees and their gross salaries\n"+
        "4) search for an employee and display salary calculated.\n");
  }

  }
  

and lastly here is my tester class which displays the menu and allows the user to select an option

import java.util.Scanner;

public class empTester extends employeeArray{

   static Scanner input=null;

   public static void main(String args[]){
   
      input=new Scanner(System.in);
      mainMenu();
      
      int choice=input.nextInt();
      if(choice==1){
         menuAdd();
         int option=input.nextInt();
         if(option==1){EmployeeCreation();}
         else if(option==2){System.out.println("It works number 2");}
         else if(option==3){mainMenu();}
      }
      
      else if(choice==2){
         menuDisplay();
         int option=input.nextInt();
         if(option==1){System.out.println("It works number 1");}
         else if(option==2){System.out.println("It works number 2");}
         else if(option==3){System.out.println("It works number 3");}
         else if(option==4){System.out.println("It works number 4");}
         else if(option==5){mainMenu();}
      }
      
      else if(choice==3){
         menuSearch();
         int option=input.nextInt();
         if(option==1){System.out.println("It works number 1");}
         else if(option==2){System.out.println("It works number 2");}
         else if(option==3){mainMenu();}
      }
      
      else if(choice==4){
         System.out.println("This is the About Section");
      }
      
      else if(choice==5){
         System.out.println("Ending Program");
         System.exit(0);
      }
   
   }

}

Yeah so I managed to find a way to enable the user to add an employee record and display details added. Im having trouble creating methods for other functions stated in the menu without having to create new objects.

2 Answers2

0

I think you need a static list.

public static List<Employee> employees = new ArrayList<>();`

    public void addEmployee() {
       Employee employee = new Employee();
       input=new Scanner(System.in);

       String fname=input.next();
       employee.setFirstName(fname);
       .
       .
       . and others setters...
       
       eployees.add(employee);
    }

And read about toString() method to display object details. toString(), ArrayList, static variable

Seldo97
  • 611
  • 1
  • 8
  • 17
  • Okay thanks @Seldo97 but will the information I entered be stored inside the array? – Mr_Pebbles Jul 23 '20 at 19:53
  • @Mr_Pebbles Yes, create the object, set informations and add him to List. List is better than array in this case becouse has dynamic size. I edited my answer and added useful links for you. – Seldo97 Jul 23 '20 at 19:58
0

Here are a few suggestions. The Employee class seems ok. Note that you do not need to supply default values for variables if they are the same as the type's default value. Ex:

String lname=null;
int hoursWorked=0;
double rateOfpay=0.0;

Is the same as

String lname;
int hoursWorked;
double rateOfpay;

You might consider adding a function to the class to read data from a Scanner:

class Employee {
    ....
    boolean readFromIn(Scanner input)
    {
        //ENTER NAME
        System.out.println("Enter name");
        fname = input.next();
     
        //ENTER LAST NAME
        System.out.println("Enter surname");
        lname = input.next();

        etc....
    }

    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append(lname);
        ....fill in rest
        return sb.toString();
    }
    ....
}

The entire employeeArray class seems necessary. But if you want that, there's no need for the temporary variables e1, e2, etc. You have the array, you can add directly to it. A loop helps here:

public class EmployeeArray /* extends Employee - does not extend Employee */
{
    private static Employee employees[] = new Employee[10];  // why static?

    public static void createEmployees()
    {
        Scanner in = new Scanner(System.in);
        for (int i = 0; i < employees.length; i++) {
            employees[i] = new Employee();
            employees[i].readFromIn(in);
        }
    }

    public static Employee getEmployee(int index)
    {
        if (index < 0 || index >= employees.length) {
            throw new ArrayIndexOutOfBoundsException();
        }
        return employees[i];
    }
}
001
  • 13,291
  • 5
  • 35
  • 66
  • will the information inputted stored and displayable? @JohnnyMopp – Mr_Pebbles Jul 23 '20 at 19:59
  • You might consider adding a `toString()` function to the `Employee` class. Also, the array in the `EmployeeArray` is `private` so you can add a function to get an item out of the array: `Employee getEmployee(int index)` – 001 Jul 23 '20 at 20:02
  • Okay thanks @JohnnyMopp and if I need to search for an employee object I will create a method with a search function and call that method in the for loop created in class EmployeeArray – Mr_Pebbles Jul 23 '20 at 20:06