/* Java class instances of which represent an object meant to maintain the ** state and behavior of an election. ** ** CMPS 134 Fall 2019 ** Authors: P.M.J & R.W.M. & ... ** ** Collaboration: ** ** Known defects: ... */ public class Election { // class constants // --------------- public static final int CANDIDATE_LIMIT = 16; //Default upper limit // instance variables // ------------------ private int limit; //Limit on number of candidates private int numCandidates; //# of candidates (currently) on the ballot private String[] candidate; //Array of candidate names private int[] voteCount; //Array of candidate vote counts private boolean open = false; //controls whether votes can be cast // constructors // ------------ /* Establishes this election as one in which the limit on the number of ** candidates is the default value. */ public Election() { initialize(CANDIDATE_LIMIT); } /* Establishes this election as one in which the limit on the number of ** candidates is the value provided. */ public Election(int candidateLimit) { initialize(candidateLimit); } /* Does the actual work of constructing and initializing this election's ** instance variables, consistent with the limit on the number of ** candidates being the value provided. */ private void initialize(int candidateLimit) { if(candidateLimit >= 0) { //Stubbed !!! } else { throw new IllegalArgumentException("invalid candidate limit"); } } // observers // --------- /* Returns the number of candidates (currently) on the ballot. */ public int getCandidateCount() { return numCandidates; } /* Returns the name of the candidate at the given position on the ballot. */ public String getCandidateName(int index) { String result = ""; //Stubbed !!! return result; } /* Returns the count of the number of votes cast for the candidate at ** the given position on the ballot. */ public int getCandidateVoteCount(int index) { int result = 0; //Stubbed !!! return result; } /* Reports whether or not the election is open (for votes to be cast). */ public boolean isOpen() { return open; } /* Returns a string describing the current state of the election, ** including the name of each candidate and how many votes were ** cast for them. */ public String toString() { char COMMA = ','; char BLANK = ' '; String result = ""; for(int i=0; i != numCandidates; i++) { result = result + candidate[i] + COMMA + voteCount[i] + BLANK; } return result.trim(); } // mutators // -------- /* Adds the given candidate to the ballot of this election. */ public void addCandidate(String name) { int index = indexOf(name, candidate, 0, numCandidates); if(index < 0) { //Stubbed !!! } else { throw new IllegalArgumentException("candidate already added"); } } /* Changes the state of this election object so as to allow the ** subsequent casting of votes. */ public void openVoting() { if(!open) { //Stubbed !!! } else { throw new IllegalStateException("voting already open"); } } /* Changes the state of this election object so as to prevent the ** subsequent casting of votes. */ public void closeVoting() { if(open) { //Stubbed !!! } else { throw new IllegalStateException("voting not now open"); } } /* Records a vote for the candidate with the given name. ** If no candidate with that name is currently on the ballot and ** a spot remains available for a new candidate, the given name is ** added to the ballot (and one vote is credited to them). */ public void castVote(String name) { if(open) { //Stubbed !!! } else { throw new IllegalStateException("voting not allowed now"); } } // private methods // --------------- /* Searches the specified array segment (i.e., ary[low..high)), ** returning the (highest-numbered) position within it at which the ** given target string occurs, or -1 if it does not occur. */ private int indexOf(String target, String[] ary, int low, int high) { int result = high-1; while((result != -1) && !ary[result].equals(target)) { result = result - 1; } return result; } }