tags:

views:

65

answers:

7

When person 1 become partner with person 3, person 2 should no longer have person 1 as partner and person 4 should no longer have person 3 as partner. How should I solve this?

public class Person {

    private String name;
    private Person partner;

    public Person(String name){
        this.name = name;
    }

    public void setPartner(Person partner){
        this.partner = partner;
        partner.partner = this; 
    }

    public static void main(String[] args) {

        Person one = new Person("1");           
        Person two = new Person("2");
        Person three = new Person("3");
        Person four = new Person("4");

        one.setPartner(two);
        three.setPartner(four);

        one.setPartner(three);

        //Person two is still partner with person 1
        //and person four is still partner with person 3 
   }
+1  A: 

I think putting this as the first line in setPartner should work: this.partner.partner = null;

Of course you must check if this.partner is null or not.

Petar Minchev
+2  A: 
public void setPartner(Person partner){
    if (this.partner != null) {
        this.partner.partner = null; // Reset the partner of the old partner.
    }
    this.partner = partner;          // Assign new partner.
    this.partner.partner = this;     // Set the partner of the new partner.
}
BalusC
OMG and partnership:) 8 occurences of partner here:)
Petar Minchev
...not counting the comments.
BalusC
@BalusC Good point, I missed that:)
Petar Minchev
In this case, the original this.partner.partner.partner (oh yeah, that's even more partners), will still be set to this.partner.partner, won't it?
Chris Henry
You would make it much more clear if you had used newPartner instead of reusing the term partner. Would have also, as a side effect, allowed you to remove those unnecessary, hard to read "this."
Bill K
Oh, also, why are you doing the guys homework for him?
Bill K
How would you have answered it, @Bill ?
BalusC
@BalusC I usually try to ask him leading questions when it's an obvious homework problem--at best I'll give him some psuedo code--"First see if there is a current partner, and if so reset it's partner, then set the new partner and the new partner's partner". Something along those lines. Depends on how much he's already done. This guy had done quite a bit so I probably would have gone with the psuedo code. I only commented here because I like to remind people to keep an eye out for questions that sound homeworky.
Bill K
Yes exactly, it was already tagged homework and the OP has already done a lot himself. I normally don't post code to homework questions without code :) Explore the history if you want.
BalusC
This is not homework Bill, some admin tagged it. I made a case out of the problem, because I did not know how to question the problem in words only.
Skogen
+1  A: 

change setPartner to:

public void setPartner(Person partner){
    if(this.partner != null)
        this.partner.partner = null;
    this.partner = partner;
    partner.partner = this; 
}
Stephan
+1  A: 

I would suggest that you set up a setRelationship method, which would action setPartner on the current Person, and action a new removePartner on the old partner, if not null.

The new setRelationship method would be in place so that there would be no confusion as to what setPartner does - there would be no side-effects that might be missed by the unsuspecting programmer.

akf
I've never seen "action" used as a verb like that. Is there any subtle difference to "call" that I'm not aware of?
Joachim Sauer
No, just trying to spice it up a bit. I can replace 'action' with 'call' if you'd like.
akf
+1 because the `setPartner()` doesn't document that it does any additional methods and logics. How am I supposed to know as a user thta the `setPartner()` will calculate PI ? Should be made more visible whate exactly is happening
Leni Kirilov
A: 

Here is my Code:

public void setPartner(Person partner) {
    if (this.partner != null)
       this.partner.partner = null;
    this.partner = partner;
    if (partner.partner != null)
       partner.partner.partner = null;
    partner.partner = this;
}
ablaeul
+3  A: 
public void setPartner(Partner b) {
  // Special case, otherwise we'll have troubles
  // when this.partner is already b.
  if (this.partner == b) return;

  if (this.partner != null) {
    this.partner.partner = null;
  }

  this.partner = b;

  // Make sure that the new partner has the right partner.
  // This will make sure the original b.partner has its
  // partner field nullified.
  // Note that if we don't have the special case above,
  // this will be an infinite recursion.
  b.setPartner(this);
}
Chris Henry
you said: this.partner.partner = null;but the partner is a PRIVATE field... please fix it with a set/get methods
Leni Kirilov
Actually, since they are the same class Java will allow you to manipulate a PRIVATE field from another instance of the same class--strange but true. Not that it's a great idea, but in this case it might be another way to avoid the ugly recursion that @Ablaeul hit. (This solution also avoids it nicely)
Bill K
@Leni I would have loved to use setPartner too, but it's just not possible without creating another helper method (like removePartner). Calling setPartner(null) on the original partner will cause havoc. Anyway, like Bill said, we can access private field as long as it is the same class, no need to be the same object.
Chris Henry
This private.private is strange to me. I didn't know it works. I will dig through the books/specs why this is made, because I can't see an obvious reason.
Leni Kirilov
A: 
class Person {

  String name;
  Partnership partnership;
  void setPartnership(Partnership p) {
    partnership=p;
  }
}
class Partnership {
  Person partner1;
  Person partner2;
  public setPartners(Person p1,Person p2) {
    p1.setPartnership(this);
    p2.setPartnership(this);
}

Ideally you would want a way to prevent setPartnership being called from anywhere other than Partnership.

DJClayworth