Last active
May 13, 2024 02:25
-
-
Save adophilus/3de064b43aca33c0c62d5a62633114b0 to your computer and use it in GitHub Desktop.
a smart contract powering a restaurant
This file contains hidden or 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.24; | |
import {Strings} from "@openzeppelin/contracts/utils/Strings.sol"; | |
contract Ownable { | |
address owner; | |
constructor (){ | |
owner = msg.sender; | |
} | |
modifier ownerOnly() { | |
require(msg.sender == owner, "Only the owner can call this function"); | |
_; | |
} | |
} | |
contract Restaurant is Ownable { | |
struct Food { | |
string name; | |
uint price; | |
string description; | |
uint index; | |
} | |
string[] foodNames; | |
mapping(string => Food) menu; | |
function addToMenu( | |
string calldata _name, | |
uint _price, | |
string calldata _description | |
) public ownerOnly { | |
foodNames.push(_name); | |
Food memory food = Food(_name, _price, _description, foodNames.length - 1); | |
menu[_name] = food; | |
} | |
function removeFromMenu(string memory _name) public ownerOnly { | |
Food memory food = menu[_name]; | |
foodNames[food.index] = foodNames[foodNames.length - 1]; | |
foodNames.pop(); | |
delete menu[_name]; | |
} | |
function getMenu() public view returns (Food[] memory) { | |
Food[] memory _menu = new Food[](foodNames.length); | |
for (uint i = 0; i< foodNames.length; i++) { | |
Food memory currentFood = menu[foodNames[i]]; | |
_menu[i] = menu[currentFood.name]; | |
} | |
return _menu; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment