Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FarshidNooshi9831068 #29

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
.idea
production
FarshidNooshi9831068.iml
2 changes: 2 additions & 0 deletions FarshidNooshi9831068/FarshidNooshi9831068.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Name: Farshid Nooshi
Student ID: 9831068
Binary file added FarshidNooshi9831068/JalaliCalendar-1.3.1.jar
Binary file not shown.
40 changes: 40 additions & 0 deletions FarshidNooshi9831068/src/lab4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// In The Name Of GOD
package lab4;

import java.util.ArrayList;

public class Main {

public static void main(String[] args) {
VotingSystem votingSystem = new VotingSystem();
Person[] p = new Person[]{new Person("s1", "e1"), new Person("s2", "e2"), new Person("s3", "e3"), new Person("s4", "e4")};
ArrayList<String>[] choices = new ArrayList[4];
for (int i = 0; i < 4; i++)
choices[i] = new ArrayList<>();
votingSystem.createVoting("q1", 0);
for (int i = 1; i <= 4; i++)
votingSystem.getVoting(0).createChoice("c" + i);
for (int i = 0; i < 4; i++)
choices[i].add("c" + (i + 1));
for (int i = 0; i < 4; i++)
votingSystem.vote(0, p[i], choices[i]);
votingSystem.printResult(0);
System.out.println();
votingSystem.createVoting("q2", 1);
for (int i = 1; i <= 4; i++)
votingSystem.getVoting(1).createChoice("c" + i);
for (int i = 0; i < 4; i++)
votingSystem.vote(1, p[i], choices[i]);
votingSystem.printResult(1);
System.out.println();
votingSystem.printListOfVoting();
System.out.println();
votingSystem.printVoting(0);
votingSystem.printVoting(1);
System.out.println();
votingSystem.printResult(0);
System.out.println();
votingSystem.printResult(1);
System.out.println();
}
}
44 changes: 44 additions & 0 deletions FarshidNooshi9831068/src/lab4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
// In The Name Of GOD
package lab4;

public class Person {
private final String firstName, lastName;

/**
*
* @param firstName is the first name of person(voter)
* @param lastName is the last name of person(voter)
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

public String getLastName() {
return lastName;
}

public String getFirstName() {
return firstName;
}
@Override
public String toString() {
return firstName + " " + lastName;
}

/**
* checks if two objects of person type are equal
* @param obj is the object to check
* @return true if they're equal
*/
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != getClass())
return false;
Person tmp = (Person) obj;
return tmp.getLastName().equals(lastName) &&
tmp.getFirstName().equals(firstName);
}
}
59 changes: 59 additions & 0 deletions FarshidNooshi9831068/src/lab4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
// In The Name Of GOD
package lab4;

import java.util.Objects;

public class Vote {
private final Person person;
private final String date;


/**
* @param person is the person who votes
* @param date is the date of the vote
*/
public Vote(Person person, String date) {
this.person = person;
this.date = date;
}

public Person getPerson() {
return person;
}

public String getDate() {
return date;
}

/**
* almost copied this part from here :
* https://www.sitepoint.com/how-to-implement-javas-hashcode-correctly/
*
* @return an integer representing the hash value of this vote
*/
@Override
public int hashCode() {
return Objects.hash(person, date);
}

/**
* checks if two objects are equal
* @param obj is the object to be checked
* @return true if the objects are equal
*/
@Override
public boolean equals(Object obj) {
if (obj == this)
return true;
if (obj == null || obj.getClass() != getClass())
return false;
Vote tmp = (Vote) obj;
return tmp.date.equals(date) &&
tmp.person.equals(person);
}

@Override
public String toString() {
return person.toString() + " " + date;
}
}
107 changes: 107 additions & 0 deletions FarshidNooshi9831068/src/lab4/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,107 @@
// In The Name Of GOD
package lab4;

import ir.huri.jcal.JalaliCalendar;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

public class Voting {
private final int type;
private final String question;
private ArrayList<Person> voters;
private HashMap<String, HashSet<Vote>> choices;

/**
*
* @param type is the type of the voting
* @param question is the question of the voting
*/
public Voting(int type, String question) {
this.type = type;
this.question = question;
voters = new ArrayList<>();
choices = new HashMap<>();
}

public String getQuestion() {
return question;
}

/**
* @param person is a person to get checked
* @return true if voters arrayList doesn't contain this voter
*/
private boolean valid(Person person) {
for (Person it : voters)
if (person.equals(it))
return false;
return true;
}

/**
* this method votes for someone
*
* @param person is the person who wanna attend the voting
* @param choices is the persons choices
*/
public void vote(Person person, ArrayList<String> choices) {
if (choices.size() > 1 && type == 1) {
System.out.println("You should choose one choice in this voting");
return;
}

if (!valid(person)) {
System.out.println("person already voted!");
return;
}

voters.add(person);
JalaliCalendar date = new JalaliCalendar();
for (String it : choices)
this.choices.get(it).add(new Vote(person, date.toString()));
}

/**
* this method is a getter for voters
*
* @return the voters ArrayList
*/
public ArrayList<Person> getVoters() {
return voters;
}

/**
* this method prints a single voting in the program by telling the voters of each option & number of votes for each choice
*/
public void printVotes() {
System.out.println("Printing the votes");
{
System.out.println(voters.size() + " is the number of voters");
for (String it : choices.keySet()) {
System.out.println("choice " + it + " has : " + choices.get(it).size() + " votes");
System.out.println("Voters are : ");
for (Vote vote : choices.get(it)) {
System.out.print(vote + " ");
}
System.out.println();
}
}
}

public HashMap<String, HashSet<Vote>> getChoices() {
return choices;
}

/**
* this method adds a new choice
*
* @param option is a new choice to add
*/
public void createChoice(String option) {
if (choices.containsKey(option))
System.out.println("choice already exists!");
else
choices.put(option, new HashSet<>());
}
}
80 changes: 80 additions & 0 deletions FarshidNooshi9831068/src/lab4/VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// In The Name Of GOD
package lab4;

import java.util.ArrayList;

public class VotingSystem {
private ArrayList<Voting> votingList;

/**
* a simple constructor for this class
*/
public VotingSystem() {
votingList = new ArrayList<>();
}

/**
* this class creates a new voting
*
* @param question := question of the problem
* @param type := type of the question, 0 -> single choice, 1 -> multiple choice
*/
public void createVoting(String question, int type) {
votingList.add(new Voting(type, question));
}

/**
* prints all of the voting with their number and questions
*/
public void printListOfVoting() {
int idx = 1;
for (Voting voting : votingList)
System.out.println("" + idx++ + ") " + voting.getQuestion());
}

/**
* prints the results of a voting
*
* @param idx is an index for the voting
*/
public void printResult(int idx) {
votingList.get(idx).printVotes();
}

/**
* prints the specific voting with their choices
*
* @param idx is the index of the voting
*/
public void printVoting(int idx) {
System.out.println("" + (idx + 1) + ") " + votingList.get(idx).getQuestion());
for (String it : votingList.get(idx).getChoices().keySet())
System.out.println(it);
System.out.println();
}

/**
* @return votingList of the system
*/
public ArrayList<Voting> getVotingList() {
return votingList;
}

/**
* @param idx is the index of the voting to be returned
* @return the voting[idx] from the ArrayList
*/
public Voting getVoting(int idx) {
return votingList.get(idx);
}

/**
* generates a vote for person
* @param idx is index of a voting
* @param person is a voter
* @param choices is the voters choices
*/
public void vote(int idx, Person person, ArrayList<String> choices) {
votingList.get(idx).vote(person, choices);
}
}
Loading