Skip to content

Commit d42bfd7

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

File tree

4 files changed

+19
-1
lines changed

4 files changed

+19
-1
lines changed

atm-machine/atm/atm.go

Lines changed: 13 additions & 0 deletions
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 {
@@ -91,6 +98,8 @@ func (a *ATM) Execute(operation func() error) {
9198
}
9299
}
93100

101+
// flow of ATM
102+
// insercart state -> insertpin state -> selectaccount state -> withdrawAmount state
94103
func NewATM() *ATM {
95104
atm := &ATM{
96105
countOfNotes: map[string]int{
@@ -109,6 +118,10 @@ func NewATM() *ATM {
109118
atm: atm,
110119
}
111120

121+
atm.insertPin = &InsertPin{
122+
atm: atm,
123+
}
124+
112125
atm.selectAccount = &SelectAccount{
113126
atm: atm,
114127
}

atm-machine/atm/insert_pin.go

Lines changed: 1 addition & 0 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,10 @@ func main() {
2929

3030
atm.Execute(atm.GetCardDetail)
3131

32+
fmt.Printf("\nATM state:%s\n", atm.StateName())
33+
34+
atm.Execute(atm.InsertPin)
35+
3236
fmt.Printf("\nATM state:%s\n", atm.StateName())
3337
// insert card
3438
atm.Execute(atm.SelectAccount)

0 commit comments

Comments
 (0)