0

So far I have created an Array List which only currently consists of one user. For my assignment, I have been asked to print out properties for that user such as its username and userType. As far as I aware I have created an enhanced for loop that would print out these properties if only they were assigned. My question is how would I go about assigning these properties to my user? I have created a UserGroup class in which the array list resides in and a User class in which some details about the User is specified however I am not quite sure if the User class is correct for what I am trying to achieve. Here is the code for both classes:

UserGroup:

 package main;
import java.util.ArrayList;

public class UserGroup {

    ArrayList<User> userGroup = new ArrayList<>();
    User userOne;

    public void addSampleData(String username, String userType, String name) {

    userGroup.add(new User("LeeB", "Staff", "Lee"));

}

    public User getUser(int index)  {

    return userGroup.get(0);

}

    public void printusername(){

       for (User x : userGroup)
           System.out.println(x);

    }
}

User:

 package main;
class User {

  String username;
  String userType;
  String name;


    User(String username, String userType, String name) {

    this.username = username;
    this.userType = userType;
    this.name = name;
    }

  public String getUsername() {
      return username;
  }

  public String getUserType() {
      return userType;
  }

   public String getName() {
      return name;
  }

  public String setUserType(String admin) {
      return userType = admin;
  }

}

Main method:

package main;

public class Main {
 public static void main(String[] args) {
    for (int counter=2; counter<=40; counter+=2) {
       System.out.println(counter);
    }
    System.out.println("For loop complete.");

    int counter = 1;
    int increment = 2;
    int loopexeccounter = 0;
    while (counter <= 500) {
        loopexeccounter = loopexeccounter + 1;
        System.out.println(counter);
    counter = counter + increment++;
    }
System.out.print("This loop iteratted "+loopexeccounter+" times.");
}

    public Main() {

     UserGroup userGroupObject = new UserGroup();

 System.out.println(userGroupObject.getUser(0)); 
 }

}

Ignore everything above the latest method, it's just some examples of loops that they wanted us to print out.

Updated version, no errors but user(0) isn't being printed out.

1 Answers1

1

So far I have created an Array List which only currently consists of one user.

Actually, user0 is never assigned. It's null, so you're only going to be adding null into the list.

You do not need user0 variable.

ArrayList<User> userGroup = new ArrayList<>();
User userOne;

public void addSampleData(String username, String userType, String name) {
    userGroup.add(new User(username, userType, name));
}

Then,

public User getUser(int index)  {
    return userGroup.get(index);
}

If you want the first user, call that method with 0 as the parameter.

As far as this is concerned, it's fine, but see How do I print my Java object without getting "SomeType@2f92e0f4"?

public void printusername(){

   for (User x : userGroup)
       System.out.println(x);
}

Alternatively, if you just want x's username, then print that instead of the entire object

Slightly more advanced option, since the Group essentially is a list of users.

public class UserGroup extends ArrayList<User> {

Or, you don't really need that class unless you add more specialized methods than add, get, and print

Community
  • 1
  • 1
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245