Guest User

Untitled

a guest
Jun 20th, 2018
77
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.30 KB | None | 0 0
  1. pragma solidity^0.4.0;
  2.  
  3.  
  4. //Create a contract named DogContract
  5. contract DogContract{
  6. //Create a struct named Dog with a string name
  7. //and an unassigned integer age.
  8. struct Dog {
  9. string name;
  10. uint age;
  11. }
  12. //Dog is it is a struct and thus can be called as an array,
  13. //The Dog Array is named dogs.
  14. Dog[] dogs;
  15.  
  16. // Keep Track of the owner by using a mapping structure to have key point to an
  17. //unassigned integer for the index position in the array.
  18. mapping(address => uint) ownerToDog;
  19.  
  20.  
  21. //Setter function to add a dog. The input parameters are with a string name
  22. //and an unassigned integer age
  23. function addDog(string _name, uint _age){
  24.  
  25. //create owner variable of type address equat to the transactor.
  26. address owner = msg.sender;
  27.  
  28. // Push input for the transactor into the dogs array.
  29. //This will return the id of the dog in the list (returns the index possition in the array)
  30. // therefore we stroe it as an unsigned integer named id
  31. uint id = dogs.push(Dog(_name, _age));
  32.  
  33.  
  34. ownerToDog[owner]=id;
  35. }
  36. function getDog() returns (string){
  37. address owner = msg.sender;
  38. uint id = ownerToDog[owner];
  39. return dogs[id - 1].name;
  40. }
  41. }
Add Comment
Please, Sign In to add comment