Created
June 3, 2018 10:29
-
-
Save niharrs/93b8531929bd5c558a5e8796fa5d1470 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
pragma solidity ^0.4.2; | |
contract Election { | |
//Model candidate | |
struct Candidate { | |
uint id; | |
string name; | |
uint voteCount; | |
} | |
//Store accounts that voted | |
mapping(address => bool) public voters; | |
//Store candidates | |
mapping(uint => Candidate) public candidates; //Key Value pair | |
//Fetch candidates | |
//Store candidate count | |
uint public candidatesCount; | |
//Voted event | |
event votedEvent (uint indexed _candidateId); | |
function Election() public { | |
addCandidate("Friends"); | |
addCandidate("How I Met Your Mother"); | |
} | |
function addCandidate (string _name) private { | |
candidatesCount++; | |
candidates[candidatesCount] = Candidate(candidatesCount, _name, 0); | |
} | |
function vote (uint _candidateId) public { | |
//Require that they haven't voted before | |
require(!voters[msg.sender]); | |
//Require a valid candidate | |
require(_candidateId > 0 && _candidateId <= candidatesCount); | |
//Record voter has voted | |
voters[msg.sender] = true; | |
//Update candidate vote count | |
candidates[_candidateId].voteCount++; | |
//Trigger voted event | |
votedEvent(_candidateId); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment