-
Notifications
You must be signed in to change notification settings - Fork 0
/
ATM.ixx
198 lines (197 loc) · 5.52 KB
/
ATM.ixx
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
module;
export module ATM;
import receiver;
import sender;
import withdraw;
import dispatcher;
import <string>;
export namespace messaging
{
class ATM
{
receiver incoming;
sender bank;
sender interface_hardware;
void (ATM::*state)();
std::string account;
unsigned withdrawal_amount;
std::string pin;
void process_withdrawal()
{
incoming.wait()
.handle<withdraw_ok>(
[&](withdraw_ok const& msg)
{
interface_hardware.send(
issue_money(withdrawal_amount));
bank.send(
withdrawal_processed(account,withdrawal_amount));
state=&ATM::done_processing;
}
)
.handle<withdraw_denied>(
[&](withdraw_denied const& msg)
{
interface_hardware.send(display_insufficient_funds());
state=&ATM::done_processing;
}
)
.handle<cancel_pressed>(
[&](cancel_pressed const& msg)
{
bank.send(
cancel_withdrawal(account,withdrawal_amount));
interface_hardware.send(
display_withdrawal_cancelled());
state=&ATM::done_processing;
}
);
}
void process_balance()
{
incoming.wait()
.handle<balance>(
[&](balance const& msg)
{
interface_hardware.send(display_balance(msg.amount));
state=&ATM::wait_for_action;
}
)
.handle<cancel_pressed>(
[&](cancel_pressed const& msg)
{
state=&ATM::done_processing;
}
);
}
void wait_for_action()
{
interface_hardware.send(display_withdrawal_cancelled());
incoming.wait()
.handle<withdraw_pressed>(
[&](withdraw_pressed const& msg)
{
withdrawal_amount=msg.amount;
bank.send(withdraw(account,msg.amount,incoming));
state=&ATM::process_withdrawal;
}
)
.handle<balance_pressed>(
[&](balance_pressed const& msg)
{
bank.send(get_balance(account,incoming));
state=&ATM::process_balance;
}
)
.handle<cancel_pressed>(
[&](cancel_pressed const& msg)
{
state=&ATM::done_processing;
}
);
}
void verifying_pin()
{
incoming.wait()
.handle<pin_verified>(
[&](pin_verified const& msg)
{
state=&ATM::wait_for_action;
}
)
.handle<pin_incorrect>(
[&](pin_incorrect const& msg)
{
interface_hardware.send(
display_pin_incorrect_message());
state=&ATM::done_processing;
}
)
.handle<cancel_pressed>(
[&](cancel_pressed const& msg)
{
state=&ATM::done_processing;
}
);
}
void getting_pin()
{
incoming.wait()
.handle<digit_pressed>(
[&](digit_pressed const& msg)
{
unsigned const pin_length=4;
pin+=msg.digit;
if(pin.length()==pin_length)
{
bank.send(verify_pin(account,pin,incoming));
state=&ATM::verifying_pin;
}
}
)
.handle<clear_last_pressed>(
[&](clear_last_pressed const& msg)
{
if(!pin.empty())
{
pin.pop_back();
}
}
)
.handle<cancel_pressed>(
[&](cancel_pressed const& msg)
{
state=&ATM::done_processing;
}
);
}
void waiting_for_card()
{
interface_hardware.send(display_enter_card());
incoming.wait()
.handle<card_inserted>(
[&](card_inserted const& msg)
{
account=msg.account;
pin="";
interface_hardware.send(display_enter_pin());
state=&ATM::getting_pin;
}
);
}
void done_processing()
{
interface_hardware.send(eject_card());
state=&ATM::waiting_for_card;
}
ATM(ATM const&)=delete;
ATM& operator=(ATM const&)=delete;
public:
ATM(messaging::sender bank_,
messaging::sender interface_hardware_):
bank(bank_),interface_hardware(interface_hardware_)
{}
void done()
{
get_sender().send(close_queue());
}
void run()
{
state=&ATM::waiting_for_card;
try
{
for(;;)
{
(this->*state)();
}
}
catch(close_queue const&)
{
}
}
messaging::sender get_sender()
{
return incoming;
}
};
}