Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- pragma solidity ^0.5.2;
- pragma experimental ABIEncoderV2;
- contract voter {
- uint[] public votes;
- //store votes .. array of integers
- string[] public options;
- //option for which a user can cast a vote
- mapping (address => bool) hasVoted;
- constructor(string[] memory _options) public {
- options = _options;
- votes.length = options.length;
- }
- function vote(uint option) public {
- require(0 <= option && option < options.length, "Invalid option");
- require(!hasVoted[msg.sender], "account has already voted" );
- votes[option] = votes[option] + 1;
- hasVoted[msg.sender] = true;
- }
- function getOptions() public view returns (string[] memory ){
- return options;
- }
- function getVotes() public view returns (uint[] memory) {
- return votes;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement