如何在链接的用户列表中添加类?

时间:2018-01-03 18:59:27

标签: java oop linked-list add

如何在链表中添加整个类,该链表还包含名称,密码和布尔值等参数,以检查它是否连接到服务器?

此问题的一个奇怪之处是用户已在User.java中成功注册,但未在UserRegistery.java中注册。

public class User {

    private String name = "";
    private String password = "";
    private String email = "";

    private boolean connection;

    public User(String name, String password, boolean connection){

        this.name = name;
        this.password = password;
        this.connection = connection;

        setUserName(name);
        setUserPassword(password);
        setUserConnection(connection);
        System.out.println("Client Created : User ["+format()+"]");
    }

    public void setUserName(String name){
        this.name = name;
    }

    public String getUserName(){
        return name;
    }

    public void setUserPassword(String password){
        this.password = password;
    }

    public String getUserPassword(){
        return password;
    }   

    public void setUserEmail(String email){
        this.email = email;
    }

    public String getUserEmail(){
        return email;
    }

    public void setUserConnection(boolean connection){
        this.connection = connection;
    }

    public boolean getUserConnection(){
        return connection;
    }

    public String format(){
        return String.format("%-5s, %-5s, %s" , getUserName(), 
                getUserPassword(), getUserConnection());
    }
}
public class UserRegistry {

    private LinkedList<User> users;
    private User user;
    private String name = "", password = "";
    private boolean conn = false;

    //Constructor:
    public UserRegistry() { //Setting new students in the Linked List
        users = new LinkedList<User>();
    }

    public void addUser(User aUser) {
        System.out.println("Waiting to "
            + "add user to the server . . . ");

        users.add(aUser);
        System.out.println("Client Creation "
            + ": Confirmed !!! ");

        System.out.println("Client Creation "
            + ": Declined !!! ");
    }


}

这是我得到的输出:

Screenshot

2 个答案:

答案 0 :(得分:0)

'registration'中的MainPanel.java变量是否已初始化?顺便说一下,你不需要在User构造函数中调用setter,你也不需要UserRegistry.java中的那个部分:

private User user;
private String name = "", password = "";
private boolean conn = false;

答案 1 :(得分:0)

如提供的屏幕截图所示,registration为空。请解决这个问题。