contract HelloWorld{
//data locations:
//storage
//memory
//stack
struct Person {
string name;
uint age;
uint height;
bool senior;
uint balance;
}
event personCreated(string name, bool senior);
event personDeleted(string name, bool senior, address deletedBy);
address public owner;
modifier onlyOwner(){
require(msg.sender == owner);
_; // Continue execution
}
constructor() public{
owner = msg.sender;
}
// address is the key in teh mapping
mapping(address => Person) private people;
address[] private creators;
function createPerson(string memory name, uint age, uint height) public payable {
require(age <= 150, "Age needs to be below 150");
require(msg.value >=1 ether);
//This create a person
Person memory newPerson;
newPerson.name = name;
newPerson.age = age;
newPerson.height = height;
newPerson.balance = msg.value;
if(age >= 65){
newPerson.senior = true;
}
else{
newPerson.senior = false;
}
insertPerson(newPerson);
creators.push(msg.sender);
//people[msg.sender] == newPerson;
assert(
keccak256(
abi.encodePacked(
people[msg.sender].name,
people[msg.sender].age,
people[msg.sender].height,
people[msg.sender].senior
)
)
//hash of the person that is in the people mapping from assert
==
keccak256(
abi.encodePacked(
newPerson.name,
newPerson.age,
newPerson.height,
newPerson.senior)));
emit personCreated(newPerson.name, newPerson.senior);
}
function insertPerson(Person memory newPerson) private{
address creator = msg.sender;
people[creator] = newPerson;
}
function getPerson() public view returns(string memory name, uint age, uint height, bool senior, uint balance){
address creator = msg.sender;
return (people[creator].name, people[creator].age, people[creator].height, people[creator].senior, people[creator].balance);
}
function deletePerson(address creator) public onlyOwner {
string memory name = people[creator].name;
bool senior = people[creator].senior;
// morao dodat ova dva gore da sejva locally ime i senior, zbog brisanja na liniji ispod
delete people[creator];
assert(people[creator].age == 0);
emit personDeleted(name, senior, msg.sender);
}
function getCreator(uint index) public view onlyOwner returns(address){
return creators[index];
}
}