Skip to content

Bonus Assignment #16058

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

Closed
Closed
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
62 changes: 62 additions & 0 deletions Bonus Assignment
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
pragma solidity ^0.8.0;

contract Voting {
struct Candidate {
uint id;
string name;
uint voteCount;
}
mapping(address => bool) public voters;
Candidate[] public candidates;
address public owner;
event Voted(uint indexed candidateId, address voter);

modifier onlyOwner() {
require(msg.sender == owner, "Only the owner can perform this action");
_;
}


constructor(string[] memory candidateNames) {
owner = msg.sender;
for (uint i = 0; i < candidateNames.length; i++) {
candidates.push(Candidate({
id: i,
name: candidateNames[i],
voteCount: 0
}));
}
}


function addCandidate(string memory name) public onlyOwner {
uint newId = candidates.length;
candidates.push(Candidate({
id: newId,
name: name,
voteCount: 0
}));
}

function vote(uint candidateId) public {
require(!voters[msg.sender], "You have already voted");
require(candidateId < candidates.length, "Invalid candidate ID");

voters[msg.sender] = true;
candidates[candidateId].voteCount++;

emit Voted(candidateId, msg.sender);
}


function getNumCandidates() public view returns (uint) {
return candidates.length;
}


function getCandidate(uint candidateId) public view returns (uint, string memory, uint) {
require(candidateId < candidates.length, "Invalid candidate ID");
Candidate memory candidate = candidates[candidateId];
return (candidate.id, candidate.name, candidate.voteCount);
}
}