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

SevdaImany9831010 #36

Open
wants to merge 1 commit 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
48 changes: 48 additions & 0 deletions SevdaImany/Lab4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import java.util.ArrayList;

/**
* this is a check for voting system
*/
public class Main {


/**
* @param args
*/
public static void main(String[] args) {
//create a new voting system
VotingSystem votingSystem = new VotingSystem();

//set a question
String question = "Are you happy?";

//set the choices
ArrayList<String> choices = new ArrayList<String>();
choices.add("Yes");
choices.add("No");

//create a new voting
votingSystem.createVoting(question, 0, choices);

//print a voting
votingSystem.printVoting(0);

//create a new voter
Person voter = new Person("Sevda", "Imany");

//create a new list for voter's choices
ArrayList<String> voterChoices = new ArrayList<>();

voterChoices.add("No");

//add the voter to system
votingSystem.vote(0, voter, voterChoices);

//set another voter
voter = new Person("negin", "kheirmand");
votingSystem.vote(0, voter, null);
votingSystem.printvotes(0);

}

}
51 changes: 51 additions & 0 deletions SevdaImany/Lab4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
/**
* This class represent a person
* it holds the full name of a person
*
* @author sevda imany
* @version 0.0
*/
public class Person {
//the name of a persom
private String firstName;
//the last name of a person
private String lastName;

/**
* creat a new person with given name
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}


/**
* get person's first name
* @return
*/
public String getFirstName() {
return firstName;
}



/**
* get person's last name
* @return
*/
public String getLastName() {
return lastName;
}


/**
* @return a String of a person feilds
*/
@Override
public String toString() {
return "Person [firstName=" + firstName + ", lastName=" + lastName + "]";
}


}
80 changes: 80 additions & 0 deletions SevdaImany/Lab4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@

/**
* this class represent a vote in voting class
*
* @author sevda imany
* @version 0.0
*/
public class Vote {
//the voter
private Person person;
//the date of vote
private String date;

/**
* creat a new vote
* @param person
* @param date
*/
public Vote(Person person, String date) {
this.person = person;
this.date = date;
}


/**
* get person
* @return
*/
public Person getPerson() {
return person;
}



/**
* get date
* @return
*/
public String getDate() {
return date;
}


/**
* @return int
*/
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((person == null) ? 0 : person.hashCode());
return result;
}


/**
* equality of two object
* @param obj
* @return boolean
*/
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Vote other = (Vote) obj;
if (person == null) {
if (other.person != null)
return false;
} else if (!person.equals(other.person))
return false;
return true;
}



}
146 changes: 146 additions & 0 deletions SevdaImany/Lab4/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,146 @@
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import ir.huri.jcal.JalaliCalendar;

/**
* this class represent a voting in voting system
*
* @author sevda imany
* @version 0.0
*/
public class Voting {
//0 if voter possible choose is one otherwise 1
private int type;
//the question of voting
private String question;
//the list of voters
private ArrayList<Person> voters;
//the choices
private HashMap<String, HashSet<Vote>> polls;
private JalaliCalendar calendar;

/**
* create a new voting with given type and question
* @param type
* @param question
*/
public Voting(int type, String question) {
this.type = type;
this.question = question;
voters = new ArrayList<Person>();
polls = new HashMap<String, HashSet<Vote>>();
calendar = new JalaliCalendar();
}




/**
* get question
* @return
*/
public String getQuestion() {
return question;
}

/**
* this method print all voters name
*/
public void getVoters() {
for (Person voter : voters) {
System.out.println(voter.toString());
}
}




/**
* @return HashMap<String, HashSet<Vote>>
*/
public HashMap<String, HashSet<Vote>> getPolls() {
return polls;
}



/**
* create a new choise
* @param choice
*/
public void createChoice(String choice) {
polls.put(choice, new HashSet<Vote>());
}



/**
* @return choices
*/
public ArrayList<String> getChoices() {
ArrayList<String> choices = new ArrayList<>();

for (String choice : polls.keySet()) {
choices.add(choice);
}

return choices;
}



/**
* this method add a person's vote
* @param person : the voter
* @param votes : the voter's votes
*/
public void vote(Person person,ArrayList<String> votes){
for(Person p : voters){
if(p.equals(person)){
System.out.println("You can vote once");
return;
}
}

if(type == 0){
if(votes.size() != 1){
System.out.println("You have one choice!");
return;
}
}

String date= calendar.toString();
Vote vote = new Vote(person,date);
voters.add(person);

for(String s : votes){
for(String choice : polls.keySet()){
if(s.equals(choice)){
polls.get(choice).add(vote);
}
}
}
}

/**
* this method show the votes
*/
public void printVotes(){
for (String s : polls.keySet()){
int size = polls.get(s).size();
System.out.println(s + " : " + size);
}
}


/**
* get type
* @return
*/
public int getType() {
return type;
}


}
Loading