Skip to content
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
Binary file removed AP-LabManual-Session4.zip
Binary file not shown.
Binary file removed AP-LabManual-Session5.pdf
Binary file not shown.
21 changes: 0 additions & 21 deletions LICENSE

This file was deleted.

Binary file added Mohammad Javad Rajabi - 9831025/report_wk4.pdf
Binary file not shown.
Binary file added Mohammad Javad Rajabi - 9831025/report_wk5.pdf
Binary file not shown.
77 changes: 77 additions & 0 deletions Mohammad Javad Rajabi - 9831025/s4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* The Voting program is implement an application that create voting system
* that can create new voting with question and choices to person can vote.
*
* @author Mohammad Javad Rajabi
* @version 1.0
* @since 2020-03-24
*/
public class Main {

/**
* This method simulate a voting system
* @param args
*/
public static void main(String[] args){

Scanner myObj = new Scanner(System.in);

VotingSystem votingSystem = new VotingSystem();
Person voter_0 = new Person("Javad", "Rajabi");
Person voter_1 = new Person("Parsa", "Ahani");
Person voter_2 = new Person("Erfan", "Karimian");
Person voter_3 = new Person("Erfan", "Naderi");
Person voter_4 = new Person("Ali", "Pasyar");
ArrayList travel = new ArrayList();
ArrayList city = new ArrayList();
ArrayList days = new ArrayList();
travel.add("agree");
travel.add("disagree");
travel.add("random");
city.add("Isfahan");
city.add("Kish");
city.add("Gheshm");
city.add("Shomal");
city.add("Mashad");
city.add("random");
days.add("saturday");
days.add("sunday");
days.add("Thursday");
days.add("friday");
votingSystem.creatingVoting("let's go trip",0,travel);
votingSystem.creatingVoting("where?",0,city);
votingSystem.creatingVoting("when?",1,days);
System.out.println("print voting questions");
votingSystem.printVotingQuestions();
System.out.println("print voting");
votingSystem.printVoting(0);
ArrayList votes0_0 = new ArrayList();
ArrayList votes0_1 = new ArrayList();
ArrayList votes0_2 = new ArrayList();
votes0_0.add("agree");
votes0_1.add("random");
votes0_2.add("sunday");
votes0_2.add("friday");
votingSystem.vote(0,voter_0,votes0_0);
votingSystem.vote(1,voter_0,votes0_1);
votingSystem.vote(2,voter_0,votes0_2);
votingSystem.vote(2,voter_0,votes0_2);
votingSystem.vote(2,voter_1,votes0_2);
// myObj.next();
System.out.println("print result");
votingSystem.printResults(0);
votingSystem.printResults(1);
votingSystem.printResults(2);







}

}
72 changes: 72 additions & 0 deletions Mohammad Javad Rajabi - 9831025/s4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import java.util.Objects;

/**
* This class declare a person who can vote
*
* @author Mohammad Javad Rajabi
* @version 1.0
* @since 2020-03-24
*/
public class Person {

//fields

// first name of person who vote
private String firstName;
// last name of person who vote
private String lastName;


//constructor

/**
* person constructor, for create new object of this class.
* @param firstName This is first name of person who vote
* @param lastName This is last name of person who vote
*/
public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}


//methods

/**
* This method is used to be able to access first name.
* @return String This returns first name of this person.
*/
public String getFirstName() {
return firstName;
}

/**
* This method is used to be able to access last name.
* @return String This returns last name of this person.
*/
public String getLastName() {
return lastName;
}

@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Person)) return false;
Person person = (Person) o;
return getFirstName().equals(person.getFirstName()) &&
getLastName().equals(person.getLastName());
}

@Override
public int hashCode() {
return Objects.hash(getFirstName(), getLastName());
}
}
64 changes: 64 additions & 0 deletions Mohammad Javad Rajabi - 9831025/s4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import java.util.Objects;

/**
* This class declare vote that a person can do it.
*
* @author Mohammad Javad Rajabi
* @version 1.0
* @since 2020-03-24
*/
public class Vote {

//fields

// object of person type
private Person person;
// date that person vote
private String date;


//constructor

/**
* vote constructor, for create new object of this class.
* @param person This is person who vote
* @param date This is date that person vote
*/
public Vote(Person person, String date) {
this.person = person;
this.date = date;
}


//methods

/**
* This method is used to be able to access person.
* @return String This returns person.
*/
public Person getPerson() {
return person;
}

/**
* This method is used to be able to access date of vote.
* @return String This returns date of vote.
*/
public String getDate() {
return date;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (!(o instanceof Vote)) return false;
Vote vote = (Vote) o;
return getPerson().equals(vote.getPerson()) &&
getDate().equals(vote.getDate());
}

@Override
public int hashCode() {
return Objects.hash(getPerson(), getDate());
}
}
140 changes: 140 additions & 0 deletions Mohammad Javad Rajabi - 9831025/s4/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
import javax.lang.model.type.NullType;
import java.util.*;
import ir.huri.jcal.JalaliCalendar;

/**
* This class create a voting that have question and choices.
*
* @author Mohammad Javad Rajabi
* @version 1.0
* @since 2020-03-24
*/
public class Voting {

//fields

// this field declare type of voting
private int type;
// question of voting
private String question;
// list of voter
private ArrayList<Person> voters;
// list of option that map with number of vote
private HashMap<String, HashSet<Vote>> listOfVoteToChoices;


//constructor

/**
* Voting constructor, for create new object of this class.
* @param type This is parameter that declare type of voting
* @param question This is question of voting
*/
public Voting(int type, String question) {
this.type = type;
this.question = question;
voters = new ArrayList<Person>();
listOfVoteToChoices = new HashMap<String, HashSet<Vote>>();
}

//methods

/**
* This method is used to be able to access question.
* @return String This returns question of this voting.
*/
public String getQuestion() {
return question;
}

/**
* This method is used to be able to access type of voting.
* @return int This returns type of this voting.
*/
public int getType() {
return type;
}

/**
* This method is used to be able to access list of vote to choices.
* @return HashMap This returns list of vote to choices of this voting.
*/
public HashMap<String, HashSet<Vote>> getListOfVoteToChoices() {
return listOfVoteToChoices;
}

/**
* This method is used to add a person to list of voter if he already voted he cant vote.
* @param option This parameter add to list of choices in this voting.
*/
public void createChoice(String option) {
HashSet<Vote> set = new HashSet<Vote>();
listOfVoteToChoices.put(option, set);
}

/**
* This method is used to add a person to list of voter if he already voted he cant vote.
* @param person This is a person who want to vote.
* @param options This is a list of choice that person choose.
*/
public void vote(Person person, ArrayList<String> options){
for (String option:options){
HashSet<Vote> votes = listOfVoteToChoices.get(option);
if(votes != null) {
for (Vote vote : votes) {
if (vote.getPerson().equals(person)) {
System.out.println(person.getFirstName() +" "+ person.getLastName() + " already voted");
return;
}
}
}
GregorianCalendar gc = new GregorianCalendar();
JalaliCalendar jalaliCalendar = new JalaliCalendar(gc);
Vote vote = new Vote(person, jalaliCalendar.toString());
HashSet<Vote> x = listOfVoteToChoices.get(option);
x.add(vote);
voters.add(person);
System.out.println("vote submitted!");
}
}

/**
* This method is used to print list of voter who vote in this voting.
*/
public void getVoters() {
System.out.println("-------------------------------------------");
System.out.println("list of person who voted");
for (Person person: voters){
System.out.println((voters.indexOf(person)+1)+")"+person.getFirstName() + person.getLastName());
}
System.out.println("-------------------------------------------");

}

/**
* This method is used to print the choice and list of voter who vote to this choice.
*/
public void printVotes() {
Set<String> sets = listOfVoteToChoices.keySet();
ArrayList<String> stringsList = new ArrayList<>(sets);
for (int i =0 ; i< stringsList.size() ; i++) {
System.out.println((i+1)+")"+ stringsList.get(i)+ "->" + listOfVoteToChoices.get(stringsList.get(i)).size());
for (Vote vote :listOfVoteToChoices.get(stringsList.get(i))){
System.out.println(vote.getPerson().getFirstName() + " " + vote.getPerson().getLastName() + " " +
vote.getDate().toString());
}
System.out.println();

}
}

/**
* This method is used to add get array list of choices.
* @return ArrayList This returns list of choices
*/
public ArrayList<String> getChoices() {
Set<String> sets = listOfVoteToChoices.keySet();
ArrayList<String> stringsList = new ArrayList<>(sets);
return stringsList;
}
}
Loading