0

I have a custom Rectangle class:

public class Rectangle () {
      private int height, width, x, y;
      private Color color;
      
      public Rectangle () {
           this.height = null;
           this.width = null;
           this.x = null;
           this.y = null;
           this.color = null;
      }
      
      public void setHeight(int h) { this.height = h; }

      public void setWidth(int w) { this.width = w; }
      
      public void setX(int x) { this.x = x; }
      
      public void setY(int y) { this.y = y; }
       
      public void setColor(Color c) { this.color = c; }

      public int getWidth() { return this.width; }
      
      public int getHeight() { return this.height; }

      public int getX() { return this.x; }
      
      public int getY() { return this.y; }
      
      public Color getColor() { return this.color; }
      
      public void undo() {   }

      public void redo() {   }

}

How would I go about implementing the undo and redo functions for this class in a way that it should be able to revert the rectangle to its previous state without the user mentioning what method was last used. I have a vague idea that involves using a stack but I'm stuck on how to actually code it. My second question is I'm not sure if my constructor is correct, I initialize everything to null without giving any parameters because I want people to use the getters/setters instead. Please help.

Red Apple
  • 99
  • 6
  • Well, as to oyur first question - yes, that involves some kind of stack, possibly two of them (the second for redo). At least conceptually, as you need last-in-first-out mechanics. – Hulk Oct 16 '20 at 05:32
  • Your second question cannot be answered - "correct" depends on the requirements, and we don't know your requirements. – Hulk Oct 16 '20 at 05:33
  • Or perhaps https://stackoverflow.com/questions/40391202/implementing-undo-redo-methods-using-stacks – Hulk Oct 16 '20 at 05:38

1 Answers1

1

you can saave the state in an array and restore the rectangle from it

public class Rectangle () {
    
    private states: List<Rectangle> = new ArrayList(); // we save the state of rectangle on every update to any property
    private int stateIndex = 0; // this is the index of the state which is active on this rectangle
    
    private int height, width, x, y;
    private Color color;
    
    public Rectangle () {
       this.height = null;
       this.width = null;
       this.x = null;
       this.y = null;
       this.color = null;
    }
    
    public void setHeight(int h) { 
        this.stateIndex = states.size() + 1; // we ll increase one in state index as new state is added to the states
        this.height = h; 
        this.states.add(this); // add the state after you set the value of any property
    }
    
    public void undo() { 
        if(this.stateIndex > 0){ // only undo when there is state available before the current state
            this.stateIndex--; // reduce the current index by one
            this.height = this.states.get(stateIndex).height; // set the properties from state
            ...
        }
    }
    
    public void redo() {    
        if(this.stateIndex < states.size()){ // can go more the available states in cache
            this.stateIndex++; // increase the current state index
            this.height = this.states.get(stateIndex).height; // update the values of the properties
            ...
        }
    }

}
Harkal
  • 1,770
  • 12
  • 28
  • 1
    That is possible, but you could also use an [`ArrayDeque`](https://docs.oracle.com/en/java/javase/15/docs/api/java.base/java/util/ArrayDeque.html), which has the usual `push()` and `pop()` methods of a stack. – Hulk Oct 16 '20 at 05:48
  • @Hulk thanks for adding something new to my knowledge. i didnt know that thing existed :) – Harkal Oct 16 '20 at 05:49
  • Well, if you use that, you need a second stack for the redo part. So it doesn't only have advantages here. But you may need to clear the redo part when the user does some other operation after an undo - and this is a bit easier when they are separate. – Hulk Oct 16 '20 at 05:52
  • @Hulk I see that ;) thats another question to be asked by the developer .... lol – Harkal Oct 16 '20 at 05:54