Assignment 7 Voting System Java Program
Assignment 7 Voting System Java Program
Assignment 7 Voting System Java Program
/** Election is the base class for all elections. It also contains the main program
that
** runs when the program is started
**/
abstract public class Election {
boolean done = false; // when true, break out of processing loop
class Candidate {
String name;
String party;
int voteCount;
Candidate(String name, String party) {
this.name = name;
this.party = party;
voteCount = 0;
}
}
class Voter {
String name;
String party;
boolean voted;
Voter(String name, String party) {
this.name = name;
this.party = party;
voted = false;
}
}
void log(String s) {
OfficialRecord.append(s + '\n');
System.out.println(s);
}
/**
** Main processing loop, reads and processes messages
**/
void process(FileReader rdr) throws IOException {
BufferedReader b = new BufferedReader(rdr);
String msg;
MessageFactory MF = new MessageFactory();
while (!done && b.ready()) {
msg = b.readLine();
// In a real system, we would probably use multithreading
// and place each message in a queue.
Message m = MF.create(msg);
if (m != null) {
try {
m.perform(this);
} catch (ElectionException ex) {
log(ex.getLogMessage());
}
}
}
}
/** Reset all of the vote counts to 0 and voters as not voted **/
void reset() {
log("Reset");
Enumeration ic = candidates.elements();
while(ic.hasMoreElements()) {
Candidate c = (Candidate) ic.nextElement();
c.voteCount = 0;
}
Enumeration iv = voters.elements();
while(iv.hasMoreElements()) {
Voter v = (Voter) iv.nextElement();
v.voted = false;
}
}
void dump() {
System.out.println("\n\n----------------Start of
Dump-------------------");
System.out.println(OfficialRecord);
System.out.println("---------------End of Dump------------------------");
}
void exit(){
done = true;
log("Exit");
}
}; // Election
/*****************************************************************************
MESSAGE CLASSES
*****************************************************************************/
/** The Messsage class is the abstract base class for all messages. Each drived
** message type must define a perform method that operates on an election
**/
abstract class Message {
String type;
/** perform implements tasks associated with the specific message
** @param e is the election for which the message is intended
** @throws ElectionException if the tasks cannot be performed
**/
abstract void perform(Election e) throws ElectionException;
/** Example of a Factory -- this contains only one static method, create, which
** generates a new message object of the appropriate type from a text
** message. Each message has the form described in the assignment
** where the first four characters define the message type, the fifth
** character is blank and the remainder of the message string contains
** arguments delimited by angle brackets
**/
class MessageFactory {
/** This method, create, examines the first few characters of the message
** to determine the type of the message, then creates a new instance of
** the appropriate message type.
** @param msg is text, which could be read from a file or could be received
** from a network of voting machines and terminals
** @return is an object derived from the class Method, or null if the type of
** the message is unknown
**/
public static Message create(String msg) {
Message result = null;
char type = msg.charAt(0);
// Note, if this were real code we would check the complete message id.
switch (type) {
case 'D': // dump
result = new DumpMessage();
break;
case 'E': // Exit
result = new ExitMessage();
break;
case 'V': // VOTE
result = new VoteMessage(msg);
break;
case 'R': // RGST or REST
if (msg.charAt(1) == 'G') {
result = new RegisterMessage(msg);
} else {
result = new ResetMessage();
}
break;
case 'C': // CAND
result = new CandidateMessage(msg);
break;
case 'L': // LIST
result = new ListMessage(msg);
break;
case 'T': // TLLY
result = new TallyMessage();
break;
} // switch
return result;
} // create
}; // MessageFactory
/** GeneralElection
** Voters can vote for any candidate
**/
class GeneralElection extends Election {
GeneralElection() {
log("General Election");
}
/**
** verify only that the voter has not already voted
@param candidate is not used
@param voter is the name of a voter
**/
void verify(String candidate, String voter)throws ElectionException {
Voter v = getVoter(voter);
if (v.voted) { // check that voter hasn't already voted
throw new AlreadyVotedException(voter);
}
}
};
/** PrimaryElection
** Voters can only vote for candidates in the same party
**/
class PrimaryElection extends Election {
PrimaryElection() {
log("Primary Election");
}
/** Verify that the voter has not already voted and that the candidate
** for whom the voter is voting has the same party affiliation as the
** voter
** @param candidate is the name of the candidate for whom the voter
** is voting
** @param voter is the name of the voter
** @throws AlreadyVotedException if the voter has already voted in
** this election.
** @throws WrongPartyException if the voter and candidate do not
** belong to the same party.
**/
void verify(String candidate, String voter) throws ElectionException {
// check that voter hasn't already voted
Voter v = getVoter(voter);
if (v.voted) {
throw new AlreadyVotedException(voter);
}
// check that party affiliations are the same
Candidate c = getCandidate(candidate);
if (!sameParty(c, v)) {
throw new WrongPartyException(voter, c.party);
}
}
/** List only the candidates for the voters own party **/
void list(String voter) throws ElectionException {
Enumeration it = candidates.elements();
Voter v = getVoter(voter);
log("List for " + voter);
int counter = 0;
while(it.hasMoreElements()) {
Candidate c = (Candidate) it.nextElement();
if (sameParty(c, v)) {
log(" " + ++counter + c.name);
}
}
}
};
/**
** Exceptions for various unexpected situations
** This is obviously overkill, but it illustrates the use of an exception
hierarchy
**/
abstract class ElectionException extends Exception {
abstract String getLogMessage();
};