Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Airlock implements AirlockInterface {
- List<State> history;
- Map<State, Integer> usageCounters;
- State state;
- Integer tmpInt;
- State lastIn;
- State lastEx;
- Airlock(){
- history = new ArrayList<State>();
- usageCounters = new HashMap<State, Integer>();
- usageCounters.put(State.EXTERNAL_DOOR_CLOSED, 0);
- usageCounters.put(State.INTERNAL_DOOR_CLOSED, 0);
- usageCounters.put(State.EXTERNAL_DOOR_OPENED, 0);
- usageCounters.put(State.INTERNAL_DOOR_OPENED, 0);
- }
- @Override
- public List<State> getHistory() {
- return this.history;
- }
- @Override
- public Map<State, Integer> getUsageCounters() {
- return this.usageCounters;
- }
- @Override
- public State getState() {
- return this.state;
- }
- @Override
- public void setState(State state) {
- this.state = state;
- if(state == State.INTERNAL_DOOR_OPENED || state == State.INTERNAL_DOOR_CLOSED)
- lastIn = state;
- else
- lastEx = state;
- history.add(state);
- tmpInt = usageCounters.get(state);
- usageCounters.replace(state, new Integer(tmpInt.intValue()+1) );
- System.out.println(history.get(history.size()-1));
- }
- @Override
- public Set<State> newStates() {
- Set<State> tmp = new HashSet<State>();
- if(lastEx == State.EXTERNAL_DOOR_OPENED && lastIn == State.INTERNAL_DOOR_OPENED || state == State.DISASTER || lastIn == State.DISASTER || lastEx == State.DISASTER){
- tmp.add(State.DISASTER);
- return tmp;
- }else if(this.state == State.INTERNAL_DOOR_CLOSED || this.state == State.EXTERNAL_DOOR_CLOSED){
- tmp.add(State.EXTERNAL_DOOR_CLOSED);
- tmp.add(State.INTERNAL_DOOR_CLOSED);
- tmp.add(State.INTERNAL_DOOR_OPENED);
- tmp.add(State.EXTERNAL_DOOR_OPENED);
- return tmp;
- }else if(this.state == State.EXTERNAL_DOOR_OPENED){
- tmp.add(State.EXTERNAL_DOOR_CLOSED);
- tmp.add(State.INTERNAL_DOOR_CLOSED);
- tmp.add(State.DISASTER);
- tmp.add(State.EXTERNAL_DOOR_OPENED);
- return tmp;
- }else if(this.state == State.INTERNAL_DOOR_OPENED){
- tmp.add(State.EXTERNAL_DOOR_CLOSED);
- tmp.add(State.INTERNAL_DOOR_CLOSED);
- tmp.add(State.DISASTER);
- tmp.add(State.INTERNAL_DOOR_OPENED);
- return tmp;
- }
- return null;
- }
- @Override
- public void openExternalDoor() {
- State st = State.EXTERNAL_DOOR_OPENED;
- if(lastIn == State.INTERNAL_DOOR_OPENED)
- st = State.DISASTER;
- lastEx = st;
- history.add(st);
- tmpInt = usageCounters.get(st);
- usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
- System.out.println(history.get(history.size()-1));
- }
- @Override
- public void openInternalDoor() {
- State st = State.INTERNAL_DOOR_OPENED;
- if(lastEx == State.EXTERNAL_DOOR_OPENED)
- st = State.DISASTER;
- lastIn = st;
- history.add(st);
- tmpInt = usageCounters.get(st);
- usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
- System.out.println(history.get(history.size()-1));
- }
- @Override
- public void closeExternalDoor() {
- State st = State.EXTERNAL_DOOR_CLOSED;
- lastEx = st;
- history.add(st);
- tmpInt = usageCounters.get(st);
- usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
- System.out.println(history.get(history.size()-1));
- }
- @Override
- public void closeInternalDoor() {
- State st = State.INTERNAL_DOOR_CLOSED;
- lastIn = st;
- history.add(st);
- tmpInt = usageCounters.get(st);
- usageCounters.replace(st, new Integer(tmpInt.intValue()+1) );
- System.out.println(history.get(history.size()-1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement