Advertisement
cd62131

qa8310825 light system with enum

Oct 18th, 2013
300
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5 2.72 KB | None | 0 0
  1. import java.util.*;
  2. // test class
  3. public class QA8310825 {
  4.   public static void main(String[] args) {
  5.     LightSystem system =
  6.         new LightSystem(new Swich("f1"), new Swich("f2"), new Swich("f3"));
  7.     system.showState();
  8.     system.turnSwich1(); system.showState();
  9.     system.turnSwich1(); system.showState();
  10.     system.turnSwich2(); system.showState();
  11.     system.turnSwich3(); system.showState();
  12.     system.turnSwich2(); system.showState();
  13.     system.turnSwich1(); system.showState();
  14.   }
  15. }
  16. class Swich {
  17.   private enum State {
  18.     Left, Right
  19.   }
  20.   private String floor;
  21.   private State state;
  22.  
  23.   public Swich(String floor) {
  24.     this.floor = floor;
  25.     this.state = State.Left;
  26.   }
  27.  
  28.   public String getFloor() {
  29.     return this.floor;
  30.   }
  31.  
  32.   public State getState() {
  33.     return this.state;
  34.   }
  35.  
  36.   public void toggle() {
  37.     if (this.state == State.Left) {
  38.       this.state = State.Right;
  39.       return;
  40.     }
  41.     this.state = State.Left;
  42.   }
  43.  
  44.   @Override
  45.   public String toString() {
  46.     StringBuilder sb = new StringBuilder();
  47.     sb.append("<Floor:" + this.getFloor());
  48.     sb.append(", " + this.getState().name());
  49.     sb.append(">");
  50.     return sb.toString();
  51.   }
  52. }
  53.  
  54. class LightSystem {
  55.   private enum Light {
  56.     On, Off
  57.   }
  58.   private Map<Integer, Swich> swiches = new TreeMap<Integer, Swich>();
  59.   private Light lamp;
  60.  
  61.   public LightSystem(Swich... s) {
  62.     for (int i = 0; i < s.length; i++) {
  63.       int size = this.swiches.size(); // auto increment
  64.       this.swiches.put(size, s[i]);
  65.     }
  66.     this.lamp = Light.Off;
  67.   }
  68.  
  69.   public Light getLight() {
  70.     return this.lamp;
  71.   }
  72.  
  73.   public void showState() {
  74.     System.out.println(this);
  75.   }
  76.  
  77.   public void turn() {
  78.     if (this.lamp == Light.Off) {
  79.       this.lamp = Light.On;
  80.       return;
  81.     }
  82.     this.lamp = Light.Off;
  83.   }
  84.  
  85.   private Swich find(String name) {
  86.     Swich ret = null;
  87.     for (Swich s : this.swiches.values()) {
  88.       if (s.getFloor().equals(name)) {
  89.         ret = s;
  90.         break;
  91.       }
  92.     }
  93.     return ret;
  94.   }
  95.  
  96.   private boolean turnSwich(Swich swich) {
  97.     Swich s = this.find(swich.getFloor());
  98.     if (s == null) { return false; }
  99.     s.toggle();
  100.     this.turn();
  101.     return true;
  102.   }
  103.  
  104.   public void turnSwich1() {
  105.     this.turnSwich(this.swiches.get(0)); // 0 means first inserts
  106.   }
  107.  
  108.   public void turnSwich2() {
  109.     this.turnSwich(this.swiches.get(1));
  110.   }
  111.  
  112.   public void turnSwich3() {
  113.     this.turnSwich(this.swiches.get(2));
  114.   }
  115.  
  116.   @Override
  117.   public String toString() {
  118.     StringBuilder sb = new StringBuilder();
  119.     sb.append("Lamp: " + this.getLight().name() + ", swiches: ");
  120.     sb.append(this.swiches.values());
  121.     return sb.toString();
  122.   }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement