-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathPerson.java
157 lines (146 loc) · 4.55 KB
/
Person.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
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
149
150
151
152
153
154
155
156
157
import java.util.HashMap;
import java.util.ArrayList;
public abstract class Person {
String name;
Integer age;
char gender;
String type; // GrownUp or Child
Integer busyUntil; // based on the timeOn of an appliance until when he will
// use it
boolean taskDone = false; // if the task was found and done
Integer currentStartTime; // the startTime of a task
boolean isBusy; // if the person is performing another action
/**
* The tasks allocated to this person
*/
HashMap<String, Integer> tasks = new HashMap<String, Integer>();
/**
* @param peName
* Person's name
* @param peAge
* Person's Age
* @param peGender
* @param peType
* GrownUp or Child
*/
public Person(String peName, Integer peAge, char peGender, String peType) {
name = peName;
age = peAge;
gender = peGender;
type = peType;
busyUntil = -1;
}
public boolean isAdult() {
return age>=18;
}
/**
*
* @param applianceList
* The list of existing appliance in the house passed from House
* @param currentTask
* The name of the current task
*/
public void turnOnAppliance(ArrayList<Appliance> applianceList, String currentTask) {
for (Appliance appliance : applianceList) {
/*
* It compares the word after "turnon" with an appliance name
* eg.turnonTV
*/
if (currentTask.substring(6).equals(appliance.getName())) {
appliance.turnOn(this); // turns the appliance on
isBusy = true;
taskDone = true;
}
}
}
/**
*
* @param applianceList
* The list of existing appliance in the house passed from House
* @param currentTask
* The name of the current task
*/
public void turnOffAppliance(ArrayList<Appliance> applianceList, String currentTask) {
for (Appliance appliance : applianceList) {
/*
* It compares the word after "turnoff" with an appliance name
* eg.turnoffTV
*/
if (currentTask.substring(7).equals(appliance.getName())) {
appliance.turnOff(this);
isBusy = false;
taskDone = true;
}
}
}
/**
* Matches the name of each task with the use() method in an appliance or
* the turnOn/turnoff for ApplianceOnOff. After it performs this action.
*
* @param time
* Global Time
* @param applianceList
* The list of existing appliance in the house passed from House
*/
public void checkTasks(Integer time, ArrayList<Appliance> applianceList) {
for (String currentTask : tasks.keySet()) {
currentStartTime = tasks.get(currentTask);
if (currentStartTime.equals(time)) { // If the tasks starts at this hour now then it continues
if (currentTask.contains("turnon")) { // if the task is a turnon action
this.turnOnAppliance(applianceList, currentTask);
} else if (currentTask.contains("turnoff")) { // if the task is a turnoff action
this.turnOffAppliance(applianceList, currentTask);
} else { // else the task is a standard task from an appliance
for (Appliance appliance : applianceList) {
if (currentTask.equals(appliance.getTask())) { // It matches the task with an appliance
if (!appliance.getState()) { // if the appliance and the person not busy it uses it
if (busyUntil <= time && !isBusy) {
appliance.use(this);
busyUntil = appliance.getTimeOn() + time; //sets until when the person is going to be busy
taskDone = true;
} else {
System.out.println(this.getName() + " is busy and cannot " + currentTask);
/*
* If the task cannot be started now then it moves its operation
* one time unit into the future until the day ends or the person
* is not busy and there is an available machine
*/
tasks.put(currentTask, currentStartTime + 1);
}
}
}
}
if (!taskDone) { // There is no free machine for the task
System.out.println("There is no avalaible machine for " + currentTask);
tasks.put(currentTask, currentStartTime + 1); // see above
}
}
}
}
}
/**
* The time simulation of Person
*/
public void timePasses(Integer time, ArrayList<Appliance> applianceList) {
if (!tasks.isEmpty()) {
this.checkTasks(time, applianceList);
}
}
/**
* It adds a task to this person
* @param name The name of the task
* @param startTime When the task needs to be started
*/
public void addTask(String name, Integer startTime) {
tasks.put(name, startTime);
}
public void setIsBusy(boolean busy) {
isBusy = busy;
}
public String getName() {
return name;
}
public Integer getAge() {
return age;
}
}