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

9831066 Mohammad Mahdi Nemati #11

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
441 changes: 441 additions & 0 deletions 9831066_MohammadMahdi_Nemati_Haravani/Session4/JalaliCalendar.java

Large diffs are not rendered by default.

151 changes: 151 additions & 0 deletions 9831066_MohammadMahdi_Nemati_Haravani/Session4/Main.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,151 @@
import java.util.*;


public class Main {

/**
* this method print menu
*/
public static void showMainMenu() {
System.out.println("[1] Add voting\n" +
"[2] Remove voting\n" +
"[3] See voting\n" +
"[4] All questions\n" +
"[5] Vote\n" +
"[6] See Voters\n" +
"[7] See Choices\n" +
"[8] Exit");
}

public static void main(String[] args) {

VotingSystem votingSystem = new VotingSystem();

System.out.println("Welcome to the voting system.");

Scanner scan = new Scanner(System.in);

ArrayList<String> choices = new ArrayList<>();
ArrayList<String> myChoices = new ArrayList<>();
String question;
String firstName;
String lastName;
int type;
int numberOfChoices;
int index;

while (true) {

showMainMenu();

int mainMenu = scan.nextInt();

choices.clear();
myChoices.clear();

switch (mainMenu) {
case 1:

System.out.println("Write the question of voting:");
scan.nextLine();
question = scan.nextLine();

do {
System.out.println("Enter the type of voting ( 0 for single and 1 for multi ):");
type = scan.nextInt();
} while (type != 0 && type != 1);

System.out.println("Enter the number of choices you want to add:");
numberOfChoices = scan.nextInt();
scan.nextLine();

for (int i = 0; i < numberOfChoices; i++) {
System.out.println("Enter your option:");
String temp = scan.nextLine();
choices.add(temp);
}

votingSystem.createVoting(question, type, choices);
System.out.println("The voting added successfully.");

break;
case 2:

System.out.println("Enter the index of the voting:");
index = scan.nextInt();
votingSystem.removeVoting(index);

break;
case 3:

System.out.println("Enter the index of the voting:");
index = scan.nextInt();
votingSystem.printResult(index);

break;
case 4:
votingSystem.printListOfVotingQuestion();
break;
case 5:

System.out.println("Enter the index of the voting:");
index = scan.nextInt();
System.out.println("Enter Your first name:");

scan.nextLine();
firstName = scan.nextLine();
System.out.println("Enter Your last name:");
lastName = scan.nextLine();

if (votingSystem.getType(index) == 0) {
int choose;
do {
System.out.println("This is a single voting. Press 1 for random vote or 2 for importing vote.");
choose = scan.nextInt();
} while (choose != 1 && choose !=2);

if (choose == 1) {
myChoices.add(votingSystem.giveRandomVote(index));
} else {
System.out.println("Write the chosen option:");
scan.nextLine();
myChoices.add(scan.nextLine());
}

} else {

System.out.println("This is a multi voting. Enter the number of options you want to chose:");
numberOfChoices = scan.nextInt();

scan.nextLine();
for (int i = 0; i < numberOfChoices; i++) {
System.out.println("Enter your option:");
String temp = scan.nextLine();
myChoices.add(temp);
}
}
votingSystem.vote(index, new Person(firstName, lastName), myChoices);
break;
case 6:
index = scan.nextInt();
votingSystem.seeVotersOfVoting(index);
break;
case 7:
index = scan.nextInt();
votingSystem.printChoicesOfVoting(index);
break;
case 8:
break;
default:
System.out.println("Invalid input.");
break;
}
if (mainMenu == 8) {
System.out.println("Thanks for working with us.");
return;
}
}

}

}
44 changes: 44 additions & 0 deletions 9831066_MohammadMahdi_Nemati_Haravani/Session4/Person.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
public class Person {

private String firstName;
private String lastName;

/**
* the class of person contains the information of a voter
* it keeps the voter name and last name and some method to
* access them
*/
public Person (String firstName,String lastName){

this.firstName = firstName;
this.lastName = lastName;

}

/**
* the getter method for last name of voter
* @return the last name
*/
public String getFirstName() {
return firstName;
}

/**
* the getter method for first name of voter
* @return the first name
*/
public String getLastName() {
return lastName;
}

/**
* an override of toString method for voter
* information
* @return a single string as voter information
*/
@Override
public String toString() {
return "Person : " + "First Name = " + getFirstName() + " , Last Name = " + getLastName();
}

}
65 changes: 65 additions & 0 deletions 9831066_MohammadMahdi_Nemati_Haravani/Session4/Vote.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
import java.util.Date;

public class Vote {

private Person personInformation;
private String dateOfVote;

/**
* the constructor of vote class
* @param person an instance of person class
*/
public Vote (Person person){

personInformation = person;

JalaliCalendar jDate = new JalaliCalendar();
Date date = new Date();

dateOfVote = jDate.getYear()+"/"+jDate.getMonth()+"/"+jDate.getDay()+" "+date.getHours()+":"+date.getMinutes()+":"+date.getSeconds() ;

}

/**
* simple getter method for getting the person
* @return instance of person class in this vote
*/
public Person getPersonInformation() {
return personInformation;
}

/**
* simple getter method for getting the date
* of the vote
* @return vote date as a string
*/
public String getDateOfVote() {
return dateOfVote;
}

/**
* the override hashcode for faster search
* @return the number in hashTable
*/
@Override
public String toString(){
return personInformation.toString()+" "+dateOfVote;
}

/**
* and override of equals method for checking votes
* @param o the vote
* @return if it is new or not
*/
@Override
public boolean equals(Object o) {

if (o == null) {
return false;
} else if (this == o) {
return true;
} else return personInformation.toString().equals(o.toString());

}

}
Loading