// SPDX-License-Identifier: MIT // Author: https://github.com/jspruance pragma solidity ^0.8.11; contract VendingMachine { address public owner; mapping(address => uint256) public balances; constructor() { owner = msg.sender; // deployer of contract to be set = owner balances[address(this)] = 50; // initial bal (we need to start with something in it) } function getMachineBalance() public view returns (uint256) { return balances[address(this)]; } // Let the owner restock the vending machine function restock(uint256 amount) public { require(msg.sender == owner, "Only the owner can restock."); balances[address(this)] += amount; } // Purchase donuts from the vending machine function purchase(uint256 amount) public payable { require( msg.value >= amount * 2 ether, "You must pay at least 2 ETH per donut" ); require( balances[address(this)] >= amount, "Not enough donuts in stock to complete this purchase" ); balances[address(this)] -= amount; balances[msg.sender] += amount; } }