0

我目前正在学习 java 并试图创建一个员工管理程序,将员工记录存储到一个数组中,并允许管理层操作数组并打印任何员工报告。所以我创建了一个菜单和十个存储在数组中的员工对象......现在我想做的是允许用户通过菜单输入员工详细信息,并且该信息将存储在对象中,我希望用户能够按职称搜索员工,并且只显示男性员工和女性员工。

我首先从我的员工班开始,那里有我的修改器

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;
  }


}

然后在下面的类中创建对象和数组

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");
  }

  }
  

最后是我的测试器类,它显示菜单并允许用户选择一个选项

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);
      }
   
   }

}

是的,所以我设法找到了一种方法,使用户能够添加员工记录并显示添加的详细信息。我无法为菜单中所述的其他功能创建方法而无需创建新对象。

4

2 回答 2

0

这里有一些建议。Employee上课好像还可以。请注意,如果变量与类型的默认值相同,则不需要为变量提供默认值。前任:

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

是相同的

String lname;
int hoursWorked;
double rateOfpay;

您可能会考虑向该类添加一个函数以从 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();
    }
    ....
}

整个employeeArray班级似乎是必要的。但是,如果您愿意,则不需要临时变量e1, e2, etc。你有数组,你可以直接添加到它。循环在这里有帮助:

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];
    }
}
于 2020-07-23T19:54:27.807 回答
0

我认为您需要一个静态列表。

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);
    }

并阅读 toString() 方法以显示对象详细信息。 toString() , ArrayList ,静态变量

于 2020-07-23T19:50:29.510 回答