使用employee类获取用户输入,将其存储到数组中,并在主方法Java中显示

使用employee类获取用户输入,将其存储到数组中,并在主方法Java中显示,java,class,arraylist,input,Java,Class,Arraylist,Input,我是编程新手,在过去的几天里,我一直在尝试获取用户输入,将其存储在arraylist中,然后输出具体的细节。如果有人能告诉我哪里出了问题,我会非常感激。我似乎无法访问employee类的输入并将其存储在main方法的arraylist中 谢谢 public class OctEmployeeArray{ public void menu(){ boolean breakOut= false; while(breakOut == false){ System.o

我是编程新手,在过去的几天里,我一直在尝试获取用户输入,将其存储在arraylist中,然后输出具体的细节。如果有人能告诉我哪里出了问题,我会非常感激。我似乎无法访问employee类的输入并将其存储在main方法的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());
        }
    }
}   
公共类OctEmployeeArray{
公共无效菜单(){
布尔突破=假;
while(breakOut==false){
System.out.println(“======================================================================”);
System.out.println(“======================================================================”);
System.out.println(“员工假期权利计划”);
System.out.println(“请选择适当的选项:”);
System.out.println(“======================================================================”);
System.out.println(“======================================================================”);
System.out.println(“1)输入新员工详细信息:”;
System.out.println(“2)显示员工平均年龄:”;
System.out.println(“3)显示特定员工的信息:”;
System.out.println(“4)显示所有数据库帐户:”;
System.out.println(“5)退出程序:”;
System.out.println(“======================================================================”);
扫描仪输入=新扫描仪(System.in);//已分配给“输入”的扫描仪
String choice=in.nextLine();//存储在choice中的用户输入
开关(选择){
案例“1”:System.out.println(“您选择了“添加员工”输入”);
员工e1=新员工();
e1.添加员工();
打破
案例“2”:
System.out.println(“您选择了b输入”);
打破
案例“3”:
System.out.println(“您选择了c输入”);
//SearchSpecificEmployee();
打破
案例“4”:
System.out.println(“您选择了“全部显示”输入”);
//displayAll();
打破
案例“5”:
System.out.println(“您选择退出”);
突破=正确;
打破
违约:
System.out.println(“输入不准确”);
打破
}
}   
}
公营雇员{
私有字符串staffName;
私人int员工编号;
私人国际公寓;
私营机构开始运作;
私有字符串角色;
公共雇员(字符串staffName、int staffAge、int yearStarted、int staffNumber、字符串角色){
this.staffName=staffName;
这个.staffNumber=staffNumber;
这个.staffAge=staffAge;
this.yearStarted=yearStarted;
这个角色=角色;
}
公职人员(){
}
公共整数getAge(){
返回staffAge;
}
公共字符串getName(){
返回员工姓名;
}
public int getStaffNum(){
返回员工编号;
}
public int getStartYear(){
开始返回;
}
public Employee addEmployee(){//这会询问新员工的问题,并将其保存在名为e的Employee对象中
扫描仪输入=新扫描仪(系统输入);
//员工d=新员工();
System.out.println(“请输入您的全名:”);
字符串名称=in.nextLine();
System.out.println(“请输入您的年龄:”;
int age=Integer.parseInt(in.nextLine());
System.out.println(“请输入您的员工编号:”);
int staffNum=Integer.parseInt(in.nextLine());
System.out.println(“请输入当前工作年份:”);
int startYear=Integer.parseInt(in.nextLine());
System.out.println(“请输入角色雇用:”);
字符串role=in.nextLine();
Employee e=新员工(姓名、年龄、startYear、staffNum、角色);//将所有变量放入e中并返回
返回e;
}
}
公共静态void main(字符串[]args){
ArrayList alist=new ArrayList();//为保存员工对象而创建的ArrayList
OctEmployeeArray o=新建OctEmployeeArray();//为访问主菜单而创建的对象
o、 menu();//显示主菜单
Employee em=new Employee();//创建Employee对象以访问add Employee方法
列表添加(em.addEmployee);
用于(员工人数:1){
System.out.println(count.getName());
System.out.println(count.getStaffNum());
}
}
}   

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


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

您能否将代码缩小到问题的特定区域,并详细说明哪些不起作用?“alist.add(em.addEmployee);“将其更改为”alist.add(em.addEmployee());“错误的方法调用如果您询问得到的是什么错误或它卡在哪里以给出输出,而不是将所有代码交给技术人员检查,则会更好。通过查看您的代码,我觉得如果您将代码分离,这将是一个很好的选择。让我们举一个例子,您创建了
Employee
作为一个类来包含数据,并将方法
addEmployee
添加到同一个类中,我觉得您可以将Employee作为一个模型,并使用不同的类来容纳您的业务逻辑。