-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcoffee_machine.sol
103 lines (89 loc) · 3.49 KB
/
coffee_machine.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.18;
contract CoffeeWorkflow {
// States and their respective boolean values
mapping(string => bool) private states;
// Events for each transition
event BoilWaterEvent();
event GrindBeansEvent();
event BrewCoffeeEvent();
event PourCoffeeEvent();
event SendEvent();
event CreditEvent();
constructor() {
// Initialize states
states["Water"] = true;
states["BoiledWater"] = false;
states["CoffeeBeans"] = true;
states["GroundCoffee"] = false;
states["Filter"] = true;
states["CoffeeInPot"] = false;
states["Pending"] = true;
states["Sent"] = false;
states["Payment"] = false;
states["Cup"] = true;
}
// Helper to check state
modifier stateIsTrue(string memory state) {
require(states[state], "State not active");
_;
}
// Helper to set states
function setState(string memory state, bool value) private {
require(states[state] != value, "State already set");
states[state] = value;
}
// Transition: Boil Water
function boilWater() external stateIsTrue("Water") {
setState("Water", false);
setState("BoiledWater", true);
emit BoilWaterEvent();
}
// Transition: Grind Beans
function grindBeans() external stateIsTrue("CoffeeBeans") {
setState("CoffeeBeans", false);
setState("GroundCoffee", true);
emit GrindBeansEvent();
}
// Transition: Brew Coffee
function brewCoffee() external stateIsTrue("BoiledWater") stateIsTrue("GroundCoffee") stateIsTrue("Filter") {
setState("BoiledWater", false);
setState("GroundCoffee", false);
setState("Filter", false);
setState("CoffeeInPot", true);
emit BrewCoffeeEvent();
}
// Transition: Pour Coffee
function pourCoffee() external stateIsTrue("Payment") stateIsTrue("CoffeeInPot") stateIsTrue("Cup") {
setState("CoffeeInPot", false);
setState("Cup", false);
emit PourCoffeeEvent();
}
// Transition: Send
function send() external stateIsTrue("Pending") {
setState("Pending", false);
setState("Sent", true);
emit SendEvent();
}
// Transition: Credit
function credit() external stateIsTrue("Sent") {
setState("Sent", false);
setState("Payment", true);
emit CreditEvent();
}
// View current states for debugging or inspection
function viewStates() external view returns (string memory) {
string memory result = "Current States: \n";
if (states["Water"]) result = string(abi.encodePacked(result, "Water\n"));
if (states["BoiledWater"]) result = string(abi.encodePacked(result, "BoiledWater\n"));
if (states["CoffeeBeans"]) result = string(abi.encodePacked(result, "CoffeeBeans\n"));
if (states["GroundCoffee"]) result = string(abi.encodePacked(result, "GroundCoffee\n"));
if (states["Filter"]) result = string(abi.encodePacked(result, "Filter\n"));
if (states["CoffeeInPot"]) result = string(abi.encodePacked(result, "CoffeeInPot\n"));
if (states["Pending"]) result = string(abi.encodePacked(result, "Pending\n"));
if (states["Sent"]) result = string(abi.encodePacked(result, "Sent\n"));
if (states["Payment"]) result = string(abi.encodePacked(result, "Payment\n"));
if (states["Cup"]) result = string(abi.encodePacked(result, "Cup\n"));
return result;
}
}