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

MohammadSajad-Alipour-9831043 #9

Open
wants to merge 4 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
2 changes: 2 additions & 0 deletions MohammadSajad-Alipour/MohammadSajad-Alipour.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Full Name: MohammadSajad Alipour
Student Number: 9831043
83 changes: 83 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;

/**
* This is the main class to test and run classes and methods
*/
public class Main {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
VotingSystem votingSystem = new VotingSystem();
/**
* Type 0 voting
*/
//Creating choices list
ArrayList<String> choicesVotingNum1=new ArrayList<>();
//Adding choices
choicesVotingNum1.add("Cristiano Ronaldo");
choicesVotingNum1.add("Lionel Messi");
choicesVotingNum1.add("Neymar");
//Creating the voting
votingSystem.createVoting("Who is the best player?",0,choicesVotingNum1);
//Testing printVoting method
votingSystem.printVoting(0);
//Creating voters and choosing their choices
Person voter1=new Person("a","A");
ArrayList<String> voter1Choices=new ArrayList<>();
voter1Choices.add("Cristiano Ronaldo");
Person voter2=new Person("b","B");
ArrayList<String> voter2Choices=new ArrayList<>();
Random random=new Random();
voter2Choices.add(choicesVotingNum1.get(random.nextInt(3)));
Person voter3=new Person("c","C");
ArrayList<String> voter3Choices=new ArrayList<>();
voter3Choices.add("Neymar");
Person voter4=new Person("d","D");
ArrayList<String> voter4Choices=new ArrayList<>();
voter4Choices.add("Cristiano Ronaldo");
//Submitting votes
votingSystem.vote(0,voter1,voter1Choices);
votingSystem.vote(0,voter2,voter2Choices);
votingSystem.vote(0,voter3,voter3Choices);
votingSystem.vote(0,voter4,voter4Choices);
//Showing the result
votingSystem.printResults(0);
/**
* Type 1 voting
*/
ArrayList<String> choicesVotingNum2=new ArrayList<>();
choicesVotingNum2.add("Money heist");
choicesVotingNum2.add("Dark");
choicesVotingNum2.add("Mr.Robot");
choicesVotingNum2.add("Rick And Morty");
choicesVotingNum2.add("Bojack Horseman");
votingSystem.createVoting("Which TV show do you like?",1,choicesVotingNum2);
votingSystem.printVoting(1);
Person voter11=new Person("a","A");
ArrayList<String> voter11Choices=new ArrayList<>();
voter11Choices.add("Dark");
voter11Choices.add("Bojack Horseman");
Person voter22=new Person("b","B");
ArrayList<String> voter22Choices=new ArrayList<>();
voter22Choices.add("Dark");
voter22Choices.add("Mr.Robot");
Person voter33=new Person("c","C");
ArrayList<String> voter33Choices=new ArrayList<>();
voter33Choices.add("Money heist");
voter33Choices.add("Rick And Morty");
voter33Choices.add("Dark");
voter33Choices.add("Mr.Robot");
Person voter44=new Person("d","D");
ArrayList<String> voter44Choices=new ArrayList<>();
voter44Choices.add("Dark");
voter44Choices.add("Money heist");
votingSystem.vote(1,voter11,voter11Choices);
votingSystem.vote(1,voter22,voter22Choices);
votingSystem.vote(1,voter33,voter33Choices);
votingSystem.vote(1,voter44,voter44Choices);
votingSystem.printResults(1);
//Testing printVotingQuestions Method
votingSystem.printVotingQuestions();
}
}
27 changes: 27 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
/**
* This is a class for voter's details
*/
public class Person {
private String firstName;
private String lastName;
public Person(String firstName,String lastName){
this.firstName=firstName;
this.lastName=lastName;
}

public String getFirstName() {
return firstName;
}

public String getLastName() {
return lastName;
}

@Override
public String toString() {
return "Person{" +
"firstName='" + firstName + '\'' +
", lastName='" + lastName + '\'' +
'}';
}
}
35 changes: 35 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import java.util.Objects;

/**
* This is a class for vote's details
*/
public class Vote {
private Person person;
private String date;
public Vote(Person person,String date){
this.person=person;
this.date=date;
}

public Person getPerson() {
return person;
}

public String getDate() {
return date;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Vote vote = (Vote) o;
return Objects.equals(person, vote.person) &&
Objects.equals(date, vote.date);
}

@Override
public int hashCode() {
return Objects.hash(person, date);
}
}
85 changes: 85 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-4/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
import ir.huri.jcal.JalaliCalendar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;

/**
* This is a class for voting's purposes and details and functions
*/
public class Voting {
//if type==0 --> one vote mode
//if type==1 --> multiple vote mode
private int type;
private String question;
private ArrayList<Person> voters=new ArrayList<>();
private HashMap<String, HashSet<Vote>> listOfVotesToChoices=new HashMap<>();

public Voting(int type,String question){
this.type=type;
this.question=question;
}

public String getQuestion() {
return question;
}

/**
* This is a method to add a choice
* @param choice
*/
public void createChoice(String choice){
listOfVotesToChoices.put(choice,new HashSet<Vote>());
}

/**
* This is a method for submitting a vote
* @param person
* @param choices
*/
public void vote(Person person,ArrayList<String> choices){
voters.add(person);
if(type==0){
JalaliCalendar calendar=new JalaliCalendar();
Vote vote=new Vote(person,calendar.toString());
listOfVotesToChoices.get(choices.get(0)).add(vote);
}else{
for(String choice:choices){
JalaliCalendar calendar=new JalaliCalendar();
Vote vote=new Vote(person,calendar.toString());
listOfVotesToChoices.get(choice).add(vote);
}
}
}

public void getVoters() {
for(Person voter:voters){
System.out.println(voter);
}
}

/**
* This is a method to show the result of voting
*/
public void printVotes(){
int maximum=0;
String choiceMax=null;
for(String key:listOfVotesToChoices.keySet()){
if(listOfVotesToChoices.get(key).size()>=maximum){
choiceMax=key;
maximum=listOfVotesToChoices.get(key).size();
}
System.out.println(key+" : "+listOfVotesToChoices.get(key).size()+" votes ");
}
System.out.println("the most voted choice is : ");
System.out.println(choiceMax);
}

public ArrayList<String> getChoices(){
ArrayList<String> choices=new ArrayList<>();
for(String key:listOfVotesToChoices.keySet()){
choices.add(key);
}
return choices;
}
}
69 changes: 69 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-4/VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import java.util.ArrayList;

/**
* This is a class for handling all the votings there is.
*/
public class VotingSystem {
private ArrayList<Voting> votingList=new ArrayList<>();

public VotingSystem(){

}

/**
* This is a method to create a voting
* @param question
* @param type
* @param choices
*/
public void createVoting(String question,int type,ArrayList<String> choices){
Voting voting=new Voting(type,question);
for(String choice:choices){
voting.createChoice(choice);
}
votingList.add(voting);

}

/**
* This is a method to print all the questions of votings
*/
public void printVotingQuestions(){
for(Voting voting:votingList){
System.out.println(voting.getQuestion());
}
}

/**
* This is a method to print voting's details
* @param votingNumber
*/
public void printVoting(int votingNumber){
Voting voting = votingList.get(votingNumber);
System.out.println(voting.getQuestion());
for(String choice:voting.getChoices()){
System.out.println(choice);
}

}

/**
* This is a method to vote to a specific voting
* @param votingNum
* @param person
* @param choices
*/
public void vote(int votingNum,Person person,ArrayList<String> choices){
Voting voting=votingList.get(votingNum);
voting.vote(person,choices);
}

/**
* This is a method to show the result of a specific voting
* @param votingNum
*/
public void printResults(int votingNum){
Voting voting=votingList.get(votingNum);
voting.printVotes();
}
}
30 changes: 30 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-5/better-code/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/**
* This is a class for circle shapes and inherits from Shape
*/
public class Circle extends Shape {
private int radius;

public Circle(int radius) {
this.radius = radius;
}

@Override
public double calculateArea() {
return Math.PI * radius * radius;
}

@Override
public double calculatePerimeter() {
return 2 * Math.PI * radius;
}


public int getRadius() {
return radius;
}

@Override
public String toString() {
return "Circle:: radius:" + radius;
}
}
31 changes: 31 additions & 0 deletions MohammadSajad-Alipour/src-ap-lab-session-5/better-code/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
/**
* This is the test class for testing classes
*/
public class Main {
/**
* This is the main method
*
* @param args
*/
public static void main(String[] args) {
//creating paint
Paint paint = new Paint();
//creating shapes
Shape circle = new Circle(1);
Polygon triangle = new Triangle(3, 4, 5);
Polygon rectangle = new Rectangle(4, 8, 4, 8);
Rectangle eqRectangle = new Rectangle(6, 6, 6, 6);
Triangle eqTriangle = new Triangle(2, 2, 2);
//testing add methods
paint.addShape(circle);
paint.addShape(triangle);
paint.addShape(rectangle);
paint.addShape(eqRectangle);
paint.addShape(eqTriangle);
//testing draw and print all methods
paint.drawAll();
paint.printAll();
//testing describeEqualSides method
paint.describeEqualSides();
}
}
Loading