File tree 4 files changed +19
-1
lines changed
4 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ type ATM struct {
18
18
account Account
19
19
uiOption []string
20
20
WithdrawAs * WithdrawNote
21
+ insertPin ATMState
21
22
insertCard ATMState
22
23
readCard ATMState
23
24
selectAccount ATMState
@@ -62,6 +63,12 @@ func (a *ATM) GetCardDetail() error {
62
63
return a .currentState .GetCardDetail ()
63
64
}
64
65
66
+ func (a * ATM ) InsertPin () error {
67
+ a .mu .Lock ()
68
+ defer a .mu .Unlock ()
69
+ return a .currentState .InsertPin ()
70
+ }
71
+
65
72
func (a * ATM ) DispenserAmount () error {
66
73
a .mu .Lock ()
67
74
if err := a .currentState .DispenserAmount (); err != nil {
@@ -109,6 +116,10 @@ func NewATM() *ATM {
109
116
atm : atm ,
110
117
}
111
118
119
+ atm .insertPin = & InsertPin {
120
+ atm : atm ,
121
+ }
122
+
112
123
atm .selectAccount = & SelectAccount {
113
124
atm : atm ,
114
125
}
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ func (s *InsertPin) InsertPin() error {
19
19
fmt .Scanf ("%s" , & pin )
20
20
21
21
if pin == "1111" {
22
+ s .atm .SetState (s .atm .selectAccount )
22
23
return nil
23
24
} else {
24
25
return errors .New ("Pin is not valid" )
Original file line number Diff line number Diff line change @@ -11,7 +11,7 @@ func (d *ReadCard) GetCardDetail() error {
11
11
d .atm .SetState (d .atm .insertCard )
12
12
}
13
13
d .atm .card = card
14
- d .atm .SetState (d .atm .selectAccount )
14
+ d .atm .SetState (d .atm .insertPin )
15
15
return nil
16
16
}
17
17
Original file line number Diff line number Diff line change 5
5
"fmt"
6
6
)
7
7
8
+ // flow of ATM
9
+ // insertCard state -> readCard state -> insertPin state -> selectAccount state -> withdrawAmount state
8
10
func main () {
9
11
atm := atm .NewATM ()
10
12
@@ -29,6 +31,10 @@ func main() {
29
31
30
32
atm .Execute (atm .GetCardDetail )
31
33
34
+ fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
35
+
36
+ atm .Execute (atm .InsertPin )
37
+
32
38
fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
33
39
// insert card
34
40
atm .Execute (atm .SelectAccount )
You can’t perform that action at this time.
0 commit comments