使用employee类进行用户输入,将其存储到数组中并以main方法显示Java

时间:2017-10-05 10:12:52

标签: java class arraylist input

我是编程新手,并且花了最后几天尝试接受用户输入,将其存储在arraylist中然后输出特定的详细信息。我真的很感激有人在我出错的地方发光。我似乎无法访问员工类的输入并将其存储在主方法的arraylist中。

感谢。

public class OctEmployeeArray{

public void menu(){

    boolean breakOut= false;
    while(breakOut == false){
        System.out.println("=====================================");            
        System.out.println("=====================================");        
        System.out.println("EMPLOYEE HOLIDAY ENTITLEMENT PROGRAM");
        System.out.println("Please chose the appropriate option: ");

        System.out.println("=====================================");
        System.out.println("=====================================");
        System.out.println("1) Enter new employee details:");
        System.out.println("2) Display the employee average age:");
        System.out.println("3) Display information on specific employee:");
        System.out.println("4) Display all database accounts:");
        System.out.println("5) Exit the program:");
        System.out.println("=====================================");

        Scanner in = new Scanner(System.in);    //scanner assigned to 'in'
        String choice = in.nextLine();  //user input stored in choice

        switch (choice){
            case "1": System.out.println("You chose the 'add employee' input");
                Employee e1= new Employee();
                e1.addEmployee();
                break;

            case "2": 
                System.out.println("You chose the b input");
                break;

            case "3":   
                System.out.println("You chose the c input");
               //searchspecficEmployee();   
               break;

            case "4":
                System.out.println("You chose the 'display all' input");
                //displayAll();
                break;

            case "5":
                System.out.println("You chose to Exit.");
                breakOut= true;
                break;

            default:
                System.out.println("The input is not accurate");
                break;
        }
    }   
}

public class Employee {
    private String staffName;
    private int staffNumber;
    private int staffAge;
    private int yearStarted;
    private String role;


    public Employee(String staffName, int staffAge, int yearStarted, int staffNumber, String role){
        this.staffName= staffName;
        this.staffNumber= staffNumber;
        this.staffAge= staffAge;
        this.yearStarted= yearStarted;
        this.role= role;            
    }

    public Employee(){

    }

    public int getAge(){
        return staffAge;
    }

    public String getName(){
        return staffName;
    }

    public int getStaffNum(){
        return staffNumber;
    }

    public int getStartYear(){
        return yearStarted;
    }

    public Employee addEmployee(){//this asks the new employee questions, saves them in a employee object called e

        Scanner in= new Scanner(System.in);
        //Employee d= new Employee();
        System.out.println("Please enter your Full name:");
        String name = in.nextLine();
        System.out.println("Please enter your age:");
        int age= Integer.parseInt(in.nextLine());
        System.out.println("Please enter your staff number:");
        int staffNum= Integer.parseInt (in.nextLine());
        System.out.println("Please enter year of current employment:");
        int startYear=Integer.parseInt(in.nextLine());
        System.out.println("Please enter role employment:");
        String role=in.nextLine();

        Employee e= new Employee(name, age, startYear, staffNum, role);// puts all the varibles and puts them inside e and returns it
        return e;
    }

}

    public static void main(String[] args) {
        ArrayList<Employee> alist= new ArrayList();// arraylist created to save employee objects
        OctEmployeeArray o= new OctEmployeeArray();// object created for access to main menu    
        o.menu();//display main menu

        Employee em= new Employee();// create employee object to access add employee method
        alist.add(em.addEmployee);

        for(Employee count : alist){
            System.out.println(count.getName());
            System.out.println(count.getStaffNum());
        }
    }
}   

1 个答案:

答案 0 :(得分:0)

您正在Employee方法中创建新的addEmployee()实例。那没错。但是这个方法在Employee类中的事实在语义上是无意义的(对不起) - 而且我认为这也是它不起作用的原因(这是巧合。不幸的是,许多语义废话都有效。)。

您应该从addEmployee()类中删除Employee方法,并将其移至包含Employee数组的类。不要在Employee类中创建新的Employee实例。