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

RohamHayedi9831107 #27

Open
wants to merge 2 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 RohamHayedi/RohamHayedi.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Full Name: Roham Hayedi
Student Number: 9831107
25 changes: 25 additions & 0 deletions RohamHayedi/lab4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
public class Person {

private final String firstName;
private final 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 firstName +" "+
lastName ;
}
}
66 changes: 66 additions & 0 deletions RohamHayedi/lab4/Run.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
import ir.huri.jcal.JalaliCalendar;

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

public class Run {
public static void main(String[] args) {

VotingSystem votingSystem = new VotingSystem();
ArrayList<String> options0 = new ArrayList<>();
ArrayList<String> options1 = new ArrayList<>();
ArrayList<String> options2 = new ArrayList<>();

options0.add("Pizza");
options0.add("Sandwich");
options0.add("Neither");

options1.add("Saturday");
options1.add("Sunday");
options1.add("Monday");

votingSystem.createVoting("Pizza or Sandwich?", 0, options0);
votingSystem.createVoting("When are you available?", 1, options1);

Person me = new Person("Roham", "Hayedi");
ArrayList<String> myChoices0 = new ArrayList<>();
ArrayList<String> myChoices1 = new ArrayList<>();
myChoices0.add("Pizza");
myChoices1.add("Saturday");
myChoices1.add("Saturday");
myChoices1.add("Sunday");

votingSystem.vote(0, me, myChoices0);
votingSystem.vote(1, me, myChoices1);
System.out.println("Print questions:");
votingSystem.printVotingQuestions();
System.out.println("\nPrint voting 0 :");
votingSystem.printVoting(0);
System.out.println("\nPrint result 0 :");
votingSystem.printResult(0);
System.out.println("\nPrint voting 1 :");
votingSystem.printVoting(1);
System.out.println("\nPrint result 1 :");
votingSystem.printResult(1);

Person person = new Person("kc", "sd");
ArrayList<String> kcsdChoices0 = new ArrayList<String>();
ArrayList<String> kcsdChoices1 = new ArrayList<String>();
kcsdChoices0.add("Neither");
kcsdChoices1.add("Saturday");
votingSystem.vote(1, person, kcsdChoices1);
votingSystem.vote(0, person, kcsdChoices0);
votingSystem.vote(0, person, kcsdChoices0);

System.out.println("\nPrint voting 0 :");
votingSystem.printVoting(0);
System.out.println("\nPrint result 0 :");
votingSystem.printResult(0);
System.out.println("\nPrint voting 1 :");
votingSystem.printVoting(1);
System.out.println("\nPrint result 1 :");
votingSystem.printResult(1);

}
}
36 changes: 36 additions & 0 deletions RohamHayedi/lab4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import ir.huri.jcal.JalaliCalendar;

import java.util.Objects;

public class Vote {

private final Person person;
private final 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 instanceof Vote)) 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);
}
}
79 changes: 79 additions & 0 deletions RohamHayedi/lab4/Voting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
import ir.huri.jcal.JalaliCalendar;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.concurrent.atomic.AtomicInteger;

public class Voting {

private final int type;
private final String question;
private ArrayList<Person> voters;
private HashMap<String, HashSet<Vote>> polls;

public Voting(String question, int type, ArrayList<String> choices){
this.type = type;
this.question = question;
polls = new HashMap<>();
voters = new ArrayList<>();
for (String choice : choices){
polls.put(choice, new HashSet<>());
}
}

public void creatChoice(String choice){
polls.put(choice, new HashSet<>());
}

public void vote(Person person, ArrayList<String> choices){
if (type == 0 && choices.size() > 1) {
System.out.println(person + "-> This voting (" + question + ") accepts only single choices");
}
else {
if (!voters.contains(person)) {
voters.add(person);
Vote vote = new Vote(person, new JalaliCalendar().toString());
for (String choice : choices)
if (polls.containsKey(choice))
polls.get(choice).add(vote);
else {
System.out.println(person + "->The input (" + choice + ") is not among the choices");
}
}else{
System.out.println("The same person cannot vote twice");
}
}
}

public String getQuestion() {
return question;
}

public void printVoters() {
int i = 0;
for (Person person : voters) {
System.out.println(i + "-" + person);
}
}

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

public void printChoices(){
AtomicInteger i = new AtomicInteger(1);
polls.forEach((k,v) -> System.out.println(i.getAndIncrement() + "- " + k));
}

public void printResult(){
System.out.println(question);
for (Map.Entry<String, HashSet<Vote>> entry : polls.entrySet()) {
String choice = entry.getKey();
HashSet<Vote> votes = entry.getValue();
System.out.println(choice + " -> " + votes.size());
votes.forEach(vote -> System.out.println(vote.getPerson() + " " + vote.getDate()));
}
}
}
45 changes: 45 additions & 0 deletions RohamHayedi/lab4/VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
import java.util.ArrayList;
import java.util.HashSet;
import java.util.Set;

public class VotingSystem {

private ArrayList<Voting> votingList;

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

public void createVoting(String question, int type, ArrayList<String> choices ){
votingList.add(new Voting(question, type, choices));
}

public void deleteVoting(int index){
votingList.remove(index);
}

public void vote(int index, Person person, ArrayList<String> votes){
votingList.get(index).vote(person, votes);
}

public void printVotingQuestions() {
for (Voting voting : votingList){
System.out.println(voting.getQuestion());
}
}

public void printVoting(int index){
if (index < votingList.size()) {
System.out.println(votingList.get(index).getQuestion());
votingList.get(index).printChoices();
}else
System.out.println("Wrong index");
}

public void printResult(int index) {
if (index < votingList.size()) {
votingList.get(index).printResult();
}else
System.out.println("Wrong index");
}
}
32 changes: 32 additions & 0 deletions RohamHayedi/lab5/Part1-PaintWithPolymorphism/Circle.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
public class Circle extends Shape {

private double radius;

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

public double getRadius() {
return radius;
}

@Override
public void draw() {
System.out.println((char) 0x25EF);
}

@Override
public double calculateParameter() {
return 2 * 3.14 * radius;
}

@Override
public double calculateArea() {
return 3.14 * radius*radius;
}

@Override
public String toString() {
return "radius = " + radius;
}
}
40 changes: 40 additions & 0 deletions RohamHayedi/lab5/Part1-PaintWithPolymorphism/Paint.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
import java.util.ArrayList;
import java.util.logging.SocketHandler;

public class Paint {

private ArrayList<Shape> shapes;

public Paint(){
shapes = new ArrayList<>();
}

public void addShape(Shape shape){
shapes.add(shape);
}

public void drawAll(){
for(Shape shape : shapes){
shape.draw();
}
}

public void describeEqualSides(){
System.out.println("describeEqualSides::");
for (Shape shape : shapes){
if (shape instanceof Triangle) {
if (((Triangle) shape).isEquilateral())
System.out.println(shape);
}else if (shape instanceof Rectangle)
if (((Rectangle) shape).isSquare())
System.out.println(shape);
}
}

public void printAll(){
for (Shape shape : shapes){
System.out.println(shape);
}
}

}
20 changes: 20 additions & 0 deletions RohamHayedi/lab5/Part1-PaintWithPolymorphism/Polygon.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import java.util.ArrayList;

public abstract class Polygon extends Shape {

private ArrayList<Double> sides;

public Polygon(double... args){
sides = new ArrayList<>();
for (double side : args){
sides.add(side);
}
}

@Override
public abstract void draw();

public ArrayList<Double> getSides() {
return sides;
}
}
Loading