Former question: Assigning probability to a random enum object
So I have this:
import java.util.List;
import java.io.File;
import java.io.IOException;
import java.util.Arrays;
import java.util.Random;
import java.util.ArrayList;
import java.util.Scanner;
import java.lang.Integer;
public class ElectoralCollege {
public static final String FILE = "Electoral201X.txt";
private ArrayList <State> stateVotes;
Random rand = new Random();
List<Party> parties = Arrays.asList(Party.DEMOCRAT, Party.DEMOCRAT, Party.REPUBLICAN, Party.REPUBLICAN, Party.INDEPENDENT);
public ElectoralCollege() throws IOException {
stateVotes = new ArrayList<State>();
}
public void assignStates() throws IOException {
File f = new File(FILE);
Scanner fReader = new Scanner(f);
while(fReader.hasNext()) {
String stateData = fReader.nextLine();
int stateEnd = stateData.indexOf(" - ");
String stateName = stateData.substring(0, stateEnd);
String numVote = stateData.substring(stateEnd + 2);
Party winner = parties.get(rand.nextInt(5));
State voteInfo = new State(stateName, Integer.parseInt(numVote.trim()), winner);
stateVotes.add(voteInfo);
}
}
public void announceWinner() {
int dem = 0;
int rep = 0;
int ind = 0 ;
for(State a : stateVotes) {
if(a.getWinner().equals(Party.DEMOCRAT))
dem = dem + a.getNumVotes();
else if(a.getWinner().equals(Party.REPUBLICAN))
rep = rep + a.getNumVotes();
else if(a.getWinner().equals(Party.INDEPENDENT))
ind = ind + a.getNumVotes();
}
if(dem >= 270)
System.out.println("DEMOCRATS WIN");
else if(rep >= 270)
System.out.println("REPUBLICANS WIN");
else if(ind >= 270)
System.out.println("INDEPENDENTS WIN");
else
System.out.println("CONGRESS MUST CHOOSE");
for(State b : stateVotes)
System.out.println(b.getName() + " " + b.getWinner());
}
}
I need to answer this:
In announceWinner() look through stateVotes and add up the total electoral votes for each of the three parties. Print out the total electoral votes for each party and if any party has more than the 270 needed to win, announce that party as the winner - otherwise announce that Congress will have to make the choice. Finally, print the individual winner of each state.
For some reason it always prints CONGRESS MUST CHOOSE. Upon further investigation all int values of dem, ind and rep are always 0. Not sure if I need to initialize the variable or not like I did, but it kept giving me errors when I did not.
The Accessor methods are all part of a basic State.class, that has a constructor and three accessor methods to access every parameter in the state object. Which stateVotes is made up of.
State values in stateVotes are also all set. From the old question I read a line of text from a file, made it into a state object, then put it into the stateVotes arraylist. I did this for each line of text in that file.
The second foreach loop doesn't print anything at all.
State class is simple enough:
public class State {
private String Name;
private int votes;
private Party winningParty;
public State(String stateName, int numVote, Party winner) {
Name = stateName;
votes = numVote;
winningParty = winner;
}
public Party getWinner() {
return winningParty;
}
public String getName() {
return Name;
}
public int getNumVotes() {
return votes;
}
}
Example of the State data is "Floria 29 DEMOCRAT".