|
| 1 | +package atm |
| 2 | + |
| 3 | +import ( |
| 4 | + "atm-machine/model" |
| 5 | + "fmt" |
| 6 | + "sync" |
| 7 | +) |
| 8 | + |
| 9 | +type WithdrawNote struct { |
| 10 | + FiveHundred int |
| 11 | + Hundred int |
| 12 | + Left int |
| 13 | +} |
| 14 | + |
| 15 | +type ATM struct { |
| 16 | + countOfNotes map[string]int |
| 17 | + card model.Card |
| 18 | + account Account |
| 19 | + uiOption []string |
| 20 | + WithdrawAs *WithdrawNote |
| 21 | + insertPin ATMState |
| 22 | + insertCard ATMState |
| 23 | + readCard ATMState |
| 24 | + selectAccount ATMState |
| 25 | + dispenserAmount ATMState |
| 26 | + currentState ATMState |
| 27 | + mu sync.RWMutex |
| 28 | +} |
| 29 | + |
| 30 | +func (a *ATM) SetState(s ATMState) { |
| 31 | + a.currentState = s |
| 32 | +} |
| 33 | + |
| 34 | +func (a *ATM) CleanPreviousAtmTransaction() { |
| 35 | + a.mu.Lock() |
| 36 | + defer a.mu.Unlock() |
| 37 | + a.WithdrawAs = &WithdrawNote{} |
| 38 | + a.card = model.Card{} |
| 39 | + a.account = nil |
| 40 | +} |
| 41 | + |
| 42 | +func (a *ATM) PrintMoney() { |
| 43 | + a.mu.Lock() |
| 44 | + defer a.mu.Unlock() |
| 45 | + fmt.Printf("\n500 note present:%d, 100 not present:%d", a.countOfNotes["500"], a.countOfNotes["100"]) |
| 46 | +} |
| 47 | + |
| 48 | +func (a *ATM) StateName() string { |
| 49 | + a.mu.Lock() |
| 50 | + defer a.mu.Unlock() |
| 51 | + return a.currentState.StateName() |
| 52 | +} |
| 53 | + |
| 54 | +func (a *ATM) InsertCard() error { |
| 55 | + a.mu.Lock() |
| 56 | + defer a.mu.Unlock() |
| 57 | + return a.currentState.InsertCard() |
| 58 | +} |
| 59 | + |
| 60 | +func (a *ATM) GetCardDetail() error { |
| 61 | + a.mu.Lock() |
| 62 | + defer a.mu.Unlock() |
| 63 | + return a.currentState.GetCardDetail() |
| 64 | +} |
| 65 | + |
| 66 | +func (a *ATM) InsertPin() error { |
| 67 | + a.mu.Lock() |
| 68 | + defer a.mu.Unlock() |
| 69 | + return a.currentState.InsertPin() |
| 70 | +} |
| 71 | + |
| 72 | +func (a *ATM) DispenserAmount() error { |
| 73 | + a.mu.Lock() |
| 74 | + if err := a.currentState.DispenserAmount(); err != nil { |
| 75 | + return err |
| 76 | + } |
| 77 | + |
| 78 | + a.mu.Unlock() |
| 79 | + |
| 80 | + a.CleanPreviousAtmTransaction() |
| 81 | + |
| 82 | + return nil |
| 83 | +} |
| 84 | + |
| 85 | +func (a *ATM) SelectAccount() error { |
| 86 | + a.mu.Lock() |
| 87 | + defer a.mu.Unlock() |
| 88 | + return a.currentState.SelectAccount() |
| 89 | +} |
| 90 | + |
| 91 | +func (a *ATM) Execute(operation func() error) { |
| 92 | + err := operation() |
| 93 | + if err != nil { |
| 94 | + a.CleanPreviousAtmTransaction() |
| 95 | + a.SetState(a.insertCard) |
| 96 | + fmt.Println("All operation will be nil operation:") |
| 97 | + fmt.Println("Error while operation:", err.Error()) |
| 98 | + } |
| 99 | +} |
| 100 | + |
| 101 | +func NewATM() *ATM { |
| 102 | + atm := &ATM{ |
| 103 | + countOfNotes: map[string]int{ |
| 104 | + "500": 1000, |
| 105 | + "200": 2000, |
| 106 | + "100": 1000, |
| 107 | + }, |
| 108 | + WithdrawAs: &WithdrawNote{}, |
| 109 | + } |
| 110 | + |
| 111 | + atm.insertCard = &InsertCard{ |
| 112 | + atm: atm, |
| 113 | + } |
| 114 | + |
| 115 | + atm.readCard = &ReadCard{ |
| 116 | + atm: atm, |
| 117 | + } |
| 118 | + |
| 119 | + atm.insertPin = &InsertPin{ |
| 120 | + atm: atm, |
| 121 | + } |
| 122 | + |
| 123 | + atm.selectAccount = &SelectAccount{ |
| 124 | + atm: atm, |
| 125 | + } |
| 126 | + |
| 127 | + atm.dispenserAmount = &DispenserAmount{ |
| 128 | + atm: atm, |
| 129 | + } |
| 130 | + |
| 131 | + atm.SetState(atm.insertCard) |
| 132 | + |
| 133 | + return atm |
| 134 | +} |
0 commit comments