-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElection.sol
53 lines (42 loc) · 1.33 KB
/
Election.sol
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
53
pragma solidity 0.4.24;
contract Election {
struct Candidate {
uint id;
string name;
uint voteCount;
}
bool goingon = true;
mapping(address => bool) public voters;
mapping(uint => Candidate) public candidates;
uint public candidatesCount;
event votedEvent (
uint indexed _candidateId
);
constructor () public {
addCandidate("Candidate 1");
addCandidate("Candidate 2");
addCandidate("Candidate 3");
addCandidate("Candidate 4");
addCandidate("Candidate 5");
addCandidate("Candidate 6");
addCandidate("Candidate 7");
addCandidate("Candidate 8");
addCandidate("Candidate 9");
addCandidate("Candidate 10");
}
function addCandidate (string memory _name) private {
candidatesCount ++;
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0);
}
function end () public {
goingon = false;
}
function vote (uint _candidateId) public {
require(!voters[msg.sender],"Already voted");
require(_candidateId > 0 && _candidateId <= candidatesCount,"Invalid candidate");
require(goingon,"Election ended");
voters[msg.sender] = true;
candidates[_candidateId].voteCount ++;
emit votedEvent(_candidateId);
}
}