-
Notifications
You must be signed in to change notification settings - Fork 88
/
VendingMachine.sol
34 lines (28 loc) · 1.12 KB
/
VendingMachine.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.11;
contract VendingMachine {
// state variables
address public owner;
mapping (address => uint) public donutBalances;
// set the owner as th address that deployed the contract
// set the initial vending machine balance to 100
constructor() {
owner = msg.sender;
donutBalances[address(this)] = 100;
}
function getVendingMachineBalance() public view returns (uint) {
return donutBalances[address(this)];
}
// Let the owner restock the vending machine
function restock(uint amount) public {
require(msg.sender == owner, "Only the owner can restock.");
donutBalances[address(this)] += amount;
}
// Purchase donuts from the vending machine
function purchase(uint amount) public payable {
require(msg.value >= amount * 2 ether, "You must pay at least 2 ETH per donut");
require(donutBalances[address(this)] >= amount, "Not enough donuts in stock to complete this purchase");
donutBalances[address(this)] -= amount;
donutBalances[msg.sender] += amount;
}
}