-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.java
More file actions
148 lines (122 loc) · 3.7 KB
/
Person.java
File metadata and controls
148 lines (122 loc) · 3.7 KB
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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
import java.util.Set;
import common.Util;
import common.types.Tuple1;
/** A instance represents a person and their health.
*
* @author Mshnik, revised by gries */
public class Person extends Tuple1<String> {
/** The possible virus-related states of a person. */
public enum State { // The names indicate the state.
HEALTHY,
SICK,
DEAD,
IMMUNE
}
/** The network to this person belongs. */
private final Network graph;
/** Amount of health this person has. >= 0.<br>
* 0 means dead, any other number means alive */
private int health;
/** State of this person. */
private State state;
/** Time step in which this person got sick (-1 if never been sick). */
private int stepGotSick= -1;
/** Time step in which this person got immune (-1 if not immune). */
private int stepGotImmune= -1;
/** Time step in which this person died (-1 if not dead). */
private int stepDied= -1;
/** Constructor: a healthy Person with name n and health h, added to graph g. <br>
* Precondition: The new person is not in g, and their name is distinct from the name of <br>
* any other person in g. */
public Person(String n, int h, Network g) {
// super(StringUtil.toPronounCase(n));
super(n);
health= h;
state= State.HEALTHY;
graph= g;
graph.addVertex(this);
}
/** Return a representation of this Person. */
public @Override String toString() {
return super.toString() + " - " + state;
}
/** Return the name of this person. */
public String name() {
return _1;
}
/** Make this person sick during step currentStep. <br>
* Throw a RuntimeException if this person is not HEALTHY. */
public void getSick(int currentStep) {
if (state != State.HEALTHY) {
throw new RuntimeException(state + " person can't become sick");
}
state= State.SICK;
stepGotSick= currentStep;
}
/** Make this person immune during step currentStep. <br>
* Throw a RuntimeException if this person is immune or dead. */
public void getImmune(int currentStep) {
if (state == State.IMMUNE || state == State.DEAD) {
throw new RuntimeException(state + " person can't become immune");
}
state= State.IMMUNE;
stepGotImmune= currentStep;
}
/** Decrement the health of this person in step currentStep. <br>
* If its health becomes 0, the person dies. <br>
* Throw a RuntimeException if this person is not sick. */
public void reduceHealth(int currentStep) {
if (state != State.SICK) {
throw new RuntimeException(state + " person can't lose health");
}
health-- ;
if (health == 0) {
state= State.DEAD;
stepDied= currentStep;
}
}
/** = the state of this person. */
public State getState() {
return state;
}
/** = "This person is alive". */
public boolean isAlive() {
return state != State.DEAD;
}
/** = "This person is dead". */
public boolean isDead() {
return !isAlive();
}
/** = "This person is healthy. */
public boolean isHealthy() {
return state == State.HEALTHY;
}
/** = "This person is immune". */
public boolean isImmune() {
return state == State.IMMUNE;
}
/** = "This person is sick". */
public boolean isSick() {
return state == State.SICK;
}
/** = the time step in which this person got sick" (-1 if never been sick). */
public int frameGotSick() {
return stepGotSick;
}
/** = the time step in which this person got immune" (-1 if not immune). */
public int frameGotImmune() {
return stepGotImmune;
}
/** = the time step in which this person died" (-1 if not dead). */
public int frameDied() {
return stepDied;
}
/** = the neighbors of this person. */
public Set<Person> neighbors() {
return graph.neighborsOf(this);
}
/** = a random neighbor of this person */
public Person randomNeighbor() {
return Util.randomElement(graph.neighborsOf(this));
}
}