-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBuilding.java
More file actions
212 lines (191 loc) · 5.74 KB
/
Building.java
File metadata and controls
212 lines (191 loc) · 5.74 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
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){
if (this.data == null){
Building returnBuilding = new Building (this.data);
return returnBuilding;
}
if (b.yearOfConstruction<this.data.yearOfConstruction){
if (this.older == null){
this.older = new Building(b);
return this;
}else{
(this.older).addBuilding(b);
}
}else if (b.yearOfConstruction>this.data.yearOfConstruction){
if (this.younger == null){
this.younger = new Building (b);
return this;
}else{
(this.younger).addBuilding(b);
}
}else{
if (b.height>this.data.height){
OneBuilding helper= this.data;
this.data=b;
this.addBuilding(helper);
}else if (this.same == null){
this.same = new Building(b);
return this;
}else{
(this.same).addBuilding(b);
}
}
return this; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
public Building addBuildings (Building b){
if(b.data==null){
return this;
}
this.addBuilding (b.data);
if (b.older != null){
this.addBuildings(b.older);
}
if (b.same != null){
this.addBuildings (b.same);
}
if (b.younger != null){
this.addBuildings(b.younger);
}
return this; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
public Building removeBuilding (OneBuilding b){
if(b==null){
return this;
}
if (this.data.equals(b)) {
if (this.same != null) {
this.data = this.same.data;
this.same = this.same.same;
return this;
} else if (this.older != null){
Building helper = this.younger;
this.data = this.older.data;
this.same = older.same;
this.younger = this.older.younger;
this.older = this.older.older;
if (helper != null){
this.addBuildings(helper);
}
} else {
return this.younger;
}
} else {
if (this.older != null) {
this.older = this.older.removeBuilding(b);
}
if (this.same != null){
this.same = this.same.removeBuilding(b);
}
if (this.younger != null) {
this.younger = this.younger.removeBuilding(b);
}
}
return this;
}
public int oldest(){
if(this.older!=null){
return this.older.oldest();
}
return this.data.yearOfConstruction; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
public int highest(){
if(this.older!=null && this.older.data.height>=this.data.height){
return this.older.highest();
}
if (this.younger!=null && this.younger.data.height>=this.data.height){
return this.younger.highest();
}
return this.data.height; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
public OneBuilding highestFromYear (int year){
if(this.data.yearOfConstruction==year){
return this.data;
}else if (year<this.data.yearOfConstruction && this.older !=null){
return this.older.highestFromYear(year);
}else if (year>this.data.yearOfConstruction && this.younger !=null){
return this.younger.highestFromYear(year);
}else{
return null;
}
// DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
public int numberFromYears (int yearMin, int yearMax){
if (yearMin>yearMax){
return 0;
}
int counter=0;
int diff = yearMax - yearMin;
for (int i=0; i<=diff; i++){
counter= counter + countYear(yearMin+i);
}
return counter; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
// helper for numberFromYears
public int countYear (int year){
int counter=0;
if(this.data.yearOfConstruction==year){
counter++;
if (this.same !=null){
counter= counter + this.same.countYear(year);
}
}else if (year<this.data.yearOfConstruction && this.older !=null){
counter = counter+ this.older.countYear(year);
}else if (year>this.data.yearOfConstruction && this.younger !=null){
counter = counter + this.younger.countYear(year);
}else{
return 0;
}
return counter;
}
public int[] costPlanning (int nbYears){
int array[]=new int[nbYears];
for (int i=0; i<nbYears;i++){
array[i]= costOneYear (2018+i);
}
return array; // DON'T FORGET TO MODIFY THE RETURN IF NEEDS BE
}
// helper for costPlanning
public int costOneYear (int year){
int counter=0;
if(this.data.yearForRepair==year){
counter = counter + this.data.costForRepair;
}
if (this.same !=null){
counter= counter + this.same.costOneYear(year);
}
if (this.older !=null){
counter = counter+ this.older.costOneYear(year);
}
if (this.younger !=null){
counter = counter + this.younger.costOneYear(year);
}
return counter;
}
}