Created
March 25, 2023 20:54
-
-
Save reselbob/b4118e0d1f49a5c20e8e3dc1b55c25b5 to your computer and use it in GitHub Desktop.
Solidity Smart Contracts for OOP Developers
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: MIT | |
pragma solidity ^0.8.9.0; | |
import "./MessageRepeater.sol"; | |
contract AmazingMessageRepeater is MessageRepeater { | |
function repeatMessage(uint256 repetitions) public view virtual override returns (string[] memory){ | |
string[] memory arr; | |
for (uint i = 0; i < repetitions; i++) { | |
arr = addElementToStringArray(arr, toUpperCase(getMessage())); | |
//arr.push(myMessage); | |
} | |
return arr; | |
} | |
function toUpperCase(string memory str) private pure returns (string memory) { | |
bytes memory strBytes = bytes(str); | |
for (uint i = 0; i < strBytes.length; i++) { | |
if (strBytes[i] >= 0x61 && strBytes[i] <= 0x7A) { | |
strBytes[i] = bytes1(uint8(strBytes[i]) - 32); | |
} | |
} | |
return string(strBytes); | |
} | |
} |
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: MIT | |
pragma solidity ^0.8.9.0; | |
interface Messageable { | |
function setMessage(string memory message) external; | |
function getMessage() external returns (string memory) ; | |
function getMessage(uint256 repetitions) external returns (string[] memory ); | |
function repeatMessage(uint256 repetitions) external returns (string[] memory ) ; | |
} |
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: MIT | |
pragma solidity ^0.8.9.0; | |
import "./Messageable.sol"; | |
contract MessageRepeater is Messageable { | |
string private myMessage; | |
address private creator; | |
constructor() { | |
creator = msg.sender; | |
} | |
function setMessage(string memory message) public { | |
myMessage = message; | |
} | |
function getMessage() public view returns (string memory) { | |
return myMessage; | |
} | |
function getMessage(uint256 repetitions) public view returns (string[] memory) { | |
return repeatMessage(repetitions); | |
} | |
function repeatMessage(uint256 repetitions) public virtual view returns (string[] memory){ | |
string[] memory arr; | |
for (uint i = 0; i < repetitions; i++) { | |
arr = addElementToStringArray(arr, myMessage); | |
} | |
return arr; | |
} | |
function addElementToStringArray(string[] memory oldArray, string memory newElement) internal pure returns (string[] memory) { | |
string[] memory newArray = new string[](oldArray.length + 1); | |
for (uint i = 0; i < oldArray.length; i++) { | |
newArray[i] = oldArray[i]; | |
} | |
newArray[oldArray.length] = newElement; | |
return newArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment