pragma solidity ^0.4.21;
contract Election{
struct Canditate{
string name;
uint voteCount;
}
struct Voter{
bool authorized;
bool voted;
uint vote;
}
address public owner;
string public electionName;
mapping(address => Voter) public Voters;
Canditate[] public Canditates;
uint public totalVotes;
modifier ownerOnly(){
require(msg.sender == owner);
_;
}
function Election(string _name) public {
owner = msg.sender;
electionName = _name;
}
function getNumberCandidates() public view returns(uint) {
return Canditates.length;
}
function addCandidate(string _name) ownerOnly public {
Canditates.push(Canditate(_ name,0));
}
function authorize(address _person) ownerOnly public {
Voters[_person].authorized = true;
}
function vote(uint _voterIndex) public {
require(!Voters[msg.sender]. voted);
require(Voters[msg.sender]. authorized);
Voters[msg.sender].vote = _voterIndex;
Voters[msg.sender].voted = true;
Canditates[_voterIndex]. voteCount += 1;
totalVotes += 1;
}
function end() ownerOnly public {
selfdestruct(owner);
}
No comments:
Post a Comment