-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPollPost.java
More file actions
39 lines (34 loc) · 1.29 KB
/
Copy pathPollPost.java
File metadata and controls
39 lines (34 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
import java.util.HashMap;
import java.util.Map;
public class PollPost extends Post {
private Map<String, Integer> options = new HashMap<>();
public PollPost(String title, String optionsString, User user) {
super(title, user);
for (String option : optionsString.split(";")) {
option = option.trim();
options.put(option, 0);
}
}
@Override
public void display() {
System.out.println("Post #" + getId());
System.out.println("Created By: " + user.getFullName() + " (@" + user.getUsername() + ")");
System.out.println("Title: " + title);
for (Map.Entry<String, Integer> entry : options.entrySet()) {
System.out.println(entry.getKey() + ": " + entry.getValue());
}
System.out.println();
}
public void vote(int optionIndex) {
String[] optionKeys = options.keySet().toArray(new String[0]);
if (optionIndex >= 0 && optionIndex < optionKeys.length) {
String selectedOption = optionKeys[optionIndex];
options.put(selectedOption, options.get(selectedOption) + 1);
} else {
System.out.println("Invalid option index.");
}
}
public String[] getOptions() {
return options.keySet().toArray(new String[0]);
}
}