-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColony.java
More file actions
106 lines (96 loc) · 2.69 KB
/
Copy pathColony.java
File metadata and controls
106 lines (96 loc) · 2.69 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
import java.util.ArrayList;
public class Colony {
public ArrayList<Resurce> res = new ArrayList<>();
public ArrayList<Build> building = new ArrayList<>();
public String name;
public Colony(String temp){
name = temp;
AddResorce(new Electric());
AddResorce(new Matter());
AddResorce(new People());
}
public String toString() {
String result = name + "\n";
for(int i = 0; i < res.size(); i++) {
result += res.get(i).getClass().getSimpleName() + " " + res.get(i).count + "\n";
}
result += "\n";
for(int i = 0; i < building.size(); i++) {
result += building.get(i).getClass().getSimpleName() + " " + building.get(i).name + "\n";
}
return result;
}
@Override
public boolean equals(Object obj) {
if(obj == null) return false;
if(obj instanceof Colony) {
Colony temp = (Colony) obj;
if(!temp.name.equals(name)) return false;
return true;
}
return false;
}
public void AddResorce(Resurce r){
synchronized(res) {
if(!res.contains(r)){
res.add(r);
}
}
}
public Resurce GetResurce(String name){
for (Resurce temp : res) {
if(temp.getClass().getSimpleName()==name) {
return temp;
}
}
return null;
}
public void DelResorce(Resurce r){
synchronized(res) {
if(r != null){
res.remove(r);
}
}
}
public boolean CreateBuilding(Build b){
synchronized(building) {
if(!building.contains(b)){
if(b.getClass().getSimpleName() == "MatterExtractor" && GetResurce("Matter").count >= b.cost
&& GetResurce("People").count >= b.cost) {
GetResurce("Matter").count += -b.cost;
GetResurce("People").count += -b.cost;
building.add(b);
return true;
}
if(b.getClass().getSimpleName() == "PowerStation" && GetResurce("Electric").count >= b.cost
&& GetResurce("People").count >= b.cost) {
GetResurce("Electric").count += -b.cost;
GetResurce("People").count += -b.cost;
building.add(b);
return true;
}
if(b.getClass().getSimpleName() == "House") {
GetResurce("People").count += 300;
building.add(b);
return true;
}
}
}
return false;
}
public void DelBuilding(Build b){
synchronized(building) {
if(b != null) {
building.remove(b);
}
}
}
public Build GetBuilding(String name){
for(int i = 0;i < building.size(); i++){
if(building.get(i).name.equals(name)){
return building.get(i);
}
}
return null;
}
}