-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPoliticalQuizApp.java
More file actions
161 lines (140 loc) · 7.2 KB
/
PoliticalQuizApp.java
File metadata and controls
161 lines (140 loc) · 7.2 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
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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
import java.io.*;
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
public class PoliticalQuizApp {
private static final Map<String, Map<String, String>> partyAnswers = new HashMap<>();
private static final Map<String, Map<String, Double>> answerWeights = new HashMap<>();
private static final Map<String, Integer> userAnswers = new HashMap<>();
private static void initializePartyAnswers() {
// Initialize party answers
Map<String, String> democraticAnswers = new HashMap<>();
democraticAnswers.put("A", "Democratic answer for option A.");
democraticAnswers.put("B", "Democratic answer for option B.");
democraticAnswers.put("C", "Democratic answer for option C.");
democraticAnswers.put("D", "Democratic answer for option D.");
partyAnswers.put("Democratic", democraticAnswers);
// Add answers for other parties
Map<String, String> republicanAnswers = new HashMap<>();
republicanAnswers.put("A", "Republican answer for option A.");
republicanAnswers.put("B", "Republican answer for option B.");
republicanAnswers.put("C", "Republican answer for option C.");
republicanAnswers.put("D", "Republican answer for option D.");
partyAnswers.put("Republican", republicanAnswers);
Map<String, String> independentAnswers = new HashMap<>();
independentAnswers.put("A", "Independent answer for option A.");
independentAnswers.put("B", "Independent answer for option B.");
independentAnswers.put("C", "Independent answer for option C.");
independentAnswers.put("D", "Independent answer for option D.");
partyAnswers.put("Independent", independentAnswers);
Map<String, String> progressiveAnswers = new HashMap<>();
progressiveAnswers.put("A", "Progressive answer for option A.");
progressiveAnswers.put("B", "Progressive answer for option B.");
progressiveAnswers.put("C", "Progressive answer for option C.");
progressiveAnswers.put("D", "Progressive answer for option D.");
partyAnswers.put("Progressive", progressiveAnswers);
}
private static void initializeAnswerWeights() {
// Load answer weights from data files
loadAnswerWeightsFromFile("democratic_weights.txt", "Democratic");
loadAnswerWeightsFromFile("republican_weights.txt", "Republican");
loadAnswerWeightsFromFile("independent_weights.txt", "Independent");
loadAnswerWeightsFromFile("progressive_weights.txt", "Progressive");
}
private static void loadAnswerWeightsFromFile(String filename, String party) {
try (BufferedReader reader = new BufferedReader(new FileReader(filename))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split(",");
if (parts.length == 3) {
String option = parts[0].trim();
double weight = Double.parseDouble(parts[1].trim());
answerWeights.putIfAbsent(party, new HashMap<>());
answerWeights.get(party).put(option, weight);
}
}
} catch (IOException e) {
System.err.println("Error loading answer weights: " + e.getMessage());
}
}
private static double getAnswerWeight(String party, String option) {
Map<String, Double> partyWeights = answerWeights.get(party);
if (partyWeights != null) {
return partyWeights.getOrDefault(option, 0.0);
} else {
return 0.0;
}
}
private static String getAnswer(String party, String option) {
Map<String, String> partyOptions = partyAnswers.get(party);
if (partyOptions != null) {
return partyOptions.getOrDefault(option, "Invalid option.");
} else {
return "Invalid party.";
}
}
private static void storeUserAnswer(String option, int score) {
userAnswers.put(option, score);
}
private static void displayUserResults() {
// Analyze user answers and determine their political affiliation
double democraticScore = 0.0;
double republicanScore = 0.0;
double independentScore = 0.0;
double progressiveScore = 0.0;
for (Map.Entry<String, Integer> entry : userAnswers.entrySet()) {
String option = entry.getKey();
int score = entry.getValue();
democraticScore += score * getAnswerWeight("Democratic", option);
republicanScore += score * getAnswerWeight("Republican", option);
independentScore += score * getAnswerWeight("Independent", option);
progressiveScore += score * getAnswerWeight("Progressive", option);
}
double maxScore = Math.max(Math.max(democraticScore, republicanScore), Math.max(independentScore, progressiveScore));
if (democraticScore == maxScore) {
System.out.println("You are most likely affiliated with the Democratic party.");
} else if (republicanScore == maxScore) {
System.out.println("You are most likely affiliated with the Republican party.");
} else if (independentScore == maxScore) {
System.out.println("You are most likely an Independent.");
} else if (progressiveScore == maxScore) {
System.out.println("You are most likely affiliated with the Progressive party.");
} else {
System.out.println("Unable to determine your political affiliation.");
}
}
private static void saveUserData() {
try (ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream("user_data.dat"))) {
out.writeObject(userAnswers);
System.out.println("User data saved to file.");
} catch (IOException e) {
System.err.println("Error saving user data: " + e.getMessage());
}
}
private static void loadUserData() {
try (ObjectInputStream in = new ObjectInputStream(new FileInputStream("user_data.dat"))) {
userAnswers.clear();
userAnswers.putAll((Map<String, Integer>) in.readObject());
System.out.println("User data loaded from file.");
} catch (IOException | ClassNotFoundException e) {
System.err.println("Error loading user data: " + e.getMessage());
}
}
public static void main(String[] args) {
initializePartyAnswers();
initializeAnswerWeights();
Scanner scanner = new Scanner(System.in);
System.out.println("Welcome to the Political Quiz App!");
System.out.println("Please answer the following 20 questions:");
for (int i = 1; i <= 20; i++) {
System.out.print("Question " + i + " (A-D): ");
String option = scanner.nextLine().toUpperCase();
int score = 21 - i;
storeUserAnswer(option, score);
System.out.println("You selected option " + option + ". " + getAnswer("Democratic", option));
}
displayUserResults();
saveUserData();
loadUserData();
}
}