-
Notifications
You must be signed in to change notification settings - Fork 0
/
BirdSurvey.java
143 lines (128 loc) · 4.28 KB
/
BirdSurvey.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
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
package module5.datastructures.birdsurvey;
import java.util.Scanner;
import java.util.StringJoiner;
/**
* BirdSurveyList
*/
public class BirdSurvey extends MyLinkedList<BirdSpecies> {
private static final long serialVersionUID = 4838685580168749376L;
private static final Scanner console = new Scanner(System.in);
/**
* Search the list for a BirdSpecies object which matches the given species.
*
* @return A matching BirdSpecies object, or {@code null} if no such bird
* exists.
*/
public BirdSpecies getBirdSpecies(String species) {
species = species.toLowerCase().trim();
for (BirdSpecies b : this) {
if (b.getSpecies().equals(species)) {
return b;
}
}
return null;
}
public void addBird(String bird) {
bird = bird.toLowerCase().trim();
BirdSpecies b = this.getBirdSpecies(bird);
if (b == null) {
b = new BirdSpecies(bird, 1);
this.add(b);
} else {
b.addCount(); // count += 1
}
}
public void addBirds(String... birds) {
for (String b : birds) {
addBird(b);
}
}
/**
* @param The name of the species to look for
* @return Return the number of birds counted for the given species
*/
public int getCount(String species) {
BirdSpecies b = getBirdSpecies(species);
if (b == null) {
return 0;
}
return b.getCount();
}
/** @return the number of unique species which have been recorded */
public int getSpeciesCount() {
return this.size();
}
/** @return the total number of birds which have been recorded in the survey */
public int getTotalBirdCount() {
int sum = 0;
for (BirdSpecies b : this) {
sum += b.getCount();
}
return sum;
}
/**
* @return a formatted message, displaying a list of the birds spotted so far
*/
public String getReport() {
int speciesCount = this.getSpeciesCount();
int totalBirds = this.getTotalBirdCount();
StringJoiner report = new StringJoiner("\n");
report.add(String.format("=== %d SPECIES RECORDED SO FAR (%d TOTAL BIRDS) ===", speciesCount, totalBirds));
for (BirdSpecies b : this) {
String species = b.getSpecies();
int count = b.getCount();
report.add(String.format(" * %-20s – %2d sightings", species, count));
}
return report.toString();
}
// CONSOLE METHODS
/** Prompts the user for input, then returns a string */
protected static String readString(String prompt) {
System.out.print(prompt);
return console.nextLine();
}
protected static String readStringNonEmpty(String prompt) {
String input = "";
do {
input = readString(prompt);
} while (input.length() == 0);
return input;
}
/**
* Add the specified bird to the survey. If that bird's species is not found in
* the list, then ask the user whether they'd still like to add it
*
* @param bird
*/
public void inputBird(String bird) {
BirdSpecies b = getBirdSpecies(bird);
if (b == null) {
System.out.printf("Species \"%s\" has not yet been sighted.\n", bird);
boolean doContinue = readString("Add anyway? (y/N) ").matches("[Yy]");
if (!doContinue) {
System.out.printf("\n\"%s\" was not added. List unchanged.\n", bird);
return;
}
}
addBird(bird);
System.out.printf("\nYou sighted a %s! (%d sightings total)\n\n", bird, getCount(bird));
}
/**
* The main interactive loop for the BirdSurvey application. Repeatedly prompts
* the user to enter birds, displaying the bird list each time.
*/
public void inputBirds() {
while (true) {
System.out.println("\n" + this.getReport() + "\n");
String input = readString("Enter species (q to exit): ");
if (input.matches("[qQ]")) {
break;
}
this.inputBird(input);
pause();
}
}
public static void pause() {
readString("(Press enter to continue...)");
}
}