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

MohammadSafari9831138 #14

Open
wants to merge 3 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 .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"java.dependency.packagePresentation": "flat"
}
42 changes: 21 additions & 21 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,21 +1,21 @@
MIT License

Copyright (c) 2020 AUT AP 2020

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
MIT License
Copyright (c) 2020 AUT AP 2020
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
80 changes: 80 additions & 0 deletions MohammadSafari/Lab_S04/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package MohammadSafari.Lab_S04;

import java.util.*;

/**
* running an example of system
*
* @author M.Safari
* @version 1399.02.05
*/
public class Main {
public static void main(String[] args) {
Person me = new Person("firstName", "lastName");
Person you = new Person("Mohammad", "Safari");
Person he = new Person("Amir", "Hamidi");
VotingSystem test = new VotingSystem();
// first poll
{
test.createVoting("are you okay?", 1, new ArrayList<String>() {
{
add("YES!");
add("NO!");
};
});
test.vote(0, me, new HashSet<String>() {
{
add("YES!");
};
});
test.vote(0, you, new HashSet<String>() {
{
add("NO!");
};
});
test.vote(0, he, new HashSet<String>() {
{
add("YES!");
};
});
System.out.println("\033[1;31m");
System.out.println(test.getVoting(0).question);
System.out.println("\033[0m");
test.getResult(0);
}
// second poll
{
test.createVoting("which is worse?", 2, new ArrayList<String>() {
{
add("virtual class and elearning:))!");
add("no spare time to learn more!");
add("sleeping all the day!");
add("getting insomnia after a while");
};
});
test.vote(1, me, new HashSet<String>() {
{
add("sleeping all the day!");
add("virtual class and elearning:))!");

};
});
test.vote(1, you, new HashSet<String>() {
{
add("sleeping all the day!");
add("getting insomnia after a while");
};
});
test.vote(1, he, new HashSet<String>() {
{
add("virtual class and elearning:))!");
add("no spare time to learn more!");
};
});
System.out.println("\033[1;31m");
System.out.println(test.getVoting(1).question);
System.out.println("\033[0m");
test.getResult(1);
}
}
}
41 changes: 41 additions & 0 deletions MohammadSafari/Lab_S04/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package MohammadSafari.Lab_S04;

/**
* keeping neccessary information of a person for voting
*
* @author M.Safari
* @version 1399.02.05
*/
public class Person {
public final String firstName;
public final String lastName;

public Person(String firstName, String lastName) {
this.firstName = firstName;
this.lastName = lastName;
}

/**
* @return the firstName
*/
public String getFirstName() {
return firstName;
}

/**
* @return the lastName
*/
public String getLastName() {
return lastName;
}

@Override
public String toString() {
return String.format("%s %s", firstName, lastName);
}

@Override
public boolean equals(Object obj) {
return (obj instanceof Person) ? this.toString().equals(((Person) obj).toString()) : false;
}
}
43 changes: 43 additions & 0 deletions MohammadSafari/Lab_S04/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package MohammadSafari.Lab_S04;

/**
* shows each vote that is specified for each person
*
* @author M.Safari
* @version 1399.02.05
*/
public class Vote {
public final Person voter;
public final String date;
private static int code = 15;

public Vote(Person voter, String date) {
code++;
this.voter = voter;
this.date = date;
}

/**
* @return the voter
*/
public Person getVoter() {
return voter;
}

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

@Override
public boolean equals(Object obj) {
return (obj instanceof Vote) ? this.equals((Vote) obj) : false;
}

@Override
public int hashCode() {
return code;
}
}
81 changes: 81 additions & 0 deletions MohammadSafari/Lab_S04/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
package MohammadSafari.Lab_S04;

import java.util.*;

import ir.huri.jcal.JalaliCalendar;

/**
* stimulate a voting with specified number of votes for each person with a
* specified question and choices
*
* @author M.Safari
* @version 1399.02.05
*/
public class Voting {
public final int type;
public final String question;
private boolean isActive;
private Set<Person> voters;
private Map<String, Set<Vote>> choices;

public Voting(int type, String question) {
this.question = question;
this.type = type;
isActive = true;
voters = new HashSet<Person>();
choices = new HashMap<String, Set<Vote>>();
}

public void createChoice(String choice) {
// preventing same choices
if (choices.keySet().contains(choice))
return;
Set<Vote> votes = new HashSet<Vote>();
choices.put(choice, votes);
}

/**
* let a person vote to n authorized choices
*
* @param voter
* @param choices
*/
public void vote(Person voter, Set<String> choices) {
int count = 0;
// if voting is still active and voter has not voted yet
if (isActive && !voters.contains(voter))
// checking whether the given choice is authorized
for (String choice : choices)
if (this.choices.keySet().contains(choice)) {
this.choices.get(choice).add(new Vote(voter, new JalaliCalendar().toString()));
count++;
if (count == type)
return;
}
}

/**
* @return the voters
*/
public Set<Person> getVoters() {
return voters;
}

public void printVotes() {
for (String choice : choices.keySet())
System.out.println(String.format("%30.30s : %d", choice, choices.get(choice).size()));

}

/**
* @return the choices
*/
public Map<String, Set<Vote>> getchoices() {
return choices;
}

public void endVoting() {
isActive = false;
}

}
78 changes: 78 additions & 0 deletions MohammadSafari/Lab_S04/VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package MohammadSafari.Lab_S04;

import java.util.*;

/**
* manages active votings
*
* @author M.Safari
* @version 1399.02.05
*/
public class VotingSystem {
private ArrayList<Voting> votingList;

public VotingSystem() {
votingList = new ArrayList<Voting>();
}

/**
*
* @param question
* @param type
* @param choices
*/
public void createVoting(String question, int type, ArrayList<String> choices) {
Voting voting = new Voting(type, question);
// creating choices one by one
for (String choice : choices)
voting.createChoice(choice);
votingList.add(voting);
}

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

/**
* geting index-specified voting stimualtion
*
* @param index
* @return
*/
public Voting getVoting(int index) {
return votingList.get(index);

}

/**
* vote in a form of attending in a index-specified voting
*
* @param index
* @param voter
* @param choices
*/
public void vote(int index, Person voter, Set<String> choices) {
getVoting(index).vote(voter, choices);
}

/**
* printing result of a index-specified voting
*
* @param index
* @return
*/
public Map<String, Set<Vote>> getResult(int index) {
Voting voting = getVoting(index);
voting.printVotes();
return voting.getchoices();
}

public void printListOfVoting(){
for(Voting poll:votingList){
System.out.println(String.format("%d) %30.30s", votingList.indexOf(poll), poll.question));
}
}
}
Loading