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

The Traveling Salesmen_SY_80 #25

Open
wants to merge 1 commit into
base: main
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
23 changes: 23 additions & 0 deletions Candidate.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
package Buffer;
public class Candidate {
private String name;
private int votes;

public Candidate(String name) {
this.name = name;
votes = 0;
}

public String getName() {
return name;
}

public int getVotes() {
return votes;
}

public void addVote() {
votes++;
}
}

25 changes: 25 additions & 0 deletions Voters.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package Buffer;

public class Voters {
private String name;
private int ID;
private int passcode;

public Voters(int ID, int passcode, String name) {
this.ID = ID;
this.passcode = passcode;
this.name = name;
}

public String getName() {
return name;
}

public int getID() {
return ID;
}

public int getPasscode() {
return passcode;
}
}
72 changes: 72 additions & 0 deletions VotingSystem.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
package Buffer;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.NoSuchElementException;



public class VotingSystem {
private ArrayList<Candidate> candidates;
private ArrayList<Voters> voters;

public VotingSystem() {
candidates = new ArrayList<Candidate>();
voters = new ArrayList<Voters>();
}

public void addCandidate(String name) {
candidates.add(new Candidate(name));
}

public void addVoter(int id, int passcode, String name) {
voters.add(new Voters(id, passcode, name));
}

public boolean verifyVoter(int id, int passcode) {
for (Voters voter : voters) {
if (voter.getID() == id && voter.getPasscode() == passcode) {
return true;
}
}
return false;
}


public void castVote() {
Scanner input = new Scanner(System.in);
try{System.out.println("Enter your voter ID:");
int id = input.nextInt();
System.out.println("Enter your passcode:");
int passcode = input.nextInt();

if (verifyVoter(id, passcode)) {
System.out.println("Voter verified.");
System.out.println("Enter the name of the candidate you wish to vote for:");
String candidateName = input.next();
for (Candidate candidate : candidates) {
if (candidate.getName().trim().equals(candidateName)) {
candidate.addVote();
System.out.println("Vote cast for " + candidateName + ".");
return;
}
}
System.out.println("Candidate not found");
} else {
System.out.println("Voter not found or invalid passcode // The Voter has already casted Vote");
}
}catch (NoSuchElementException exception) {
System.out.println("InValid Entry ");
}
input.close();

}



public void printResults() {
System.out.println("Results:");
for (Candidate candidate : candidates) {
System.out.println(candidate.getName() + ": " + candidate.getVotes() + " votes.");
}
}
}
42 changes: 42 additions & 0 deletions VotingSystemTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package Buffer;

import java.io.File;
import java.util.Scanner;


public class VotingSystemTest {
public static void main(String[] args) throws Exception {
VotingSystem votingSystem = new VotingSystem();

// Add candidates
votingSystem.addCandidate("CandidateA") ;
votingSystem.addCandidate("CandidateB");
votingSystem.addCandidate("CandidateC");


// Read voters from CSV file
Scanner voterScanner = new Scanner(new File("D:\\Buffer 4.0\\Codes\\Buffer\\codes.csv"));
while (voterScanner.hasNextLine()) {
String[] voterInfo = voterScanner.nextLine().split(",");
int id = Integer.parseInt(voterInfo[0].trim());
int passcode = Integer.parseInt(voterInfo[1].trim());
String name = voterInfo[2].trim();
//int flag = Integer.parseInt(voterInfo[3].trim());
votingSystem.addVoter(id, passcode, name);
}
voterScanner.close();



// Cast votes
votingSystem.castVote();
votingSystem.castVote();
votingSystem.castVote();
votingSystem.castVote();
votingSystem.castVote();
votingSystem.castVote();

// Print results
votingSystem.printResults();
}
}
11 changes: 11 additions & 0 deletions codes.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
21, 96075, nandini, 0
17, 70661, anushri, 0
11, 84465, sourabhi, 0
1, 11111, aaaaa, 0
2, 22222, bbbbb, 0
3, 33333, ccccc, 0
4, 44444, ddddd, 0
5, 55555, eeeee, 0
6, 66666, fffff, 0
7, 77777, ggggg, 0
8, 88888, hhhhh, 0