Skip to content

Instantly share code, notes, and snippets.

@Maxcutex
Created April 10, 2019 10:49
Show Gist options
  • Save Maxcutex/4cb73f3c34c38014aba9d42eecc8aa87 to your computer and use it in GitHub Desktop.
Save Maxcutex/4cb73f3c34c38014aba9d42eecc8aa87 to your computer and use it in GitHub Desktop.
Election Project: complete file
pragma solidity ^0.4.25;
contract Election {
// Model a Candidate
struct Candidate {
uint id;
string name;
uint voteCount;
}
// Store accounts that have voted
mapping(address => bool) public voters;
// Read/write candidates
mapping(uint => Candidate) public candidates;
// Store Candidates Count
uint public candidatesCount;
function Election () public {
addCandidate("Candidate 1");
addCandidate("Candidate 2");
}
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 that voter has voted
voters[msg.sender] = true;
// update candidate vote Count
candidates[_candidateId].voteCount ++;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment