Ragnar
Ragnar

Reputation: 13

How to use a boolean in an "else if" directed to objects from an ArrayList

Ok hello there. To begin with, excuse me if my English looks poor at any point; not really my mother tongue. Also, excuse me for any horrible coding mistakes but i am a beginner in Java/coding.

So let me explain what's the problem and hopefully you can help me:

There is a simplified Social Network that has Users and Groups. Users can also become friends.

There are regular groups and closed groups (sub-class of Groups). The difference is that in closed groups, you can only join if: 1) you are the first member of the group to join or 2) if you have a friend in the group already (in this case obviously someone has already joined the group before you so condition #1 fails).

Here's the thing. Essentially, every user has an ArrayList to store his friends (named "friendList"). There is also an ArrayList in class "Group" to store the users that are enrolled in the group (named "members").

When it comes to ClosedGroups, i have an "if" to check if the ArrayList of the group is empty. If it is, then i add the user. From then on, i need to check if the user that wants to join exists in the friendlist of an already existing member, and this is where i don't really know what to do. The "addMember" method of class "ClosedGroup" goes as stated below.

public void addMember(User aUser){
    if(members.isEmpty()){
        members.add(aUser);
        aUser.addGroup(this);
        System.out.println(aUser.getName() + " has successfully been enrolled in group " + this.getName());
    }
    else if(){
        members.add(aUser);
        aUser.addGroup(this);
        System.out.println(aUser.getName() + " has successfully been enrolled in group " + this.getName());
    }
    else{
        System.out.println("FAILED: " + aUser.getName() + " cannot be enrolled in group " + this.getName());
    }

So, what exactly do i have to type in this "else if" ?

Additionally, it is asked to have a method in class "User" that will return true (if 2 users are friends) or false, which i am also probably doing wrong.

Help, kind devs.

Thanks in advance.

EDIT: Disjoint worked like a charm. Thank you very much for the suggestion guys!

Upvotes: 1

Views: 64

Answers (3)

GhostCat
GhostCat

Reputation: 140427

You need something like:

List<User> friends = aUser.getFriends();
List<User> registeredMembers = members ...

Now the check is a simple:

if (Collections.disjoint(friends, registeredMembers) {
  ... no friends in group

More info about the method I used can be found here.

And hint: you do not want to put all those checks into single method. Instead, you should create small helper methods to check such conditions. Like:

private boolean hasUserFriendsInThisGroup(User userToCheck) {
  return ... // result based on check as above

And: doing a printout is not sufficient. You see, when your operation fails, you only notice if you pay close attention to your messages. And that is not very reliable. Instead, you want to make sure that the code that calls that method understands that something went wrong. For example, you could throw an exception to indicate "the operation failed". So that the code using that method ... has a chance to put up a proper error message to the user for example!

Upvotes: 1

Matthew Brzezinski
Matthew Brzezinski

Reputation: 1794

What you would need to do is something similar to this:

public void addMember(User aUser){
    if(members.isEmpty()){
    }
    else if(friendInGroup(aUser)){
      // The user has a friend in their group, add them to the group here
    }
    else{
    }
}

Then your friendInGroup(User aUser) would look like,

private boolean friendInGroup(User aUser) {
    // loop through all aUsers friends
    // check if any of these friends are in the group
    // if they are in the group, return true
    // else return false
}

Upvotes: 0

Joe
Joe

Reputation: 800

Check out the disjoint method of the Collections class: http://docs.oracle.com/javase/6/docs/api/java/util/Collections.html#disjoint%28java.util.Collection,%20java.util.Collection%29

This will return true if there are no elements the same.

if(!Collections.disjoint(members, aUser.getFriends()))

Upvotes: 2

Related Questions