Alex Simion
Alex Simion

Reputation: 93

Making an equivalence in Java

As you will probably see from my code I'm quite new with this. I am trying to write a simple program to retrieve the friends of one person. Here is my code:

public class Person {
    private String name;
    private String friends;

    public Person(String aName) {
        name = aName;
        friends = "";
    }

    public String getFriends() {
        return friends;
    }

    public void addFriend(Person friend) {
        friends = friends + " " + friend.name; 
    }

    public void unfriend(Person nonFriend) {
        friends = friends.replace(" " + nonFriend.name, "");
    }

    public static void main(String[] args) {
        Person dana = new Person("Dana");
        Person gina = new Person("Gina");
        Person john = new Person("John");

        dana.addFriend(gina);
        dana.addFriend(john);
        john.addFriend(gina);
        john.addFriend(dana);
        john.unfriend(dana);

        System.out.println("Dana's friends are: " + dana.getFriends());
        System.out.println("Gina's friends are: " + gina.getFriends());
        System.out.println("John's friends are: " + john.getFriends());
    }
}

Everything works, but I do not know how to create a method that will say: If Gina is both Dana's and John's friend then clearly Gina's friends will be Dana and John. I know that I can add two lines there gina.addFriend(dana) and gina.addFriend(john), to accomplish the same result, but I would like to know what will the method be for that. Thanks in advance.

Upvotes: 2

Views: 1213

Answers (3)

bloodscript
bloodscript

Reputation: 136

i would suggest to use an arrayList of persons/friends instead of one string.

ArrayList<Person> friends = new ArrayList<Person>; 

you add friends by typing:

public void addFriend(Person newFriend){ 
    friends.add(newFriend);
    newFriend.friends.add(this);
}

you delete friends by typing:

public void unfriend(Person noFriend){
    for(int i = 0; i < this.friends.size(); i++){
        if(this.friends.get(i).name.equals(noFriend.name){
            this.friends.delete(i);
        }
    }
    for(int i = 0; i < noFriend.friends.size(); i++){
        if(noFriend.friends.get(i).name.equals(this.name){
            noFriend.friends.delete(i);
        }
    }
}

to show the whole list of friends:

public void showFriends(){
    for(int i = 0; i < this.friends.size(); i++){
        System.out.println(this.friends.get(i));
    }
}

Upvotes: 0

Matt
Matt

Reputation: 3353

I would use a Set, and add a unique Id to person to get around the problem of multiple people having the same name.

Your class will then look like:

public class Person
{
    private final String personId;
    private final String name;
    private final Set<Person> friends;

     public Person(String personId, String name) {
        super();
        this.personId = personId;
        this.name = name;
        this.friends = new HashSet<Person>();
    }

     public void addFriend(Person friend) {
         if(friend != null && !friends.contains(friend)) {
             this.friends.add(friend);
             // Optional : if it is a two-way relationship that doesn't need approving etc
             friend.addFriend(this);
         }
     }

     public void unfriend(Person nonFriend)
     {
         if(nonFriend != null && friends.contains(nonFriend)) {
             this.friends.remove(nonFriend);
             // Optional : if it is a two-way relationship that doesn't need approving etc
             nonFriend.unfriend(this);
         }
     }

     public Set<Person> getFriends()
     {
         return friends;
     }

    @Override
    public String toString() {
        return "Person [name=" + name + "]";
    }

    public static void main(String[] args)
    {
        Person dana = new Person("D001", "Dana");
        Person gina = new Person("G001", "Gina");
        Person john = new Person("J001", "John");


        dana.addFriend(gina);
        dana.addFriend(john);
        john.addFriend(gina);
        john.addFriend(dana);
        john.unfriend(dana);



        System.out.println("Dana's friends are: "+dana.getFriends());
        System.out.println("Gina's friends are: "+gina.getFriends());
        System.out.println("John's friends are: "+john.getFriends());

    }


    // Equals and Hashcode are very important when using 'contains' and other Set-based methods

    @Override
    public int hashCode() {
        final int prime = 31;
        int result = 1;
        result = prime * result + ((personId == null) ? 0 : personId.hashCode());
        return result;
    }

    @Override
    public boolean equals(Object obj) {
        if (this == obj)
            return true;
        if (obj == null)
            return false;
        if (getClass() != obj.getClass())
            return false;
        Person other = (Person) obj;
        if (personId == null) {
            if (other.personId != null)
                return false;
        } else if (!personId.equals(other.personId))
            return false;
        return true;
    }
}

Upvotes: 2

ostrichofevil
ostrichofevil

Reputation: 734

First of all, make friends an ArrayList<Friend>.

private ArrayList<Friend> friends;

This has several advantages, including being easier to use and storing the Persons themselves rather than just their names.

Then, change the addFriend method to go in both directions:

public void addFriend(Person friend) {
    friends.add(friend);
    friend.friends.add(this);
}

In this way, friendships will automatically go back and forth. You should also change unfriend:

public void unFriend(Person friend) {
    friends.remove(friend);
    friend.friends.remove(this);
}

EDIT: as per a comment above, a set would actually be better, as it can only have one of each value.

Upvotes: 5

Related Questions