Commit c380ebdd authored by MostafaRahmati's avatar MostafaRahmati

Minor Bug Fixed.

parent 1d3b43e7
...@@ -6,57 +6,69 @@ public class Main { ...@@ -6,57 +6,69 @@ public class Main {
* *
*/ */
public static void main(String[] args) { public static void main(String[] args) {
VotingSystem system = new VotingSystem(); VotingSystem votingSystem = new VotingSystem();
ArrayList<String> singleVoting = new ArrayList<String>(); ArrayList<String> singleVoting = new ArrayList<>();
singleVoting.add("option1"); singleVoting.add("option1");
singleVoting.add("option2"); singleVoting.add("option2");
singleVoting.add("option3"); singleVoting.add("option3");
Voting voting1 = system.createVoting(0, "Enter Selected Option Please...", singleVoting); Voting voting1 = votingSystem.createVoting(0, "Enter Selected Option Please...", singleVoting);
Person person1 = new Person("firstName1", "lastName1"); Person person1 = new Person("firstName1", "lastName1");
Person person2 = new Person("firstName", "lastName2"); Person person2 = new Person("firstName", "lastName2");
Person person3 = new Person("firstName3", "lastName3"); Person person3 = new Person("firstName3", "lastName3");
Person person4 = new Person("firstName4", "lastName4"); Person person4 = new Person("firstName4", "lastName4");
system.vote(voting1, person1, "option1"); ArrayList<String> person1Votes = new ArrayList<>();
system.vote(voting1, person2, "option2"); person1Votes.add("option1");
system.vote(voting1, person3, "option3");
system.vote(voting1, person4, "option2"); ArrayList<String> person2Votes = new ArrayList<>();
person2Votes.add("option2");
ArrayList<String> person3Votes = new ArrayList<>();
person3Votes.add("option1");
ArrayList<String> person4Votes = new ArrayList<>();
person4Votes.add("option3");
votingSystem.vote(voting1, person1, person1Votes, 0);
votingSystem.vote(voting1, person2, person2Votes, 0);
votingSystem.vote(voting1, person3, person3Votes, 0);
votingSystem.vote(voting1, person4, person4Votes, 0);
system.printVotingResult(voting1, 0); votingSystem.printVotingResult(voting1, 0);
ArrayList<String> multipleVoting = new ArrayList<>(); ArrayList<String> multipleVoting = new ArrayList<>();
multipleVoting.add("option11"); multipleVoting.add("option11");
multipleVoting.add("option21"); multipleVoting.add("option21");
multipleVoting.add("option31"); multipleVoting.add("option31");
Voting voting2 = system.createVoting(1, "Please Enter Your Choice For Multiple System" Voting voting2 = votingSystem.createVoting(1, "Please Enter Your Choice For Multiple System"
, multipleVoting); , multipleVoting);
ArrayList<String> person1Votes = new ArrayList<>(); ArrayList<String> person1VotesMultiple = new ArrayList<>();
person1Votes.add("option11"); person1VotesMultiple.add("option11");
person1Votes.add("option21"); person1VotesMultiple.add("option21");
ArrayList<String> person2Votes = new ArrayList<>(); ArrayList<String> person2VotesMultiple = new ArrayList<>();
person2Votes.add("option11"); person2VotesMultiple.add("option11");
person2Votes.add("option31"); person2VotesMultiple.add("option31");
ArrayList<String> person3Votes = new ArrayList<>(); ArrayList<String> person3VotesMultiple = new ArrayList<>();
person3Votes.add("option11"); person3VotesMultiple.add("option11");
person3Votes.add("option21"); person3VotesMultiple.add("option21");
ArrayList<String> person4Votes = new ArrayList<>(); ArrayList<String> person4VotesMultiple = new ArrayList<>();
person4Votes.add("option11"); person4VotesMultiple.add("option11");
person4Votes.add("option21"); person4VotesMultiple.add("option21");
system.vote(voting2, person1, person1Votes); votingSystem.vote(voting2, person1, person1VotesMultiple, 1);
system.vote(voting2, person2, person2Votes); votingSystem.vote(voting2, person2, person2VotesMultiple, 1);
system.vote(voting2, person3, person3Votes); votingSystem.vote(voting2, person3, person3VotesMultiple, 1);
system.vote(voting2, person4, person4Votes); votingSystem.vote(voting2, person4, person4VotesMultiple, 1);
system.printVotingResult(voting2, 1); votingSystem.printVotingResult(voting2, 1);
} }
} }
\ No newline at end of file
...@@ -39,7 +39,7 @@ public class Voting { ...@@ -39,7 +39,7 @@ public class Voting {
this.type = type; this.type = type;
this.question = question; this.question = question;
this.options = options; this.options = options;
this.voters = new ArrayList<Person>(); this.voters = new ArrayList<>();
this.polls = new HashMap<String, HashSet<Vote>>(); this.polls = new HashMap<String, HashSet<Vote>>();
} }
...@@ -55,36 +55,48 @@ public class Voting { ...@@ -55,36 +55,48 @@ public class Voting {
* @param person voter * @param person voter
* @param selectedOptions selected options by voter in multiple type * @param selectedOptions selected options by voter in multiple type
*/ */
public void vote(Person person, ArrayList<String> selectedOptions) { public void vote(Person person, ArrayList<String> selectedOptions, int type) {
this.voters.add(person); this.voters.add(person);
if (type==1){
for (String selectedOption : selectedOptions) {
if (!this.polls.containsKey(selectedOption)) {
HashSet<Vote> voteSet = new HashSet<>();
Vote vote = new Vote(person, new JalaliCalendar());
voteSet.add(vote);
this.polls.put(selectedOption, voteSet);
}
HashSet<Vote> voteSet = this.polls.get(selectedOption);
voteSet.add(new Vote(person, new JalaliCalendar()));
this.polls.put(selectedOption, voteSet);
}
}
for (String selectedOption : selectedOptions) { else{
if (!this.polls.containsKey(selectedOption)) { String selectedOption = selectedOptions.get(0);
HashSet<Vote> voteSet = new HashSet<>(); HashSet<Vote> voteSet = this.polls.containsKey(selectedOption) ?
this.polls.get(selectedOption) : new HashSet<>();
Vote vote = new Vote(person, new JalaliCalendar()); Vote vote = new Vote(person, new JalaliCalendar());
voteSet.add(vote); voteSet.add(vote);
this.polls.put(selectedOption, voteSet); this.polls.put(selectedOption, voteSet);
}
HashSet<Vote> voteSet = this.polls.get(selectedOption);
voteSet.add(new Vote(person, new JalaliCalendar()));
this.polls.put(selectedOption, voteSet);
} }
} }
/** // /**
* @param person voter // * @param person voter
* @param selectedOption selected option by voter in single type // * @param selectedOption selected option by voter in single type
*/ // */
public void vote(Person person, String selectedOption) { // public void vote(Person person, String selectedOption) {
this.voters.add(person); // this.voters.add(person);
HashSet<Vote> voteSet = this.polls.containsKey(selectedOption) ? // HashSet<Vote> voteSet = this.polls.containsKey(selectedOption) ?
this.polls.get(selectedOption) : new HashSet<>(); // this.polls.get(selectedOption) : new HashSet<>();
Vote vote = new Vote(person, new JalaliCalendar()); // Vote vote = new Vote(person, new JalaliCalendar());
voteSet.add(vote); // voteSet.add(vote);
this.polls.put(selectedOption, voteSet); // this.polls.put(selectedOption, voteSet);
} // }
/** /**
* @return voters list * @return voters list
......
...@@ -58,15 +58,11 @@ public class VotingSystem { ...@@ -58,15 +58,11 @@ public class VotingSystem {
* @param person the voter * @param person the voter
* @param selectedOptions options which are selected * @param selectedOptions options which are selected
*/ */
public void vote(Voting voting, Person person, ArrayList<String> selectedOptions) { public void vote(Voting voting, Person person, ArrayList<String> selectedOptions, int type) {
voting.vote(person, selectedOptions); voting.vote(person, selectedOptions, type);
} }
public void vote(Voting voting, Person person, String selectedOption) {
voting.vote(person, selectedOption);
}
public void printVotingResult(Voting voting, int type) { public void printVotingResult(Voting voting, int type) {
System.out.println(voting.getQuestion()); System.out.println(voting.getQuestion());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment