Search code examples
javaoopif-statementstatecommand-pattern

Why won't my comparative if statement of two variables work?


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.


Solution

  • 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
    }