Skip to content

Instantly share code, notes, and snippets.

@shreyvijayvargiya
Created May 5, 2022 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shreyvijayvargiya/f77e3ccfbb2f4d8196ff172fbb5c6640 to your computer and use it in GitHub Desktop.
Save shreyvijayvargiya/f77e3ccfbb2f4d8196ff172fbb5c6640 to your computer and use it in GitHub Desktop.
// 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