0

In the following piece of code, once executed, it will change the state diet of an object hek based on a random number generator. I also have an undo() function which works perfectly fine in run-time.

What I am trying to do is: When the state of the object is changed but, the previous state prevDiet is equal to the changed state currentDiet, I want the program to call undo().

import java.util.Random;
import javax.swing.JOptionPane;

public class changeDietCom implements Command {

    Hek hek;
    Random rand = new Random();
    int x;
    DietBehaviour prevDiet;
    DietBehaviour currentDiet;

    public changeDietCom(Hek hek) {
        this.hek = hek;
    }

    public void execute() {

        x = rand.nextInt(3) + 1;

        prevDiet = hek.getDietBehaviour();
        if (x == 1) {

            hek.setDietBehaviour(new Enemies());
            JOptionPane.showMessageDialog(null, "Hek's diet has been changed!", "Changing diet", JOptionPane.INFORMATION_MESSAGE);

            currentDiet = hek.getDietBehaviour();
            hek.performDiet();

            if (prevDiet == currentDiet) {
                JOptionPane.showMessageDialog(null, "Diet hasn't changed!", "Current Diet = Prev Diet", JOptionPane.INFORMATION_MESSAGE);
                undo();
            }
        }
        else if (x == 2) {

            hek.setDietBehaviour(new ArmouredKnights());
            JOptionPane.showMessageDialog(null, "Hek's diet has been changed!", "Changing diet", JOptionPane.INFORMATION_MESSAGE);

            currentDiet = hek.getDietBehaviour();
            hek.performDiet();

            if (prevDiet == currentDiet) {
                JOptionPane.showMessageDialog(null, "Diet hasn't changed!", "Current Diet = Prev Diet", JOptionPane.INFORMATION_MESSAGE);
                undo();
            }
        }
        else if (x == 3) {

            hek.setDietBehaviour(new BigMac());
            JOptionPane.showMessageDialog(null, "Hek's diet has been changed!", "Changing diet", JOptionPane.INFORMATION_MESSAGE);

            currentDiet = hek.getDietBehaviour();
            hek.performDiet();

            if (prevDiet == currentDiet) {
                JOptionPane.showMessageDialog(null, "Diet hasn't changed!", "Current Diet = Prev Diet", JOptionPane.INFORMATION_MESSAGE);
                undo();
            }
        }
    }

    public void undo() {
        JOptionPane.showMessageDialog(null, "Undoing diet change...", "Undoing Diet Change", JOptionPane.INFORMATION_MESSAGE);
        hek.setDietBehaviour(prevDiet);
        hek.performDiet();
    }
}

But whenever both the previous state prevDiet and the current state currentDiet are equal to each other during run-time the if statement doesn't return true, and I can't see why not? Could someone please maybe shed some light as to why and how I can fix this?

I should mention that I am using the command pattern for the design of this part of the program.

resueman
  • 10,572
  • 10
  • 31
  • 45
  • 4
    have you tried prevDiet.equals(currentDiet) ? – Bon Nov 19 '15 at 14:31
  • It looks like you're new here, you should know that If an answer works for you its courteous to accept and upvote, if it doesn't work you may be able to comment on it and it will be altered or explained for you. – Kevin Klute Nov 19 '15 at 15:00
  • This answered was useful, but didn't solve my problem. `DietBehaviour` is an interface that I have implemented from and not a concrete class so I'm confused when people are pointing out that I should override the `equals()` method? – PureShpongled Nov 19 '15 at 15:08

2 Answers2

1

DietBehavior objects are object types, so you can't use == to compare them. What you want to use is .equals(). If DietBehavior is your own class then you will have to implement the method, but its pretty simple.

public boolean equals(Object other) {
// compare them here based on instance variables
}
Kevin Klute
  • 450
  • 1
  • 4
  • 22
1

With if (prevDiet == currentDiet) {, you compare object references instead of the actual values. Use if (prevDiet.equals(currentDiet)) { instead, and make sure you override the equals() method of DietBehaviour.

Note that when you override equals(), you should also override hashCode() and the implementations should be consistent. More information about this can be found here.

Community
  • 1
  • 1
Manu
  • 1,474
  • 1
  • 12
  • 18