File tree Expand file tree Collapse file tree 4 files changed +19
-1
lines changed Expand file tree Collapse file tree 4 files changed +19
-1
lines changed Original file line number Diff line number Diff line change @@ -18,6 +18,7 @@ type ATM struct {
1818 account Account
1919 uiOption []string
2020 WithdrawAs * WithdrawNote
21+ insertPin ATMState
2122 insertCard ATMState
2223 readCard ATMState
2324 selectAccount ATMState
@@ -62,6 +63,12 @@ func (a *ATM) GetCardDetail() error {
6263 return a .currentState .GetCardDetail ()
6364}
6465
66+ func (a * ATM ) InsertPin () error {
67+ a .mu .Lock ()
68+ defer a .mu .Unlock ()
69+ return a .currentState .InsertPin ()
70+ }
71+
6572func (a * ATM ) DispenserAmount () error {
6673 a .mu .Lock ()
6774 if err := a .currentState .DispenserAmount (); err != nil {
@@ -109,6 +116,10 @@ func NewATM() *ATM {
109116 atm : atm ,
110117 }
111118
119+ atm .insertPin = & InsertPin {
120+ atm : atm ,
121+ }
122+
112123 atm .selectAccount = & SelectAccount {
113124 atm : atm ,
114125 }
Original file line number Diff line number Diff line change @@ -19,6 +19,7 @@ func (s *InsertPin) InsertPin() error {
1919 fmt .Scanf ("%s" , & pin )
2020
2121 if pin == "1111" {
22+ s .atm .SetState (s .atm .selectAccount )
2223 return nil
2324 } else {
2425 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 {
1111 d .atm .SetState (d .atm .insertCard )
1212 }
1313 d .atm .card = card
14- d .atm .SetState (d .atm .selectAccount )
14+ d .atm .SetState (d .atm .insertPin )
1515 return nil
1616}
1717
Original file line number Diff line number Diff line change 55 "fmt"
66)
77
8+ // flow of ATM
9+ // insertCard state -> readCard state -> insertPin state -> selectAccount state -> withdrawAmount state
810func main () {
911 atm := atm .NewATM ()
1012
@@ -29,6 +31,10 @@ func main() {
2931
3032 atm .Execute (atm .GetCardDetail )
3133
34+ fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
35+
36+ atm .Execute (atm .InsertPin )
37+
3238 fmt .Printf ("\n ATM state:%s\n " , atm .StateName ())
3339 // insert card
3440 atm .Execute (atm .SelectAccount )
You can’t perform that action at this time.
0 commit comments