0

Stackers. I guess this question is pretty forward, but how is it possible for me to create individual setters in ValidateUser?

Right now it is the constructor that sets them all at once, but I need to have individual setters like:

public void setUsername(String user){
    userName = user;
}

... and so forth.

This is my current ArrayList:

ArrayList<ValidateUser> personer = new ArrayList<ValidateUser>();
public Users() {
    personer.add(new ValidateUser("admin", "asdf123", 0.8, "admin"));
    personer.add(new ValidateUser("jesper", "ukamm19", 2.5, "user"));
    personer.add(new ValidateUser("lars", "lol123", 1.5, "user"));
}
4
  • public void addUser(ValidateUser user) Commented Nov 22, 2013 at 21:22
  • @SteveKuo where do I put my parameters then? Commented Nov 22, 2013 at 21:24
  • You create ValidateUser just like you did above. Just put that in the parameter of addUser(new ValidateUser(....)) Commented Nov 22, 2013 at 21:32
  • public void addUser(ValidateUser user) { personer.add(user); } Commented Nov 23, 2013 at 19:51

2 Answers 2

1

I think your question is confusing, because you mention your ArrayList, but your actual question seems to be about the ValidateUser class. If you want individual setters, you have to modify the ValidateUser class to accept those setters. If you control that class, you can simply do this:

public class ValidateUser {       
   private String user;

   public void setUsername(String user) {
       this.user = user;
   }    
   // etc    
}
Sign up to request clarification or add additional context in comments.

Comments

0

like this..

public class ValidateUser{
    private String user;
    private String pass;
    private double rating;
    private String type;
    public void setUser(String user) {
        this.user = user;
    }
    public void setPass(String pass) {
        this.pass = pass;
    }
    public void setRating(double rating) {
        this.rating = rating;
    }
    public void setType(String type) {
        this.type = type;
    }
}

then you can use like this..

    ValidateUser user = new ValidateUser();
    user.setUser("admin");
    user.setPass("*****");
    user.setRating(5.4);
    user.setType("admin");
    personer.add(user);

5 Comments

my question is the same; where to put the individual parameters?
But how does Java knows which parameter from the new ValidateUser("admin", "asdf123", 0.8, "admin")) to put where?
yeah it was the second block of code, that did the trick for me! Thanks! :)
Consider making all of ValidateUser's fields final.
@SteveKuo i'm having some problems, when adding more "users". They seem to override each others informations. If not creating users1, users2 objects, how would be able to solve that?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.