-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathMain.java
214 lines (179 loc) · 7.43 KB
/
Main.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
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
12/4/24
Voting Ballot for Thor vs Loki
*/
import java.io.*;
import java.util.Scanner;
class Candidate {
private final String name;
private int votes;
public Candidate(String name) {
this.name = name;
this.votes = 0;
}
public String getName() {
return name;
}
public int getVotes() {
return votes;
}
public void vote() {
votes++;
}
public void setVotes(int votes) {
this.votes = votes;
}
}
// BallotQuestion Class
class BallotQuestion {
private final String question;
private int yesVotes;
private int noVotes;
public BallotQuestion(String question) {
this.question = question;
this.yesVotes = 0;
this.noVotes = 0;
}
public String getQuestion() {
return question;
}
public int getYesVotes() {
return yesVotes;
}
public int getNoVotes() {
return noVotes;
}
public void voteYes() {
yesVotes++;
}
public void voteNo() {
noVotes++;
}
public void setYesVotes(int votes) {
this.yesVotes = votes;
}
public void setNoVotes(int votes) {
this.noVotes = votes;
}
}
// Main Class
public class Main {
private static final String DATA_FILE = "votes_data.txt";
private static boolean hasVotedForCandidate = false; // Track if the user has voted for a candidate
private static boolean hasVotedForBallot = false; // Track if the user has voted for the ballot question
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
// Language selection
System.out.println("Choose your language:");
System.out.println("1. English");
System.out.println("2. Asgardian");
System.out.print("Enter your choice: ");
int languageChoice = scanner.nextInt();
scanner.nextLine();
if (languageChoice == 2) {
System.out.println("Your device does not have the necessary plugin for Asgardian. Defaulting to English.");
}
Candidate candidate1 = new Candidate("Thor");
Candidate candidate2 = new Candidate("Loki");
BallotQuestion ballotQuestion = new BallotQuestion("Is Thor the better sibling?");
loadVotes(candidate1, candidate2, ballotQuestion);
boolean voting = true;
while (voting) {
System.out.println("\nWelcome to the voting system!");
System.out.println("1. Vote for a candidate");
System.out.println("2. Answer the ballot question");
System.out.println("3. View results");
System.out.println("4. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
scanner.nextLine();
switch (choice) {
case 1:
if (hasVotedForCandidate) {
System.out.println("You have already voted for a candidate. You can only vote once for each category.");
break;
}
System.out.println("\nCandidates:");
System.out.println("1. " + candidate1.getName());
System.out.println("2. " + candidate2.getName());
System.out.print("Enter your choice: ");
int candidateChoice = scanner.nextInt();
if (candidateChoice == 1) {
candidate1.vote();
hasVotedForCandidate = true;
saveVotes(candidate1, candidate2, ballotQuestion);
System.out.println("Vote recorded for " + candidate1.getName());
} else if (candidateChoice == 2) {
candidate2.vote();
hasVotedForCandidate = true;
saveVotes(candidate1, candidate2, ballotQuestion);
System.out.println("Vote recorded for " + candidate2.getName());
} else {
System.out.println("Invalid choice!");
}
break;
case 2:
if (hasVotedForBallot) {
System.out.println("You have already voted on the ballot question. You can only vote once for each category.");
break;
}
System.out.println("\nBallot Question: " + ballotQuestion.getQuestion());
System.out.println("1. Yes");
System.out.println("2. No");
System.out.print("Enter your choice: ");
int ballotChoice = scanner.nextInt();
if (ballotChoice == 1) {
ballotQuestion.voteYes();
hasVotedForBallot = true;
saveVotes(candidate1, candidate2, ballotQuestion);
System.out.println("You voted Yes.");
} else if (ballotChoice == 2) {
ballotQuestion.voteNo();
hasVotedForBallot = true;
saveVotes(candidate1, candidate2, ballotQuestion);
System.out.println("You voted No.");
} else {
System.out.println("Invalid choice!");
}
break;
case 3:
System.out.println("\nResults:");
System.out.println(candidate1.getName() + ": " + candidate1.getVotes() + " votes");
System.out.println(candidate2.getName() + ": " + candidate2.getVotes() + " votes");
System.out.println(ballotQuestion.getQuestion());
System.out.println("Yes: " + ballotQuestion.getYesVotes());
System.out.println("No: " + ballotQuestion.getNoVotes());
break;
case 4:
voting = false;
System.out.println("Thank you for participating in the vote!");
break;
default:
System.out.println("Invalid choice!");
}
}
scanner.close();
}
private static void saveVotes(Candidate c1, Candidate c2, BallotQuestion bq) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(DATA_FILE))) {
writer.write(c1.getName() + "," + c1.getVotes() + "\n");
writer.write(c2.getName() + "," + c2.getVotes() + "\n");
writer.write(bq.getQuestion() + "," + bq.getYesVotes() + "," + bq.getNoVotes() + "\n");
} catch (IOException e) {
System.out.println("Error saving votes: " + e.getMessage());
}
}
private static void loadVotes(Candidate c1, Candidate c2, BallotQuestion bq) {
try (BufferedReader reader = new BufferedReader(new FileReader(DATA_FILE))) {
String[] candidate1Data = reader.readLine().split(",");
c1.setVotes(Integer.parseInt(candidate1Data[1]));
String[] candidate2Data = reader.readLine().split(",");
c2.setVotes(Integer.parseInt(candidate2Data[1]));
String[] ballotData = reader.readLine().split(",");
bq.setYesVotes(Integer.parseInt(ballotData[1]));
bq.setNoVotes(Integer.parseInt(ballotData[2]));
} catch (IOException e) {
System.out.println("No previous data found. Starting fresh.");
}
}
}