First let´s create a bean class as Candidate.java to hold the data:
Candidate.java:
-----------------------------------
public class Candidate {
private String name;
private int[] votes;
private String record;
private double percentage;
private int totalVotes;
/**
* @return the name
*/
public String getName() {
return name;
}
/**
* @param name the name to set
*/
public void setName(String name) {
this.name = name;
}
/**
* @return the votes
*/
public int[] getVotes() {
return votes;
}
/**
* @param votes the votes to set
*/
public void setVotes(int[] votes) {
this.votes = votes;
}
/**
* @return the percentage
*/
public double getPercentage() {
return percentage;
}
/**
* @param percentage the percentage to set
*/
public void setPercentage(double percentage) {
this.percentage = percentage;
}
/**
* @return the totalVotes
*/
public int getTotalVotes() {
return totalVotes;
}
/**
* @param totalVotes the totalVotes to set
*/
public void setTotalVotes(int totalVotes) {
this.totalVotes = totalVotes;
}
/**
* @return the record
*/
public String getRecord() {
return record;
}
/**
* @param record the record to set
*/
public void setRecord(String record) {
this.record = record;
}
}
VoteCounter.java:
--------------------------------------
import java.util.Scanner;
public class VoteCounter {
private static int noOfCandidates=4;
public static Candidate[] candidates=new Candidate[noOfCandidates];
public static void main(String[] args) throws Exception{
Candidate candidate = null;
int[] AaronVotes=new int[]{192,147,186,114,267};
int[] BakerVotes=new int[]{48,90,12,21,13};
int[] CadenVotes=new int[]{206,312,121,408,382};
int[] DaenaVotes=new int[]{37,21,38,39,21};
double maxPercentage=0.00;
Candidate winner=null;
int noOfAreas=AaronVotes.length;
StringBuilder record=null;
int total=0;
int grandTotal=0;
StringBuilder header=new StringBuilder();
header.append("Name");
for(int i=1;i<=noOfAreas;i++){
header.append("\tArea"+i);
}
header.append("\ttotal");
header.append("\t%");
System.out.println(header);
for(int i=1; i<=noOfCandidates; i++){
total=0;
candidate= new Candidate();
switch (i) {
case 1:
candidate.setName("Aaron");
candidate.setVotes(AaronVotes);
break;
case 2:
candidate.setName("Baker");
candidate.setVotes(BakerVotes);
break;
case 3:
candidate.setName("Caden");
candidate.setVotes(CadenVotes);
break;
case 4:
candidate.setName("Daena");
candidate.setVotes(DaenaVotes);
break;
}
record = new StringBuilder(candidate.getName());
for(int votes:candidate.getVotes()){
total=total+votes;
record.append("\t"+votes);
}
record.append("\t"+total);
grandTotal=grandTotal+total;
candidate.setTotalVotes(total);
candidate.setRecord(record.toString());
candidates[i-1]=candidate;
}
double votePercentage=0.00;
for(Candidate cand: candidates){
votePercentage=cand.getTotalVotes()*100/grandTotal;
if(votePercentage>maxPercentage){
maxPercentage=votePercentage;
winner=cand;
}
cand.setPercentage(votePercentage);
cand.setRecord(cand.getRecord() + "\t"+votePercentage);
System.out.println(cand.getRecord());
}
System.out.println("Do you want to compute the final results: Y/N");
Scanner input= new Scanner(System.in);
try{
if(input.nextLine().equalsIgnoreCase("Y")){
if(null!=winner){
if(winner.getPercentage()>50.00)
System.out.println(winner.getName()+" is the winner");
else
System.out.println("a runoff between the two candidates receiving the highest number of votes; the two candidates should be identified by their names.");
}
}else{
throw new Exception("Invalid choice!");
}
}catch(Exception e){
e.printStackTrace();
}finally{
input.close();
}
}
}
0