/**
* class Election
*
* @author (Mr. Collard)
* @version (2010.11.16)
*/
import java.util.Scanner;
public class Election
{
// instance variables
Candidate[] canlist; // canlist will be an array of Candidate objects
int numcandidates = 0;
long totalvotes = 0; // should use 0 but want to avoid potential for divide by zero error
/**
* Constructor for objects of class Election
*/
public Election(int n)
{
// initialise instance variables
if (n > 0) numcandidates = n;
}
/**
* getCandidates - Use console I/O to read in the name and vote data
*
*/
public void getCandidates()
{
if (numcandidates < 1) return; // NO candidates
Scanner input = new Scanner(System.in);
String curname;
int curvotes;
// create the array of candidate object references
canlist = new Candidate[numcandidates];
for (int i=0; i< numcandidates; i++)
{
System.out.print("Candidate #"+ (i+1) + " - ");
curname = input.next();
System.out.print("Votes for Candidate #"+ (i+1) + " - ");
curvotes = input.nextInt();
canlist[i] = new Candidate(); // create a new candidate object
canlist[i].name = curname;
canlist[i].votes = curvotes;
totalvotes += curvotes; // add to vote total
}
System.out.print("Done Entering Candidate Data\n\n");
}
/**
* printList - Sample output method to demonstrate object array references
*
*/
public void printList()
{
System.out.print("Candidate Votes\n--------- -----\n");
for (int i=0; i< numcandidates; i++)
{
System.out.printf("%-12s%7d\n",canlist[i].name, canlist[i].votes);
}
}
}