Skip to content

Commit ce348d0

Browse files
committed
added atm LLD feeback for improvement
1 parent 40f2fdc commit ce348d0

File tree

8 files changed

+49
-42
lines changed

8 files changed

+49
-42
lines changed

atm-machine/atm/Account.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,20 +24,19 @@ func AccountFactory(accType AccountType) (Account, error) {
2424
}
2525
}
2626

27-
type SavingAccount struct {
27+
type BaseAccount struct {
2828
acctNo string
2929
balance float64
3030
}
3131

32-
func (s *SavingAccount) GetBalance() float64 {
33-
return s.balance
32+
func (b *BaseAccount) GetBalance() float64 {
33+
return b.balance
3434
}
3535

36-
type CurrentAccount struct {
37-
acctNo string
38-
balance float64
36+
type SavingAccount struct {
37+
BaseAccount
3938
}
4039

41-
func (c *CurrentAccount) GetBalance() float64 {
42-
return c.balance
40+
type CurrentAccount struct {
41+
BaseAccount
4342
}

atm-machine/atm/atm.go

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,13 @@ import (
77
)
88

99
type WithdrawNote struct {
10-
FiveHundread int
11-
Hundread int
12-
Left int
10+
FiveHundred int
11+
Hundred int
12+
Left int
1313
}
1414

1515
type ATM struct {
16-
countOFNotes map[string]int
16+
countOfNotes map[string]int
1717
card model.Card
1818
account Account
1919
uiOption []string
@@ -30,7 +30,7 @@ func (a *ATM) SetState(s ATMState) {
3030
a.currentState = s
3131
}
3232

33-
func (a *ATM) ResetAtm() {
33+
func (a *ATM) CleanPreviousAtmTransaction() {
3434
a.mu.Lock()
3535
defer a.mu.Unlock()
3636
a.WithdrawAs = &WithdrawNote{}
@@ -41,7 +41,7 @@ func (a *ATM) ResetAtm() {
4141
func (a *ATM) PrintMoney() {
4242
a.mu.Lock()
4343
defer a.mu.Unlock()
44-
fmt.Printf("\n500 note present:%d, 100 not present:%d", a.countOFNotes["500"], a.countOFNotes["100"])
44+
fmt.Printf("\n500 note present:%d, 100 not present:%d", a.countOfNotes["500"], a.countOfNotes["100"])
4545
}
4646

4747
func (a *ATM) StateName() string {
@@ -64,8 +64,15 @@ func (a *ATM) GetCardDetail() error {
6464

6565
func (a *ATM) DispenserAmount() error {
6666
a.mu.Lock()
67-
defer a.mu.Unlock()
68-
return a.currentState.DispenserAmount()
67+
if err := a.currentState.DispenserAmount(); err != nil {
68+
return err
69+
}
70+
71+
a.mu.Unlock()
72+
73+
a.CleanPreviousAtmTransaction()
74+
75+
return nil
6976
}
7077

7178
func (a *ATM) SelectAccount() error {
@@ -77,7 +84,7 @@ func (a *ATM) SelectAccount() error {
7784
func (a *ATM) Execute(operation func() error) {
7885
err := operation()
7986
if err != nil {
80-
a.ResetAtm()
87+
a.CleanPreviousAtmTransaction()
8188
a.SetState(a.insertCard)
8289
fmt.Println("All operation will be nil operation:")
8390
fmt.Println("Error while operation:", err.Error())
@@ -86,7 +93,7 @@ func (a *ATM) Execute(operation func() error) {
8693

8794
func NewATM() *ATM {
8895
atm := &ATM{
89-
countOFNotes: map[string]int{
96+
countOfNotes: map[string]int{
9097
"500": 1000,
9198
"200": 2000,
9299
"100": 1000,
@@ -106,10 +113,6 @@ func NewATM() *ATM {
106113
atm: atm,
107114
}
108115

109-
atm.readCard = &ReadCard{
110-
atm: atm,
111-
}
112-
113116
atm.dispenserAmount = &DispenserAmount{
114117
atm: atm,
115118
}

atm-machine/atm/card_reader.go

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@ package atm
22

33
import "atm-machine/model"
44

5+
const (
6+
InvalidCard = "invalid"
7+
)
8+
59
// This will be actuall implementation of card reader // i am returning a card from here
610
type CardReader struct {
711
}
@@ -13,7 +17,7 @@ func NewCardReader() CardReader {
1317
// for now it will return some dummy card depend of input for acting as multiple car
1418
func (c CardReader) ReadCard(cardType string) model.Card {
1519
switch cardType {
16-
case "invalid":
20+
case InvalidCard:
1721
return model.Card{
1822
Status: model.NoCard,
1923
}

atm-machine/atm/insert_pin.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@ import (
55
"fmt"
66
)
77

8-
type InsetPin struct {
8+
type InsertPin struct {
99
atm *ATM
1010
ATMAbstract
1111
}
1212

13-
func (s *InsetPin) InsertPin() error {
13+
func (s *InsertPin) InsertPin() error {
1414
// logic to valid pin, we can call api client inside it, i will return sucess and error depend on input
1515
fmt.Println("Enter 4 letter pin for card:\n")
1616

@@ -25,6 +25,6 @@ func (s *InsetPin) InsertPin() error {
2525
}
2626
}
2727

28-
func (d *InsetPin) StateName() string {
28+
func (d *InsertPin) StateName() string {
2929
return "InsetPin"
3030
}

atm-machine/atm/select_account.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,13 @@ func (s *SelectAccount) SelectAccount() error {
1717
fmt.Println("Select account type SAVING OR CURRENT:\n")
1818
fmt.Scanf("%s", &accountType)
1919

20-
s.atm.account, err = AccountFactory(AccountType(accountType))
20+
if s.atm.account, err = AccountFactory(AccountType(accountType)); err != nil {
21+
return err
22+
}
2123

2224
s.atm.SetState(s.atm.dispenserAmount)
2325

24-
return err
26+
return nil
2527

2628
}
2729

atm-machine/atm/withdraw.go

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,30 +5,30 @@ type Withdraw interface {
55
}
66

77
func NewWithDrawPipeline() Withdraw {
8-
return fiveHundreadWithdraw{
9-
next: hundreadHundreadWithdraw{},
8+
return fiveHundredWithdraw{
9+
next: oneHundredWithdraw{},
1010
}
1111
}
1212

13-
type fiveHundreadWithdraw struct {
13+
type fiveHundredWithdraw struct {
1414
next Withdraw
1515
}
1616

17-
func (f fiveHundreadWithdraw) ProcessAmount(atm *ATM, atmamount float64) {
17+
func (f fiveHundredWithdraw) ProcessAmount(atm *ATM, atmamount float64) {
1818
div := atmamount / 500
1919
rem := int(atmamount) % 500
20-
atm.WithdrawAs.FiveHundread = int(div)
20+
atm.WithdrawAs.FiveHundred = int(div)
2121
f.next.ProcessAmount(atm, float64(rem))
2222
}
2323

24-
type hundreadHundreadWithdraw struct {
24+
type oneHundredWithdraw struct {
2525
next Withdraw
2626
}
2727

28-
func (h hundreadHundreadWithdraw) ProcessAmount(atm *ATM, atmamount float64) {
28+
func (h oneHundredWithdraw) ProcessAmount(atm *ATM, atmamount float64) {
2929
div := atmamount / 100
3030
rem := int(atmamount) % 100
31-
atm.WithdrawAs.Hundread = int(div)
31+
atm.WithdrawAs.Hundred = int(div)
3232
atm.WithdrawAs.Left = rem
3333

3434
}

atm-machine/atm/withdrawAmount.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,20 +23,19 @@ func (w *DispenserAmount) DispenserAmount() error {
2323
if w.atm.WithdrawAs.Left > 0 {
2424
return errors.New("This amount is not divisible of 100")
2525
} else {
26-
if w.atm.countOFNotes["500"] >= w.atm.WithdrawAs.FiveHundread {
27-
w.atm.countOFNotes["500"] = w.atm.countOFNotes["500"] - w.atm.WithdrawAs.FiveHundread
26+
if w.atm.countOfNotes["500"] >= w.atm.WithdrawAs.FiveHundred {
27+
w.atm.countOfNotes["500"] = w.atm.countOfNotes["500"] - w.atm.WithdrawAs.FiveHundred
2828
} else {
2929
return errors.New("Bank don't have enough fund 500")
3030
}
3131

32-
if w.atm.countOFNotes["100"] >= w.atm.WithdrawAs.Hundread {
33-
w.atm.countOFNotes["100"] = w.atm.countOFNotes["100"] - w.atm.WithdrawAs.Hundread
32+
if w.atm.countOfNotes["100"] >= w.atm.WithdrawAs.Hundred {
33+
w.atm.countOfNotes["100"] = w.atm.countOfNotes["100"] - w.atm.WithdrawAs.Hundred
3434
} else {
3535
return errors.New("Bank don't have enough fund 100")
3636
}
3737
}
3838

39-
w.atm.ResetAtm()
4039
w.atm.SetState(w.atm.insertCard)
4140

4241
return nil

atm-machine/main.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ func main() {
1313
var runProcess string
1414

1515
fmt.Printf("\nATM state:%s\n", atm.StateName())
16-
fmt.Println("Input do you want to proceed, press n to exist")
16+
fmt.Println("Input do you want to proceed, press n to exit")
1717
fmt.Scanf("%s", &runProcess)
1818

1919
fmt.Println(runProcess)

0 commit comments

Comments
 (0)