Skip to content

Commit 6c55e54

Browse files
committed
added enter pin in atm state floe
1 parent ce348d0 commit 6c55e54

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

atm-machine/atm/atm.go

+11
Original file line numberDiff line numberDiff 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+
6572
func (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
}

atm-machine/atm/insert_pin.go

+1
Original file line numberDiff line numberDiff 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")

atm-machine/atm/read_card.go

+1-1
Original file line numberDiff line numberDiff 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

atm-machine/main.go

+6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import (
55
"fmt"
66
)
77

8+
// flow of ATM
9+
// insertCard state -> readCard state -> insertPin state -> selectAccount state -> withdrawAmount state
810
func main() {
911
atm := atm.NewATM()
1012

@@ -29,6 +31,10 @@ func main() {
2931

3032
atm.Execute(atm.GetCardDetail)
3133

34+
fmt.Printf("\nATM state:%s\n", atm.StateName())
35+
36+
atm.Execute(atm.InsertPin)
37+
3238
fmt.Printf("\nATM state:%s\n", atm.StateName())
3339
// insert card
3440
atm.Execute(atm.SelectAccount)

0 commit comments

Comments
 (0)