-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashMapCardCollection.java
More file actions
45 lines (35 loc) · 1.29 KB
/
HashMapCardCollection.java
File metadata and controls
45 lines (35 loc) · 1.29 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
import java.util.HashMap;
import java.util.Map;
public class HashMapCardCollection implements CardCollection {
private final Map<String, Integer> cardCount = new HashMap<>();
private final Map<String, String> cardTypes = new HashMap<>();
public HashMapCardCollection() {
// Leer el archivo y agregar las cartas al mapa cardTypes
}
@Override
public void addCard(String cardName) {
if (!cardTypes.containsKey(cardName)) {
throw new IllegalArgumentException("Carta no disponible");
}
cardCount.merge(cardName, 1, Integer::sum);
}
@Override
public String getCardType(String cardName) {
return cardTypes.get(cardName);
}
@Override
public Map<String, Integer> getAllCards() {
return new HashMap<>(cardCount);
}
@Override
public Map<String, Integer> getCardsByType() {
Map<String, Integer> cardsByType = new HashMap<>();
for (Map.Entry<String, Integer> entry : cardCount.entrySet()) {
String cardName = entry.getKey();
int count = entry.getValue();
String cardType = cardTypes.get(cardName);
cardsByType.merge(cardType, count, Integer::sum);
}
return cardsByType;
}
}