-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPokedex.java
104 lines (91 loc) · 2.75 KB
/
Pokedex.java
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
import java.util.Comparator;
/**
* Represents a Pokedex - basically a Pokemon encyclopedia that adds new entries
* when you encounter a Pokemon for the first time.
* It also provides methods for organizing its information in useful ways.
*
* @author Joe Rossi
* @version 1.0
*/
public class Pokedex {
// ------ Instance data here ------
private MySortedSet<Pokemon> set;
/**
* Constructs a Pokedex object by setting up the sorted set of Pokemon
*/
public Pokedex() {
Comparator<Pokemon> c = (a, b) -> a.compareTo(b);
set = new MySortedSet<Pokemon>(c);
}
@Override
public String toString() {
MySortedSet<Pokemon> temp = new MySortedSet<Pokemon>();
temp = set.sort((a, b) -> a.compareTo(b));
String s = "";
for (Pokemon p : temp) {
s += p.toString() + "\n";
}
return s;
}
/**
* Adds a Pokemon to the sorted set
*
* @param p the Pokemon to be added
* @return true if the pokemon was not already in the set, false otherwise
*/
public boolean add(Pokemon p) {
return set.add(p);
}
/**
* Returns the number of Pokemon in the Pokedex
*
* @return the number of Pokemon in the Pokedex
*/
public int countPokemon() {
return set.size();
}
/**
* Clear the Pokedex and start over
*/
public void clear() {
set.clear();
}
/**
* Returns a set of alphabetized Pokemon, using a lambda expression
*
* @return the alphabetized set
*/
public MySortedSet<Pokemon> listAlphabetically() {
return set.sort((a, b) -> a.getName().compareTo(b.getName()));
}
/**
* Returns a set of Pokemon grouped by type, using a lambda expression
*
* @return the grouped by primary type set
*/
public MySortedSet<Pokemon> groupByPrimaryType() {
return set.sort((a, b) -> a.getPrimaryType().compareTo(
b.getPrimaryType()));
}
/**
* Returns a set of all Pokemon of type t
*
* @param t the type we want listed
* @return the set of all Pokemon in the Pokedex of type t
*/
public MySortedSet<Pokemon> listByType(PokemonType t) {
return set.filter((p) -> p.getPrimaryType() == t
|| p.getSecondaryType() == t);
}
/**
* Returns a set of Pokemon with numbers in the range [start, end]
*
* @param start the first number in the new set
* @param end the last number in the new set
* @return the set containing all Pokemon in the Pokedex from [start, end]
*/
public MySortedSet<Pokemon> listRange(int start, int end) {
return set.filter((p) -> p.getNumber() >= start
&& p.getNumber() <= end);
}
}