-
Notifications
You must be signed in to change notification settings - Fork 0
/
Voting.sol
30 lines (25 loc) · 887 Bytes
/
Voting.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
pragma solidity ^0.5.0;
contract Voting {
mapping(bytes32 => uint256) public votesReceived;
bytes32[] public candidateList;
event newVoting(bytes32 candidate);
constructor(bytes32[] memory candidateNames) public {
candidateList = candidateNames;
}
function totalVotesFor(bytes32 candidate) public view returns(uint256) {
require(validCandidate(candidate));
return votesReceived[candidate];
}
function voteForCandidate(bytes32 candidate) public {
require(validCandidate(candidate));
votesReceived[candidate] += 1;
emit newVoting(candidate);
}
function validCandidate(bytes32 candidate) public view returns(bool) {
for(uint i = 0 ; i < candidateList.length ; i++){
if(candidateList[i] == candidate){
return true;
}
}
}
}