Created
September 13, 2023 17:17
-
-
Save Oleksii909/0e918250cd7c127cf44945d09438eb55 to your computer and use it in GitHub Desktop.
Lecture 6 Blockchain Homework
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// SPDX-License-Identifier: GPL-3.0 | |
pragma solidity >0.8.2; | |
// unsigned integer | |
contract ContactBook { | |
// dynamic data types | |
string public myName; | |
string[] names; | |
// static data types | |
uint256 public myAge; | |
uint256[] ages; | |
address public myAddress; | |
address[] addresses; | |
mapping(address => string) public addressesToNames; | |
constructor(address _address,uint256 _age, string memory _name) public { | |
myAddress = _address; | |
setAge(_age); | |
setName(_name); | |
} | |
function addContact(address _address, uint256 _age, string memory _name) public onlyOwner { | |
names.push(_name); | |
ages.push(_age); | |
addresses.push(_address); | |
addressesToNames[_address]=_name; | |
} | |
function getNameByIndex(uint256 _index) view public returns(string memory){ | |
return names[_index]; | |
} | |
function getAgeByIndex(uint256 _index) view public returns(uint256){ | |
return ages[_index]; | |
} | |
function getAddressByIndex(uint256 _index) view public returns(address){ | |
return addresses[_index]; | |
} | |
/** | |
* @dev Store value in variable | |
* @param _num value to set | |
*/ | |
function setAge(uint256 _num) public onlyOwner{ | |
myAge = _num; | |
} | |
function setName(string memory _name) public onlyOwner{ | |
myName = _name; | |
} | |
function setAddress(address _address) public onlyOwner { | |
myAddress = _address; | |
} | |
function hello() external view returns (string memory){ | |
return string.concat("Hello ",myName); | |
} | |
modifier onlyOwner(){ | |
require(msg.sender == myAddress); | |
_; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment