Commit 696a3ff7 authored by mahdikarami0111's avatar mahdikarami0111

initial commit

parents
Pipeline #5932 failed with stages
# Default ignored files
/shelf/
/workspace.xml
<html>Simple <b>Java</b> application that includes a class with <code>main()</code> method</html>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="Encoding">
<file url="PROJECT" charset="UTF-8" />
</component>
</project>
\ No newline at end of file
<component name="libraryTable">
<library name="JalaliCalendar-1.3.1">
<CLASSES>
<root url="jar://D:/Session 4/Attachments-Session4/JalaliCalendar-1.3.1.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</component>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectKey">
<option name="state" value="project://e2804f05-5315-4fc6-a121-c522a6c26470" />
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_15" default="true" project-jdk-name="15" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ProjectModuleManager">
<modules>
<module fileurl="file://$PROJECT_DIR$/APW_S4.iml" filepath="$PROJECT_DIR$/APW_S4.iml" />
</modules>
</component>
</project>
\ No newline at end of file
<template>
<input-field default="com.company">IJ_BASE_PACKAGE</input-field>
</template>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$/.." vcs="Git" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<module type="JAVA_MODULE" version="4">
<component name="NewModuleRootManager" inherit-compiler-output="true">
<exclude-output />
<content url="file://$MODULE_DIR$">
<sourceFolder url="file://$MODULE_DIR$/src" isTestSource="false" />
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="library" name="JalaliCalendar-1.3.1" level="project" />
</component>
</module>
\ No newline at end of file
package com.company;
import java.util.ArrayList;
import java.util.Random;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
VotingSystem system = new VotingSystem();
ArrayList<String> temp = new ArrayList<>();
temp.add("A1");
temp.add("A2");
temp.add("A3");
temp.add("A4");
system.createVoting("Q1",1,temp);
temp.clear();
temp.add("B1");
temp.add("B2");
temp.add("B3");
temp.add("B4");
system.createVoting("Q2",0,temp);
temp.clear();
temp.add("C1");
temp.add("C2");
temp.add("C3");
system.createVoting("Q3",0,temp);
Scanner sc = new Scanner(System.in);
ArrayList<String> answers = new ArrayList<>();
String name,tempanswer;
Random r = new Random();
int command;
while (true){
System.out.println("""
1.print votings
2.new vote""");
command = sc.nextInt();
switch (command){
case 1 ->{
for(Voting voting : system.getVotingList()){
voting.printResults();
voting.printVotes();
}
}
case 2 ->{
sc.nextLine();
System.out.print("first name: ");
name = sc.nextLine();
System.out.print("Last name: ");
Person person = new Person(name,sc.nextLine());
system.printVotings();
System.out.print("choose a voting: ");
int index = sc.nextInt();
Voting currentVoting = system.getVoting(index);
currentVoting.printPolls();
sc.nextLine();
int counter = 0;
while (counter < currentVoting.getPolls().keySet().size()){
System.out.println("Choose your answers\nEnter 0 to finish\nEnter 1 for random answer");
tempanswer = sc.nextLine();
if(tempanswer.equals("0")){
break;
}
if(tempanswer.equals("1")){
int j, i = 0;
j = r.nextInt(currentVoting.getPolls().keySet().size());
for(String key : currentVoting.getPolls().keySet()){
if(i == j){
answers.add(key);
}
i++;
}
}
else {
if(answers.contains(tempanswer)){
System.out.println("Repetitive answer");
}
else {
answers.add(tempanswer);
}
}
counter++;
}
system.vote(person,answers,index);
answers.clear();
}
}
}
}
}
\ No newline at end of file
package com.company;
/**
* a class for storing voter's information
*/
public class Person {
private final String firstName;
private final String lastName;
/**
* constructor for person class
* @param firstName voter's first name
* @param lastName voter's last name
*/
public Person(String firstName,String lastName){
this.firstName = firstName;
this.lastName = lastName;
}
public String getFirstName() {
return firstName;
}
public String getLastName() {
return lastName;
}
/**
* Equal method to check if two voters are the sameperson
* @param person second voter that we want to check if is the same voter as first voter
* @return returns true if equal ,flase if not
*/
public boolean equals(Person person){
return (firstName.equals(person.firstName) && lastName.equals(person.lastName));
}
@Override
public String toString(){
return firstName + " " +lastName;
}
}
package com.company;
/**
* a class for storing a vote and its information
*/
public class Vote {
Person person;
String date;
/**
* constructor for vote class
* @param person the voter
* @param date the date when the person submitted his vote
*/
public Vote(Person person,String date){
this.person = person;
this.date = date;
}
public Person getPerson() {
return person;
}
public String getDate() {
return date;
}
/**
* prints vote date and the voters information
*/
public void print(){
System.out.println("Voter : " + person.getFirstName() + " " + person.getLastName() + " Date : " + date);
}
@Override
public boolean equals(Object o){
if(this == o)return true;
if(!(o instanceof Vote))return false;
return this.person.equals(((Vote) o).person) && this.date.equals(((Vote) o).date);
}
@Override
public int hashCode(){
int result = 17;
result += 37 * person.getFirstName().hashCode();
result += 37 *person.getLastName().hashCode();
result += 37 *date.hashCode();
return result;
}
}
package com.company;
import ir.huri.jcal.JalaliCalendar;
import java.util.*;
/**
* a class tha simulates a voting
* stores voting question, choices, voters and all the votes that have been done
*/
public class Voting {
private int type;
private String question;
private ArrayList<Person> voters;
private HashMap<String, HashSet<Vote>> polls;
/**
* contructor for Voting class
* @param type voters can choose one or more options(0 for one option and 1 for more than one)
* @param question voting's question
*/
public Voting(int type, String question){
voters = new ArrayList<>();
polls = new HashMap<>();
this.type = type;
this.question = question;
}
/**
* adds a new choice to the current voting
* @param newPoll the new choice we want to add
*/
public void createPoll(String newPoll){
polls.put(newPoll,new HashSet<Vote>());
}
/**
* submits a vote on a choice that voter made
* @param person the person that submitted the vote
* @param answers a list ofchoices that have been submitted by voter
*/
public void vote(Person person,ArrayList<String> answers) {
for(Person guy : voters){
if(guy.equals(person)){
System.out.println("You have already voted");
return;
}
}
if (answers.size() > 1 && type == 0) {
System.out.println("You can only submit one vote");
return;
}
for (String answer : answers) {
System.out.println("answer : "+answer);
if (polls.get(answer) == null) {
System.out.println("answer " + answer + " is not valid");
return;
}
JalaliCalendar date = new JalaliCalendar(new GregorianCalendar());
Vote temp = new Vote(person, date.toString());
polls.get(answer).add(temp);
voters.add(person);
System.out.println("Vote submitted");
}
}
public ArrayList<Person> getVoters() {
return voters;
}
public HashMap<String, HashSet<Vote>> getPolls() {
return polls;
}
/**
* prints voting choices and number of votes on each choice
*/
public void printResults(){
System.out.println("Voting: " +question);
polls.forEach((key,value) -> System.out.println("choice : " + key + " Number of votes : " + value.size()));
}
/**
* prints all individual votes that have been submitted
*/
public void printVotes(){
polls.forEach((poll,votes) ->{
for(Vote vote : votes){
System.out.print(poll + " : ");
vote.print();
}
});
}
public String getQuestion() {
return question;
}
/**
* prints all the choices for current poll
*/
public void printPolls(){
polls.forEach((poll,votes) -> System.out.println(poll));
}
}
package com.company;
import java.util.ArrayList;
/**
* a class to keep all the votings in it
*/
public class VotingSystem {
private ArrayList<Voting> votingList = new ArrayList<>();
/**
* adds a new voting to the system
* @param question question of the new voting
* @param type type of new voting
* @param polls choices of the new vote
*/
public void createVoting(String question,int type,ArrayList<String> polls){
Voting temp = new Voting(type,question);
for(String poll : polls){
temp.createPoll(poll);
}
votingList.add(temp);
}
public ArrayList<Voting> getVotingList() {
return votingList;
}
/**
* gets an index and returns the voting with that index
* @param index index of the voting that we want
* @return returns the voting in the given index
*/
public Voting getVoting(int index){
if(index > votingList.size()){
System.out.println("Invalid Voting");
return null;
}
return votingList.get(index);
}
/**
* getsindex of the voting and makes new vote on that voting
* @param person voter
* @param answers nswers submited by the voter
* @param index index of the voting we want to submit answers on
*/
public void vote(Person person,ArrayList<String> answers,int index){
if(getVoting(index) == null){
return;
}
getVoting(index).vote(person,answers);
}
/**
* prints results of voting in the given index
* @param index index of the voting
*/
public void getResults(int index){
if(getVoting(index) == null){
return;
}
getVoting(index).printResults();
}
/**
* prints results of all votings
*/
public void printVotings(){
for(Voting voting : votingList){
System.out.println(voting.getQuestion());
}
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment