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 {
@@ -91,6 +98,8 @@ func (a *ATM) Execute(operation func() error) {
91
98
}
92
99
}
93
100
101
+ // flow of ATM
102
+ // insercart state -> insertpin state -> selectaccount state -> withdrawAmount state
94
103
func NewATM () * ATM {
95
104
atm := & ATM {
96
105
countOfNotes : map [string ]int {
@@ -109,6 +118,10 @@ func NewATM() *ATM {
109
118
atm : atm ,
110
119
}
111
120
121
+ atm .insertPin = & InsertPin {
122
+ atm : atm ,
123
+ }
124
+
112
125
atm .selectAccount = & SelectAccount {
113
126
atm : atm ,
114
127
}
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 @@ -29,6 +29,10 @@ func main() {
29
29
30
30
atm .Execute (atm .GetCardDetail )
31
31
32
+ fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
33
+
34
+ atm .Execute (atm .InsertPin )
35
+
32
36
fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
33
37
// insert card
34
38
atm .Execute (atm .SelectAccount )
You can’t perform that action at this time.
0 commit comments