-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvotingSystem.sol
More file actions
52 lines (42 loc) · 1.67 KB
/
Copy pathvotingSystem.sol
File metadata and controls
52 lines (42 loc) · 1.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.33;
import "@openzeppelin/contracts/access/Ownable.sol";
contract VotingSystem is Ownable {
constructor() Ownable(msg.sender) {} // <-- Fix added here
struct Candidate {
string name;
uint256 voteCount;
}
mapping(address => bool) public hasVoted;
mapping(string => bool) private _candidateNames;
Candidate[] public candidates;
event CandidateAdded(string name, uint256 index);
event Voted(address voter, uint256 candidateIndex);
function addCandidate(string memory _name) public onlyOwner {
require(!_candidateNames[_name], "Candidate already exists");
_candidateNames[_name] = true;
candidates.push(Candidate(_name, 0));
emit CandidateAdded(_name, candidates.length - 1);
}
function vote(uint256 _candidateIndex) public virtual {
require(!hasVoted[msg.sender], "Already voted");
require(_candidateIndex < candidates.length, "Invalid candidate");
candidates[_candidateIndex].voteCount++;
hasVoted[msg.sender] = true;
emit Voted(msg.sender, _candidateIndex);
}
function getCandidateCount() public view returns (uint256) {
return candidates.length;
}
function leadingCandidate() public view returns (uint256) {
uint256 maxVotes = 0;
uint256 leadingIndex = 0;
for (uint256 i = 0; i < candidates.length; i++) {
if (candidates[i].voteCount > maxVotes) {
maxVotes = candidates[i].voteCount;
leadingIndex = i;
}
}
return leadingIndex;
}
}