Skip to content

Commit

Permalink
Reto gelopfalcon#2 Validaciones: id > 0, name.length > 2 chars y no v…
Browse files Browse the repository at this point in the history
…acío
  • Loading branch information
fernandospr committed Jul 29, 2022
1 parent 1392e3e commit 7f0d35e
Showing 1 changed file with 22 additions and 9 deletions.
31 changes: 22 additions & 9 deletions PokemonFactory.sol
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,46 @@ contract PokemonFactory {
event eventNewPokemon(Pokemon pokemon);

struct Pokemon {
uint id;
string name;
uint id;
string name;
}

Pokemon[] private pokemons;

mapping (uint => address) public pokemonToOwner;
mapping (address => uint) ownerPokemonCount;

function createPokemon (string memory _name, uint _id) public {
function createPokemon (string memory _name, uint _id) public {
require(_id > 0, "id must be greater than 0.");
require(bytes(_name).length > 2, "name must be greater than 2 characters length.");
require(!isEmpty(_name), "name must not be empty.");
Pokemon memory pokemon = Pokemon(_id, _name);
pokemons.push(pokemon);
pokemonToOwner[_id] = msg.sender;
ownerPokemonCount[msg.sender]++;
emit eventNewPokemon(pokemon);
}

function isEmpty(string memory str) private pure returns (bool) {
bytes memory b = bytes(str);
for (uint i; i < b.length; i++) {
if (b[i] != 0x20) {
return false;
}
}
return true;
}

function getAllPokemons() public view returns (Pokemon[] memory) {
return pokemons;
return pokemons;
}


function getResult() public pure returns(uint product, uint sum){
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}
uint a = 1;
uint b = 2;
product = a * b;
sum = a + b;
}

}

0 comments on commit 7f0d35e

Please sign in to comment.