1
0
Fork 0
VS_LET/09-cloud/src/main/java/vs/BallotBox.java

38 lines
756 B
Java

package vs;
import java.util.HashMap;
import java.util.Map;
import java.util.Set;
public class BallotBox {
private Map<String, Integer> votes = new HashMap<>();
public synchronized void vote(String choice) {
Integer votes = this.votes.get(choice);
if (votes == null) {
votes = 0;
}
this.votes.put(choice, votes + 1);
}
public synchronized int countVotes() {
int sum = 0;
for (Map.Entry<String, Integer> choice : this.votes.entrySet()) {
sum += choice.getValue();
}
return sum;
}
public synchronized int getNumberOfVotes(String choice) {
Integer votes = this.votes.get(choice);
if (votes == null) {
votes = 0;
}
return votes;
}
public synchronized Set<String> getChoices() {
return this.votes.keySet();
}
}