-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathColony.java
More file actions
108 lines (91 loc) · 3.66 KB
/
Copy pathColony.java
File metadata and controls
108 lines (91 loc) · 3.66 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
import java.awt.geom.Point2D;
import java.util.Collections;
import java.util.HashMap;
import java.util.Map;
public class Colony implements Identifiable {
private final String colony_id; // e.g. C001
private String name;
private final Point2D location; // Coordinates of colony's location
private int riskFactor; // 1-5: 1 safest and 5 the most dangerous
private HashMap<String, Resource> inventory = new HashMap<>();
private static int nextId = 1;
public Colony(String name, Point2D location, int riskFactor) {
if (riskFactor < 1 || riskFactor > 5) {
throw new IllegalArgumentException("Risk factor must be between 1 and 5");
}
this.colony_id = "C" + String.format("%03d", nextId++); // generate colony's unique id when created
this.location = location;
this.name = name;
this.riskFactor = riskFactor;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Point2D getLocation() {
return location;
}
public int getRiskFactor() {
return riskFactor;
}
public void setRiskFactor(int riskFactor) {
if (riskFactor < 1 || riskFactor > 5) {
throw new IllegalArgumentException("Risk factor must be between 1 and 5");
}
this.riskFactor = riskFactor;
}
@Override
public String getId() {
return colony_id;
}
// Add resources to the inventory
public void addResource(Resource resource) {
String key = generateKey(resource);
if (inventory.containsKey(key)) { // Add the resource to inventory if it is already in the inventory
inventory.get(key).addAmount(resource.getAmount());
} else { // Put the resource in inventory
inventory.put(key, resource.copy());
}
}
// Remove resources from the inventory
public void removeResource(Resource resource) {
String key = generateKey(resource);
if (!inventory.containsKey(key)) { // Check if inventory does not have that item
throw new IllegalArgumentException(resource + " does not exist!");
}
Resource existing = inventory.get(key);
if (resource.getAmount() >= existing.getAmount()) {
// Remove the resource entirely if requested amount >= stored amount
inventory.remove(key);
} else {
// Otherwise, just decrease the amount
existing.removeAmount(resource.getAmount());
}
}
// Check whether the colony's inventory contains at least "amount" of that resource
public boolean hasResource(Resource resource, int amount) {
String key = generateKey(resource);
Resource existing = inventory.get(key);
return existing != null && existing.getAmount() >= amount;
}
// Return a fully safe, read-only view of the inventory through copies
public Map<String, Resource> viewInventory() {
Map<String, Resource> inventoryCopy = new HashMap<>();
for (Map.Entry<String, Resource> entry : inventory.entrySet()) {
inventoryCopy.put(entry.getKey(), entry.getValue().copy());
}
return Collections.unmodifiableMap(inventoryCopy);
}
// Create a unified, formatted key
private String generateKey(Resource resource) {
return resource.getClass().getSimpleName().toUpperCase()
+ ":" + resource.getName();
}
@Override
public String toString() {
return "Colony: " + name + "\nColony ID: " + colony_id + "\nLocation: (" + location.getX()
+ ", " + location.getY() + ")" + "\nRisk Factor: " + riskFactor;
}
}