public class PollVotesGUI_UF {
private static DecimalFormat df = new DecimalFormat("000.000");
public static void main(String[] args) {
String[] votes = {"First", "Second", "Third", "Fourth", "Result", "Quit"};
int first = 0,
second = 0,
third = 0,
fourth = 0;
int totalVotes = 0;
double firstPercent = 0.0,
secondPercent = 0.0,
thirdPercent = 0.0,
fourthPercent = 0.0;
while (true) {
int voteSelection = JOptionPane.showOptionDialog(null, "Press Your Vote :", "Vote.", JOptionPane.ERROR_MESSAGE,
JOptionPane.INFORMATION_MESSAGE, null, votes, "");
switch (voteSelection) {
case 0:
first++;
break;
case 1:
second++;
break;
case 2:
third++;
break;
case 3:
fourth++;
break;
case 4:
if (totalVotes == 0) {
JOptionPane.showMessageDialog(null, "No Votes Accumulated.", "No Votes.", JOptionPane.WARNING_MESSAGE);
}
else if (totalVotes > 0) {
firstPercent = (first / totalVotes) * 100;
secondPercent = (second / totalVotes) * 100;
thirdPercent = (third / totalVotes) * 100;
fourthPercent = (fourth / totalVotes) * 100;
JOptionPane.showMessageDialog(null, "Total Votes : " + totalVotes +
"\n" + "\n" +
"Poll A Has " + df.format(firstPercent) + " % " + " Of Total Votes.");
}
JOptionPane.showMessageDialog(null, "Thank You For Voting.");
System.exit(0);
break;
case 5:
JOptionPane.showMessageDialog(null, "Thank You.");
System.exit(0);
break;
case -1:
JOptionPane.showMessageDialog(null, "No Votes Will Be Finilized Due To Exiting The Program Forcely.",
"System Message.", JOptionPane.WARNING_MESSAGE);
System.exit(0);
break;
default:
JOptionPane.showMessageDialog(null, "Unexpected Value.");
System.exit(0);
break;
}
totalVotes = first + second + third + fourth;
JOptionPane.showMessageDialog(null, "" + "\n" + "First : " + first +
"\n" + "Second : " + second +
"\n" + "Third : " + third +
"\n" + "Fourth : " + fourth +
"\n" + "\n" + "Current Votes : " + totalVotes);
}
}
}