package dbcvote;

import java.util.LinkedList;

public class VoteReader {
       /* this class would read from a file,
        * but we want to avoid Java I/O */
       private Preference[][] voteList =
       {    { new Preference("Byrne, Joseph", 3),
              new Preference("Dingle, Sally", 1),
              new Preference("Flood, Jerry", 4),
              new Preference("Simpson, Dave", 3),
              new Preference("Thompson, Pat", 2) },

            { new Preference("Abbott, Mary", 5),
              new Preference("Dingle, Sally", 1),
              new Preference("Flood, Jerry", 4),
              new Preference("Moody, Claire", 2),
              new Preference("Thompson, Pat", 3) },

            { new Preference("Abbott, Mary", 5),
              new Preference("Dingle, Sally", 1),
              new Preference("Flood, Jerry", 1),
              new Preference("Moody, Claire", 2),
              new Preference("Thompson, Pat", 3) }
       };

       public Vote[] loadVotes() {
              Vote[] votes = new Vote[voteList.length];

              for (int i = 0; i < votes.length; i++) {
                 LinkedList prefs = new LinkedList();
                 for (int j = 0; j < voteList[i].length; j++) {
                     prefs.add(voteList[i][j]);
                 }

                 votes[i] = new Vote(prefs);
              }

              return votes;
       }

       public static String toString(Vote[] votes) {
              String out = new String();

              for (int i = 0; i < votes.length; i++) {
                  out += votes[i].toString();
              }

              return out;
       }

       public static void main(String args[]) {
              VoteReader vr = new VoteReader();

              Vote[] votes = vr.loadVotes();

              for (int i = 0; i < votes.length; i++) {
                  System.out.println("Vote " + i);
                  System.out.println(votes[i].toString());

                  System.out.println("Vote " + i + ", validated");
                  try {
                      votes[i].validateVote();
                  }
                  catch (SpoiledVoteException e) {
                      System.out.println(e.getMessage());
                  }
                  System.out.println(votes[i].toString());
              }
       }
}

