-
-
Save shreyvijayvargiya/f77e3ccfbb2f4d8196ff172fbb5c6640 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: MIT | |
pragma solidity ^0.8; | |
contract VotingApp { | |
mapping(address => uint256) public votesReceived; | |
address[] public candidateList; | |
constructor(address[] memory candidateNames){ | |
candidateList = candidateNames; | |
} | |
function totalVotesFor(address candidate) view public returns(uint256){ | |
require(validateCandidate(candidate), "Not a valid candidate"); | |
return votesReceived[candidate]; | |
} | |
function voteForCandidates(address candidate) public{ | |
require(validateCandidate(candidate), "Not a valid candidate"); | |
votesReceived[candidate] += 1; | |
} | |
function validateCandidate(address candidate) view public returns(bool isCandidateValid){ | |
for(uint i= 0; i < candidateList.length; i++){ | |
if(candidateList[i] == candidate){ | |
return isCandidateValid = true; | |
}else { | |
return isCandidateValid = false; | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment