VipPunkJoshers Droopy VipPunkJoshers Droopy - 6 months ago 13
Android Question

Dimanic Model in Java

I'm making a contacts app, this is my model.

public class Contact {
private RelationShip relationShip;

public static class RelationShip {
private Friend friend;
private Enemie enemie;
private Family family;

private class Family {
private Brother brother;
private Sister sister;
}

}
}


I want to create it you can do it the way suiguiente.

RelationShip realation = new RelationShip(RelationShip.Friend);

Contact contact = new Contact(realation);


I want to establish the kind of relationship in a variable. Really do not know how to ask the question. an example would be something like this:

layout.setOrientation (LinearLayout.VERTICAL)
;

Answer

LinearLayout.VERTICAL is a constant or an enumerate. Take a look here for example.

To achieve the same behavior in your code set those values as constants (usually int).

public static class RelationShip {
    public static final int FRIEND = 0;
    public static final int ENEMIE = 1;
    public static final int FAMILY = 2;

    ...

    }

public class Contact {
    private int relationshipType;

    ...

    public void setRelationShipType(int relationShip) {

    }

    public boolean areWeFriends() {
        if (relationshipType==Relationship.FRIEND)
            return true;
        else
            return false;
    }

    ...
}

You can then set the relationship setRelationShipType(Relationship.FRIEND). Or check if the relationship of Contact HerryPotter is a friend with HarryPotter.areWeFriends()

EDIT:

Actually the correct way is indeed the one suggested by alfasin: using enumerates.

public enum RelationShip {
    FRIEND, ENEMIE, FAMILY
    }

public class Contact {
    private RelationShip relationshipType;

    public void setRelationShipType(RelationShip relationShip) {
        ...
    }

    public boolean areWeFriends() {
        if (relationshipType==Relationship.FRIEND)
            return true;
        else
            return false;
    }
}