Repository navigation
Expand file tree
/
Copy pathBuilding.java
More file actions
255 lines (222 loc) · 6.64 KB
/
Copy pathBuilding.java
File metadata and controls
255 lines (222 loc) · 6.64 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
package assignment3;
public class Building {
OneBuilding data;
Building older;
Building same;
Building younger;
public Building(OneBuilding data){
this.data = data;
this.older = null;
this.same = null;
this.younger = null;
}
public String toString(){
String result = this.data.toString() + "\n";
if (this.older != null){
result += "older than " + this.data.toString() + " :\n";
result += this.older.toString();
}
if (this.same != null){
result += "same age as " + this.data.toString() + " :\n";
result += this.same.toString();
}
if (this.younger != null){
result += "younger than " + this.data.toString() + " :\n";
result += this.younger.toString();
}
return result;
}
public Building addBuilding (OneBuilding b){
Building nb = new Building(b);
if (b.yearOfConstruction == this.data.yearOfConstruction)
{
if (b.height > this.data.height) //change the root
{
Building temp = new Building(this.data);
temp.younger = this.younger; temp.older = this.older; temp.same = this.same;
this.older = this.younger = this.same = null;
this.data = b; this.younger = nb.younger; this.older = nb.older;
this.same = temp; this.younger = temp.younger; this.older = temp.older;
temp.younger = temp.older = null;
}
else if ((b.height < this.data.height) && (this.same == null))
{
this.same = nb;
}
else if ((b.height <= this.data.height) && (this.same != null))
{
if (this.same.data.height < b.height)
{
Building temp = new Building(this.same.data);
temp.younger = this.same.younger; temp.older = this.same.older; temp.same = this.same.same;
this.same.older = this.same.younger = this.same.same = null;
nb.younger = temp.younger; nb.older = temp.older;
this.same = nb;
nb.same = temp;
temp.younger = temp.older = null;
}
else
this.same = this.same.addBuilding(b);
}
}
else if (b.yearOfConstruction > this.data.yearOfConstruction)
{
if (this.younger == null)
this.younger = nb;
if (this.younger != null)
this.younger = this.younger.addBuilding(b);
}
else if (b.yearOfConstruction < this.data.yearOfConstruction)
{
if (this.older == null)
this.older = nb;
if (this.older != null)
this.older = this.older.addBuilding(b);
}
return this;
}
public Building addBuildings (Building b){
this.addBuilding(b.data);
if (b.younger != null)
this.addBuildings(b.younger);
if (b.older != null)
this.addBuildings(b.older);
if (b.same != null)
this.addBuildings(b.same);
return this;
}
public Building removeBuilding (OneBuilding b){
if (this.findBuilding(b) == null)
return this;
Building temp = new Building(this.data);
temp.younger = this.younger; temp.older = this.older; temp.same = this.same;
if (b.yearOfConstruction < temp.data.yearOfConstruction)
temp.older = temp.older.removeBuilding(b);
else if (b.yearOfConstruction > temp.data.yearOfConstruction)
temp.younger = temp.younger.removeBuilding(b);
else if ((b.yearOfConstruction == temp.data.yearOfConstruction) && (b.equals(temp.data) == false))
temp.same = temp.same.removeBuilding(b);
else if (b.equals(temp.data))
{
if (temp.same != null)
{
if (temp.younger != null)
temp.same = temp.same.addBuildings(temp.younger);
if (temp.older != null)
temp.same = temp.same.addBuildings(temp.older);
temp = temp.same;
}
else if ((temp.same == null) && (temp.older != null))
{
if (temp.younger != null)
temp.older = temp.older.addBuildings(temp.younger);
temp = temp.older;
}
else if ((temp.same == null) && (temp.older == null))
temp = temp.younger;
return temp;
}
return temp;
}
public int oldest(){
if (this.older == null)
return this.data.yearOfConstruction;
else
return this.older.oldest();
}
public int highest(){
int currentHeight = this.data.height;
int youngerHeight;
int olderHeight;
if (this.older != null)
{
olderHeight = this.older.highest();
if (olderHeight >= currentHeight)
currentHeight = olderHeight;
}
if (this.younger != null)
{
youngerHeight = this.younger.highest();
if (youngerHeight >= currentHeight)
currentHeight = youngerHeight;
}
return currentHeight;
}
public OneBuilding highestFromYear (int year){
if ((year > this.data.yearOfConstruction) && (this.younger != null))
return this.younger.highestFromYear(year);
else if ((year < this.data.yearOfConstruction) && (this.older != null))
return this.older.highestFromYear(year);
else if (year == this.data.yearOfConstruction)
return this.data;
return null;
}
public int numberFromYears (int yearMin, int yearMax){
if (yearMin > yearMax)
return 0;
int year = 0;
for (int i=yearMin; i<=yearMax; i++)
year += this.numberFromYear(i);
return year;
}
public int[] costPlanning (int nbYears){
int[] cost = new int[nbYears];
if ((this.data.yearForRepair<=(2018+nbYears-1)) && (this.data.yearForRepair>=2018))
cost[this.data.yearForRepair-2018] += this.data.costForRepair;
if (this.older != null)
{
for (int i=0; i<nbYears; i++)
cost[i] += this.older.costPlanning(nbYears)[i];
}
if (this.same != null)
{
for (int i=0; i<nbYears; i++)
cost[i] += this.same.costPlanning(nbYears)[i];
}
if (this.younger != null)
{
for (int i=0; i<nbYears; i++)
cost[i] += this.younger.costPlanning(nbYears)[i];
}
return cost;
}
public Building findBuilding (OneBuilding b)
{
if (b.yearOfConstruction == this.data.yearOfConstruction)
{
if (b.equals(this.data))
return this;
else if (this.same != null)
return this.same.findBuilding(b);
else if (this.same == null)
return null;
}
else if (b.yearOfConstruction > this.data.yearOfConstruction)
{
if (this.younger != null)
return this.younger.findBuilding(b);
else
return null;
}
else if (b.yearOfConstruction < this.data.yearOfConstruction)
{
if (this.older != null)
return this.older.findBuilding(b);
else
return null;
}
return this;
}
public int numberFromYear (int year){
int counter = 0;
if (this.younger != null)
counter += this.younger.numberFromYear(year);
if (this.older != null)
counter += this.older.numberFromYear(year);
if (this.same != null)
counter += this.same.numberFromYear(year);
if (year == this.data.yearOfConstruction)
counter += 1;
return counter;
}
}