Permalink
Browse files

Driver Code added

  • Loading branch information...
JanBessai committed Oct 2, 2018
1 parent c976837 commit f4b50bb41bcf97719a59ee27817f46cdd6126c6e
@@ -0,0 +1,61 @@
package edu.wpi.voting;
import akka.actor.ActorRef;
import akka.actor.ActorSystem;
import edu.wpi.voting.messages.StartElection;
import edu.wpi.voting.services.Management;
import edu.wpi.voting.services.Voter;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class Main {
enum Party {
Java("Java4Ever"),
Python("Pythonists"),
C("C-Lovers"),
NonProgrammers("Marketing People Party");
public final String partyName;
Party(String partyName) {
this.partyName = partyName;
}
}
public static void main(String[] args) {
final ActorSystem system = ActorSystem.create("VotingSystem");
List<Party> votes =
Arrays.asList(
Party.Java,
Party.Python,
Party.Python,
Party.C,
Party.Java,
Party.Java,
Party.NonProgrammers,
Party.NonProgrammers);
List<ActorRef> voters = new ArrayList<>();
for (Party vote: votes) {
voters.add(system.actorOf(Voter.props(vote.partyName)));
}
List<String> eligibleParties = new ArrayList<>();
for (Party party : Party.values()) {
if (!party.equals(Party.NonProgrammers)) {
eligibleParties.add(party.partyName);
}
}
ActorRef voteManager = system.actorOf(Management.props(eligibleParties, voters));
try {
voteManager.tell(new StartElection(), ActorRef.noSender());
System.out.println(">>> Press ENTER to exit <<<");
System.in.read();
} catch (IOException ioe) {
} finally {
system.terminate();
}
}
}
@@ -4,11 +4,11 @@
import java.util.Collections;
import java.util.List;
public class OpenBooth {
public class OpenStation {
public final List<String> eligibleParties;
public final int voterCount;
public OpenBooth(List<String> eligibleParties, int voterCount) {
public OpenStation(List<String> eligibleParties, int voterCount) {
this.eligibleParties = Collections.unmodifiableList(new ArrayList<String>(eligibleParties));
this.voterCount = voterCount;
}
@@ -2,11 +2,12 @@
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import akka.event.Logging;
import akka.event.LoggingAdapter;
import edu.wpi.voting.messages.Ballot;
import edu.wpi.voting.messages.ElectionResults;
import edu.wpi.voting.messages.OpenBooth;
import edu.wpi.voting.messages.OpenStation;
import edu.wpi.voting.messages.StartElection;
import java.util.ArrayList;
@@ -31,10 +32,10 @@ public Receive createReceive() {
return receiveBuilder()
.match(StartElection.class, s -> {
log.info("Election started");
ActorRef votingBooth = getContext().actorOf(VotingBooth.props(getSelf()));
ActorRef votingStation = getContext().actorOf(VotingStation.props(getSelf()));
votersWithoutBallots.clear();
votersWithoutBallots.addAll(voters);
votingBooth.tell(new OpenBooth(parties, voters.size()), getSelf());
votingStation.tell(new OpenStation(parties, voters.size()), getSelf());
})
.match(Ballot.class, ballot -> {
if (votersWithoutBallots.size() > 0) {
@@ -49,4 +50,8 @@ public Receive createReceive() {
getContext().stop(self());
}).build();
}
public static Props props(List<String> parties, List<ActorRef> voters) {
return Props.create(Management.class, () -> new Management(parties, voters));
}
}
@@ -1,6 +1,8 @@
package edu.wpi.voting.services;
import akka.actor.AbstractActor;
import akka.actor.ActorRef;
import akka.actor.Props;
import edu.wpi.voting.messages.Ballot;
import edu.wpi.voting.messages.ParticipationReceipt;
import edu.wpi.voting.messages.Vote;
@@ -20,4 +22,6 @@ public Receive createReceive() {
getContext().stop(getSelf());
}).build();
}
public static Props props(String voteFor) { return Props.create(Voter.class, () -> new Voter(voteFor)); }
}
@@ -7,17 +7,13 @@
import java.util.*;
public class VotingBooth extends AbstractActor {
public class VotingStation extends AbstractActor {
private final ActorRef manager;
private final Set<String> ballotPasswords;
private final Map<String, Integer> votesCast;
private int abstained;
static Props props(ActorRef manager) {
return Props.create(VotingBooth.class, () -> new VotingBooth(manager));
}
public VotingBooth(ActorRef manager) {
public VotingStation(ActorRef manager) {
this.ballotPasswords = new HashSet<String>();
this.votesCast = new HashMap<String, Integer>();
this.abstained = 0;
@@ -26,7 +22,7 @@ public VotingBooth(ActorRef manager) {
public Receive createReceive() {
return receiveBuilder()
.match(OpenBooth.class, ob -> {
.match(OpenStation.class, ob -> {
ballotPasswords.clear();
votesCast.clear();
abstained = 0;
@@ -63,4 +59,8 @@ public Receive createReceive() {
}).build();
}
public static Props props(ActorRef manager) {
return Props.create(VotingStation.class, () -> new VotingStation(manager));
}
}

0 comments on commit f4b50bb

Please sign in to comment.