01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
package myproject.model.text;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import myproject.model.AnimatorBuilder;
import myproject.model.Car;
import myproject.model.Light;
import myproject.model.Road;
import myproject.util.Animator;

/**
 * A class for building Animators.
 */
public class TextAnimatorBuilder implements AnimatorBuilder {
  TextAnimator animator;
  public TextAnimatorBuilder() {
    this.animator = new TextAnimator();
  }
  public Animator getAnimator() {
    if (animator == null) { throw new IllegalStateException(); }
    Animator returnValue = animator;
    animator = null;
    return returnValue;
  }
  public void addLight(Light d, int i, int j) {
    animator.addLight(d,i,j);
  }
  public void addHorizontalRoad(Road l, int i, int j, boolean eastToWest) {
    animator.addRoad(l,i,j);
  }
  public void addVerticalRoad(Road l, int i, int j, boolean southToNorth) {
    animator.addRoad(l,i,j);
  }


  /** Class for drawing the Model. */
  private static class TextAnimator implements Animator {

    /** Triple of a model element <code>x</code> and coordinates. */
    private static class Element<T> {
      T x;
      int i;
      int j;
      Element(T x, int i, int j) {
        this.x = x;
        this.i = i;
        this.j = j;
      }
    }

    private List<Element<Road>> roadElements;
    private List<Element<Light>> lightElements;
    TextAnimator() {
      roadElements = new ArrayList<Element<Road>>();
      lightElements = new ArrayList<Element<Light>>();
    }
    void addLight(Light x, int i, int j) {
      lightElements.add(new Element<Light>(x,i,j));
    }
    void addRoad(Road x, int i, int j) {
      roadElements.add(new Element<Road>(x,i,j));
    }

    public void dispose() { }

    public void update(Observable o, Object arg) {
      for (Element<Light> e : lightElements) {
        System.out.print("Light at (" + e.i + "," + e.j + "): ");
        if (e.x.getState()) {
          System.out.println("Blue");
        } else {
          System.out.println("Yellow");
        }
      }
      for (Element<Road> e : roadElements) {
        for (Car d : e.x.getCars()) {
          System.out.print("Road at (" + e.i + "," + e.j + "): ");
          System.out.println("Car at " + d.getPosition());
        }
      }
    }
  }
}