-
Notifications
You must be signed in to change notification settings - Fork 94
/
Gate.java
61 lines (52 loc) · 1.38 KB
/
Gate.java
1
2
3
4
5
6
7
8
9
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
package mooc.vandy.java4android.gate.logic;
/**
* This file defines the Gate class.
*/
public class Gate {
public static final int IN = 1;
public static final int OUT = -1;
public static final int CLOSED = 0;
private int mSwing;
public Gate(){
mSwing = CLOSED;
}
public boolean setSwing(int direction) {
if(direction == IN || direction == OUT || direction == CLOSED) {
mSwing = direction;
return true;
}
return false;
}
public boolean open(int direction) {
if (direction == IN || direction == OUT) {
this.setSwing(direction);
return true;
}
return false;
}
public void close() {
mSwing = 0;
}
public int getSwingDirection() {
return mSwing;
}
public int thru(int count) {
if(mSwing == IN)
return count;
else if(mSwing == OUT)
return - count;
else
return 0;
}
@Override
public String toString() {
if(mSwing ==0)
return "This gate is closed";
else if(mSwing == IN)
return "This gate is open and swings to enter the pen only";
else if (mSwing == OUT)
return "This gate is open and swings to exit the pen only";
else
return "This gate has an invalid swing direction";
}
}