Skip to content

Instantly share code, notes, and snippets.

@minhth1905
Last active May 13, 2024 11:06
Show Gist options
  • Save minhth1905/4b6208372fc5e7343b5ce1fb6d42c942 to your computer and use it in GitHub Desktop.
Save minhth1905/4b6208372fc5e7343b5ce1fb6d42c942 to your computer and use it in GitHub Desktop.
pragma solidity ^0.5.0;
pragma experimental ABIEncoderV2;
contract Money {
struct People{
uint id;
string name;
uint amount;
}
mapping (uint => People) public peoples;
event votedEvent(uint indexed _candidateId);
uint public candidateConut;
constructor() public {
candidateConut = 0;
addCandidate("Holder 1");
addCandidate("Holder 2");
}
function addCandidate(string memory _name) public {
peoples[candidateConut] = People(candidateConut,_name,0);
candidateConut++;
}
function addManyCandidate(string[] memory names) public {
for(uint i = 0; i < names.length; i++) {
addCandidate(names[i]);
}
}
//return Single structure
function get(uint _candidateId) public view returns(People memory) {
return peoples[_candidateId];
}
//return Array of structure Value
function getPeople() public view returns (uint[] memory, string[] memory,uint[] memory){
uint[] memory id = new uint[](candidateConut);
string[] memory name = new string[](candidateConut);
uint[] memory amount = new uint[](candidateConut);
for (uint i = 0; i < candidateConut; i++) {
People storage people = peoples[i];
id[i] = people.id;
name[i] = people.name;
amount[i] = people.amount;
}
return (id, name,amount);
}
//return Array of structure
function getPeoples() public view returns (People[] memory){
People[] memory id = new People[](candidateConut);
for (uint i = 0; i < candidateConut; i++) {
People storage people = peoples[i];
id[i] = people;
}
return id;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment