-
Notifications
You must be signed in to change notification settings - Fork 5
/
vote.sol
36 lines (28 loc) · 931 Bytes
/
vote.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
pragma solidity >=0.4.22 <0.9.0;
contract Voting {
/*
The key of the mapping is candidate name stored as type bytes32 and value is
an unsigned integer to store the vote count
*/
mapping (bytes32 => uint8) public votesReceived;
bytes32[] public candidateList;
function Vote_candi(bytes32[] memory candidateNames) public{
candidateList = candidateNames;
}
// Total votes received by candidate
function totalVotesFor(bytes32 candidate) public returns (uint8) {
return votesReceived[candidate];
}
// Incrementing vote count for candidate
function voteForCandidate(bytes32 candidate) public{
votesReceived[candidate] += 1;
}
function validCandidate(bytes32 candidate) public returns (bool) {
for(uint i = 0; i < candidateList.length; i++) {
if (candidateList[i] == candidate) {
return true;
}
}
return false;
}
}