forked from larajz/lab19
-
Notifications
You must be signed in to change notification settings - Fork 0
/
lab19.ml
87 lines (71 loc) · 2.63 KB
/
lab19.ml
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
(*
CS51 Lab 19
Synthesis -- The ATM Emulator
*)
(* Make use of the ATM component behaviors *)
module ATM = ATMcomponents ;;
open Printf ;;
(* Exceptions for: *)
(* ... moving on to the next customer *)
exception ATMNext ;;
(* ... exiting the ATM machine emulation *)
exception ATMFinished ;;
(* atm initial -- Emulate an ATM communicating over `stdin` and
`stdout` with customers until finished. The provided `initial` is
an initial set of accounts to use. *)
let atm (initial : ATM.account_spec list) : unit =
(* prime the set of accounts, assigning each an id and recording
name and balance *)
ATM.initialize initial;
try
(* process customers until finished *)
while true do
(* prompt for the customer's id and provide welcome *)
let id = ATM.acquire_id () in
ATM.present_message ("Welcome " ^ (ATM.get_name id));
try
(* perform actions on behalf of the selected customer *)
while true do
let act = ATM.acquire_act () in
match act with
(* balance inquiry *)
| Balance ->
ATM.present_message
(sprintf "Current balance: %d" (ATM.get_balance id))
(* withdrawal *)
| Withdraw amount ->
let bal = ATM.get_balance id in
if amount > bal then
ATM.present_message (sprintf "Insufficient funds: %d" bal)
else
(ATM.update_balance id (bal - amount);
ATM.deliver_cash amount;
ATM.present_message
(sprintf "New balance: %d" (ATM.get_balance id)))
(* deposit *)
| Deposit amount ->
let bal = ATM.get_balance id in
ATM.update_balance id (bal + amount);
ATM.present_message
(sprintf "New balance: %d" (ATM.get_balance id))
(* done with this customer; move on to the next *)
| Next ->
ATM.present_message "So long.\n";
raise ATMNext
(* exit the ATM emulation *)
| Finished ->
ATM.present_message "Exiting the ATM emulation";
raise ATMFinished
done
with ATMNext -> ()
done
with ATMFinished -> () ;;
(* Some initial accounts *)
let init : ATM.account_spec list =
[ {id = 314159; name = "Emily"; balance = 100};
{id = 271828; name = "Hannah"; balance = 400};
{id = 141421; name = "Jacob"; balance = 0};
{id = 161803; name = "Michael"; balance = 220}
] ;;
(* Fire up the ATM *)
let _ = atm init ;;