Skip to content

Instantly share code, notes, and snippets.

@Adophilus
Last active May 13, 2024 02:25
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Adophilus/3de064b43aca33c0c62d5a62633114b0 to your computer and use it in GitHub Desktop.
Save Adophilus/3de064b43aca33c0c62d5a62633114b0 to your computer and use it in GitHub Desktop.
a smart contract powering a restaurant
// 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