The three errors that I am getting now are from the line:
printResults(election);
How would I fix that so it will print the total value of the votes? Thanks.
TesstCandidate4
public class TestCandidate4
{
public static void printVotes(List<Candidate> election)
{
for(int i = 0; i < election.size(); i++)
System.out.println(election.get(i));
}
public static int getTotal(List<Candidate> election)
{
int total = 0;
for(Candidate candidate : election )
{
total += candidate.numVotes;
}
return total;
}
public static void printResults(Candidate[] election)
{
double percent;
System.out.println("Candidate Votes Received % of Total Votes");
for (Candidate candidate : election)
{
percent = (double) (candidate.votes()) / getTotal(election) * 100;
System.out.printf("%-15s %10d %20.0f", candidate.getName(), candidate.votes(), percent);
System.out.println();
}
}
public static void replaceName(List<Candidate> election,
String find, String replace)
{
for(int index = 0; index < election.size(); index++)
if (election.get(index).getName().equals(find))
election.get(index).setName(replace);
}
public static void replaceVotes(List<Candidate> election,
String find, int replace)
{
for(int index = 0; index < election.size(); index++)
if (election.get(index).getName().equals(find))
election.get(index).setVotes(replace);
}
public static void replaceCandidate(List<Candidate> election,
String find, String replace, int replaceV)
{
for(int index = 0; index < election.size(); index++)
if (election.get(index).getName().equals(find))
{
election.get(index).setName(replace);
election.get(index).setVotes(replaceV);
}
}
public static void main(String[] args)
{
List<Candidate> election = new ArrayList<Candidate>();
// create election
election.add(new Candidate("John Smith", 5000));
election.add(new Candidate("Mary Miller", 4000));
election.add(new Candidate("Michael Duffy", 6000));
election.add(new Candidate("Tim Robinson", 2500));
election.add(new Candidate("Joe Ashtony", 1800));
election.add(new Candidate("Mickey Jones", 3000));
election.add(new Candidate("Rebecca Morgan", 2000));
election.add(new Candidate("Kathleen Turner", 8000));
election.add(new Candidate("Tory Parker", 500));
election.add(new Candidate("Ashton Davis", 10000));
System.out.println("Original results:");
System.out.println();
System.out.println(election);
System.out.println();
System.out.println("Total of votes in election: " + getTotal(election) );
System.out.println();
replaceName(election, "Michael Duffy", "John Elmos");
System.out.println("Changing Michael Duffy to John Elmos:");
System.out.println();
System.out.println(election);
System.out.println("Total of votes in election: " + getTotal(election) );
System.out.println();
replaceVotes(election, "Mickey Jones", 2500);
System.out.println("Changing Mickey Jones to 2500:");
System.out.println();
printResults(election);
System.out.println();
System.out.println("Total of votes in election: " + getTotal(election) );
System.out.println();
replaceCandidate(election, "Kathleen Turner", "John Kennedy", 8500);
System.out.println("Changing Mickey Jones to 2500");
System.out.println();
printResults(election);
System.out.println();
System.out.println("Total of votes in election: " + getTotal(election) );
System.out.println();
}
}
Candidate
public class Candidate
{
// instance variables
int numVotes;
String name;
/**
* Constructor for objects of class InventoryItem
*/
public Candidate(String n, int v)
{
// initialise instance variables
name = n;
numVotes = v;
}
public int votes()
{
return numVotes;
}
public void setVotes(int num)
{
numVotes = num;
}
public String getName()
{
return name;
}
public void setName(String n)
{
name = n;
}
public String toString()
{
return name + " received " + numVotes + " votes.";
}
}