title: How could we implement a garage door keeping track of what was its last moving direction?
body: <p>I am doing the following programming exercise: <a href="https://www.codewars.com/kata/58b1ae711fcffa34090000ea" rel="nofollow noreferrer">Killer Garage Door</a>. The statement is:</p>
  
  <blockquote>
    <p>Situation</p>
    
    <p>You have been hired by a company making electric garage doors.
    Accidents with the present product line have resulted in numerous
    damaged cars, broken limbs and several killed pets. Your mission is to
    write a safer version of their controller software. Specification</p>
    
    <p>We always start with a closed door. The remote control has exactly one
    button, with the following behaviour.</p>
  
  <pre><code>If the door is closed, a push starts opening the door, and vice-versa
  It takes 5 seconds for the door to open or close completely
  While the door is moving, one push pauses movement, another push resumes movement in the same direction
  </code></pre>
    
    <p>In order to make the door safer, it has been equiped with
    resistance-based obstacle detection. When the door detects an
    obstacle, it must immediately reverse the direction of movement. Input</p>
    
    <p>A string where each character represents one second, with the
    following possible values.</p>
  
  <pre><code>'.' No event
  'P' Button has been pressed
  'O' Obstacle has been detected (supersedes P)
  </code></pre>
    
    <p>As an example, '..P....' means that nothing happens for two seconds,
    then the button is pressed, then no further events. Output</p>
    
    <p>A string where each character represents one second and indicates the
    position of the door (0 if fully closed and 5 fully open). The door
    starts moving immediately, hence its position changes at the same
    second as the event. Example</p>
    
    <p>..P...O..... as input should yield 001234321000 as output</p>
  </blockquote>
  
  <p>I have written the following code:</p>
  
  <pre><code>public class Door {
    public static String run(String events) {
      System.out.println("\n\n\nevents: "+events);
      int count = 0;
      StringBuilder result = new StringBuilder();
      boolean movingUp = false;
      boolean movingDown = false;
  
      for(char c : events.toCharArray()){
        System.out.println("movingUp: "+movingUp);
        System.out.println("movingDown: "+movingDown);
        System.out.println("c: "+c);
        System.out.println("count: "+count);
        System.out.println("result: "+result);
        if(c=='.'){
          if(movingUp){
            result.append(count &lt; 5 ? ++count : 5);
          }else if(movingDown){
            result.append(count &gt; 0 ? --count : 0);
          }else{
            result.append(count);
          }
        }else if(c=='P'){
          if(count==5){
            movingUp = false;
            movingDown = true;
            result.append(count &gt; 0 ? --count : 0);
          }else if(movingUp){
            movingUp = false;
            result.append(count);
          }else if(movingDown){
            movingDown = false;
            result.append(count);
          }else{
            movingUp = true;  
            result.append(count &lt; 5 ? ++count : 5);
          }
        }else if(c=='O'){
          movingUp = false;
          movingDown = true;
          result.append(count &gt; 0 ? --count : 0);
        }
      }
      return result.toString();
    }
  }
  </code></pre>
  
  <p>I was wondering how could we handle, when there is a pause and then the door resumes again, to be able to keep it going up or down, as it was doing before the pause?</p>
  
  <p>Here are some tests, the ones marked with →, are the ones where the pause or obstacle behaviour fails because of currently the code assumes that after a pause it should go upwards, and after an obstacle it should go downwards.</p>
  
  <pre><code>import static org.junit.Assert.*;
  
  import org.junit.Test;
  
  public class MainTest {
    @Test
    public void testNormalOperation() {
      test("should stay closed unless button is pressed (no buttonpresses)", "..........", "0000000000");
      test("should start opening on buttonpress", "P..", "123");
      test("should open completely and stay open", "P....", "12345");
    }
  
    @Test
    public void testPause() {
      test("should start opening and pause on second buttonpress", "P.P..", "12222");
      test("→ should resume closing on third buttonpress", ".....P......P.P..P....", "0000012345554333321000");
    }
  
    @Test
    public void testObstacles() {
      test("should reverse while opening", "P.O....", "1210000");
      test("→ should reverse while closing", "P.O....", "12345554345555");
    }
  
    @Test
    public void testObstaclePlusPause () {
      test("→ should reverse while opening (and allow pause)", "P..OP..P..", "1232222100");
    }
  
    @Test
    public void testExample() {
      test("should start opening and reverse when obstacle", "..P...O.....", "001234321000");
    }
  
    private void test(String description, String events, String result) {
      assertEquals(description ,result, Door.run(events));
    }
  }
  </code></pre>
  
  <p>I have also read:</p>
  
  <ul>
  <li><a href="https://users.cs.jmu.edu/bernstdh/web/common/lectures/summary_state_pattern.php" rel="nofollow noreferrer">https://users.cs.jmu.edu/bernstdh/web/common/lectures/summary_state_pattern.php</a></li>
  </ul>
  
  <p>How could we implement a garage door keeping track of what was its last moving direction?</p>