Advertisement
Guest User

Untitled

a guest
Dec 13th, 2017
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.99 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Airlock implements AirlockInterface {
  4.  
  5.     List<State> history;
  6.     Map<State, Integer> usageCounters;
  7.     State state;
  8.     Integer tmpInt;
  9.  
  10.     State lastIn;
  11.     State lastEx;
  12.  
  13.     Airlock(){
  14.         history = new ArrayList<State>();
  15.         usageCounters = new HashMap<State, Integer>();
  16.         usageCounters.put(State.EXTERNAL_DOOR_CLOSED, 0);
  17.         usageCounters.put(State.INTERNAL_DOOR_CLOSED, 0);
  18.         usageCounters.put(State.EXTERNAL_DOOR_OPENED, 0);
  19.         usageCounters.put(State.INTERNAL_DOOR_OPENED, 0);
  20.     }
  21.  
  22.     @Override
  23.     public List<State> getHistory() {
  24.         return this.history;
  25.     }
  26.  
  27.     @Override
  28.     public Map<State, Integer> getUsageCounters() {
  29.         return this.usageCounters;
  30.     }
  31.  
  32.     @Override
  33.     public State getState() {
  34.         return this.state;
  35.     }
  36.  
  37.     @Override
  38.     public void setState(State state) {
  39.         this.state = state;
  40.  
  41.         if(state == State.INTERNAL_DOOR_OPENED || state == State.INTERNAL_DOOR_CLOSED)
  42.             lastIn = state;
  43.         else
  44.             lastEx = state;
  45.  
  46.         history.add(state);
  47.         tmpInt = usageCounters.get(state);
  48.         usageCounters.replace(state, new Integer(tmpInt.intValue()+1) );
  49.         System.out.println(history.get(history.size()-1));
  50.     }
  51.  
  52.     @Override
  53.     public Set<State> newStates() {
  54.         Set<State> tmp = new HashSet<State>();
  55.         if(lastEx == State.EXTERNAL_DOOR_OPENED && lastIn == State.INTERNAL_DOOR_OPENED || state == State.DISASTER || lastIn == State.DISASTER || lastEx == State.DISASTER){
  56.             tmp.add(State.DISASTER);
  57.             return tmp;
  58.         }else if(this.state == State.INTERNAL_DOOR_CLOSED || this.state == State.EXTERNAL_DOOR_CLOSED){
  59.             tmp.add(State.EXTERNAL_DOOR_CLOSED);
  60.             tmp.add(State.INTERNAL_DOOR_CLOSED);
  61.             tmp.add(State.INTERNAL_DOOR_OPENED);
  62.             tmp.add(State.EXTERNAL_DOOR_OPENED);
  63.             return tmp;
  64.         }else if(this.state == State.EXTERNAL_DOOR_OPENED){
  65.             tmp.add(State.EXTERNAL_DOOR_CLOSED);
  66.             tmp.add(State.INTERNAL_DOOR_CLOSED);
  67.             tmp.add(State.DISASTER);
  68.             tmp.add(State.EXTERNAL_DOOR_OPENED);
  69.             return tmp;
  70.         }else if(this.state == State.INTERNAL_DOOR_OPENED){
  71.             tmp.add(State.EXTERNAL_DOOR_CLOSED);
  72.             tmp.add(State.INTERNAL_DOOR_CLOSED);
  73.             tmp.add(State.DISASTER);
  74.             tmp.add(State.INTERNAL_DOOR_OPENED);
  75.             return tmp;
  76.         }
  77.         return null;
  78.     }
  79.  
  80.     @Override
  81.     public void openExternalDoor() {
  82.         State st = State.EXTERNAL_DOOR_OPENED;
  83.         if(lastIn == State.INTERNAL_DOOR_OPENED)
  84.             st = State.DISASTER;
  85.         lastEx = st;
  86.         history.add(st);
  87.         tmpInt = usageCounters.get(st);
  88.         usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
  89.         System.out.println(history.get(history.size()-1));
  90.     }
  91.  
  92.     @Override
  93.     public void openInternalDoor() {
  94.         State st = State.INTERNAL_DOOR_OPENED;
  95.         if(lastEx == State.EXTERNAL_DOOR_OPENED)
  96.             st = State.DISASTER;
  97.         lastIn = st;
  98.         history.add(st);
  99.         tmpInt = usageCounters.get(st);
  100.         usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
  101.         System.out.println(history.get(history.size()-1));
  102.     }
  103.  
  104.     @Override
  105.     public void closeExternalDoor() {
  106.         State st = State.EXTERNAL_DOOR_CLOSED;
  107.         lastEx = st;
  108.         history.add(st);
  109.         tmpInt = usageCounters.get(st);
  110.         usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
  111.         System.out.println(history.get(history.size()-1));
  112.     }
  113.  
  114.     @Override
  115.     public void closeInternalDoor() {
  116.         State st = State.INTERNAL_DOOR_CLOSED;
  117.         lastIn = st;
  118.         history.add(st);
  119.         tmpInt = usageCounters.get(st);
  120.         usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
  121.         System.out.println(history.get(history.size()-1));
  122.     }
  123. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement