-
Notifications
You must be signed in to change notification settings - Fork 14
ATM low level design #7
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 6 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
13a501e
Added meeting scheduler low level design
codeimmortal f290569
Added mutex and Getroom
codeimmortal 5e1af39
Add feedback and readme for meeting scheduler design
codeimmortal bb5e378
Update IsFree method for room
codeimmortal 762360b
Merge branch 'thesaltree:main' into main
codeimmortal 40f2fdc
ATM LLD
codeimmortal ce348d0
added atm LLD feeback for improvement
codeimmortal 6c55e54
added enter pin in atm state floe
codeimmortal File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package atm | ||
|
|
||
| import "errors" | ||
|
|
||
| type Account interface { | ||
| GetBalance() float64 | ||
| } | ||
|
|
||
| type AccountType string | ||
|
|
||
| const ( | ||
| Saving AccountType = "SAVING" | ||
| Current AccountType = "CURRENT" | ||
| ) | ||
|
|
||
| func AccountFactory(accType AccountType) (Account, error) { | ||
| switch accType { | ||
| case Current: | ||
| return &CurrentAccount{}, nil | ||
| case Saving: | ||
| return &SavingAccount{}, nil | ||
| default: | ||
| return nil, errors.New("Not a valid account type") | ||
| } | ||
| } | ||
|
|
||
| type SavingAccount struct { | ||
| acctNo string | ||
| balance float64 | ||
| } | ||
|
|
||
| func (s *SavingAccount) GetBalance() float64 { | ||
| return s.balance | ||
| } | ||
|
|
||
| type CurrentAccount struct { | ||
| acctNo string | ||
| balance float64 | ||
| } | ||
|
|
||
| func (c *CurrentAccount) GetBalance() float64 { | ||
| return c.balance | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,120 @@ | ||
| package atm | ||
|
|
||
| import ( | ||
| "atm-machine/model" | ||
| "fmt" | ||
| "sync" | ||
| ) | ||
|
|
||
| type WithdrawNote struct { | ||
| FiveHundread int | ||
| Hundread int | ||
| Left int | ||
| } | ||
|
|
||
| type ATM struct { | ||
| countOFNotes map[string]int | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| card model.Card | ||
| account Account | ||
| uiOption []string | ||
| WithdrawAs *WithdrawNote | ||
| insertCard ATMState | ||
| readCard ATMState | ||
| selectAccount ATMState | ||
| dispenserAmount ATMState | ||
| currentState ATMState | ||
| mu sync.RWMutex | ||
| } | ||
|
|
||
| func (a *ATM) SetState(s ATMState) { | ||
| a.currentState = s | ||
| } | ||
|
|
||
| func (a *ATM) ResetAtm() { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| a.WithdrawAs = &WithdrawNote{} | ||
| a.card = model.Card{} | ||
| a.account = nil | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| func (a *ATM) PrintMoney() { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| fmt.Printf("\n500 note present:%d, 100 not present:%d", a.countOFNotes["500"], a.countOFNotes["100"]) | ||
| } | ||
|
|
||
| func (a *ATM) StateName() string { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| return a.currentState.StateName() | ||
| } | ||
|
|
||
| func (a *ATM) InsertCard() error { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| return a.currentState.InsertCard() | ||
| } | ||
|
|
||
| func (a *ATM) GetCardDetail() error { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| return a.currentState.GetCardDetail() | ||
| } | ||
|
|
||
| func (a *ATM) DispenserAmount() error { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| return a.currentState.DispenserAmount() | ||
| } | ||
|
|
||
| func (a *ATM) SelectAccount() error { | ||
| a.mu.Lock() | ||
| defer a.mu.Unlock() | ||
| return a.currentState.SelectAccount() | ||
| } | ||
|
|
||
| func (a *ATM) Execute(operation func() error) { | ||
| err := operation() | ||
| if err != nil { | ||
| a.ResetAtm() | ||
| a.SetState(a.insertCard) | ||
| fmt.Println("All operation will be nil operation:") | ||
| fmt.Println("Error while operation:", err.Error()) | ||
| } | ||
| } | ||
|
|
||
| func NewATM() *ATM { | ||
| atm := &ATM{ | ||
| countOFNotes: map[string]int{ | ||
| "500": 1000, | ||
| "200": 2000, | ||
| "100": 1000, | ||
| }, | ||
| WithdrawAs: &WithdrawNote{}, | ||
| } | ||
|
|
||
| atm.insertCard = &InsertCard{ | ||
| atm: atm, | ||
| } | ||
|
|
||
| atm.readCard = &ReadCard{ | ||
| atm: atm, | ||
| } | ||
|
|
||
| atm.selectAccount = &SelectAccount{ | ||
| atm: atm, | ||
| } | ||
|
|
||
| atm.readCard = &ReadCard{ | ||
| atm: atm, | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| atm.dispenserAmount = &DispenserAmount{ | ||
| atm: atm, | ||
| } | ||
|
|
||
| atm.SetState(atm.insertCard) | ||
|
|
||
| return atm | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,42 @@ | ||
| package atm | ||
|
|
||
| type ATMState interface { | ||
| StateName() string | ||
| InsertCard() error | ||
| InsertPin() error | ||
| SelectAccount() error | ||
| GetCardDetail() error | ||
| DispenserAmount() error | ||
| } | ||
|
|
||
| type ATMAbstract struct{} | ||
|
|
||
| func (s *ATMAbstract) InsertCard() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *ATMAbstract) InsertPin() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *ATMAbstract) AuthticateCard() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *ATMAbstract) DispenserAmount() error { | ||
| return nil | ||
| } | ||
|
|
||
| func (s *ATMAbstract) SelectAccount() error { | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func (s *ATMAbstract) GetCardDetail() error { | ||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func (s *ATMAbstract) StateName() string { | ||
| return "ATMAbstract" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package atm | ||
|
|
||
| import "atm-machine/model" | ||
|
|
||
| // This will be actuall implementation of card reader // i am returning a card from here | ||
| type CardReader struct { | ||
| } | ||
|
|
||
| func NewCardReader() CardReader { | ||
| return CardReader{} | ||
| } | ||
|
|
||
| // for now it will return some dummy card depend of input for acting as multiple car | ||
| func (c CardReader) ReadCard(cardType string) model.Card { | ||
| switch cardType { | ||
| case "invalid": | ||
| return model.Card{ | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| Status: model.NoCard, | ||
| } | ||
| default: | ||
| return model.Card{ | ||
| BankName: "hdfc", | ||
| CardNo: "34567-sd55498459", | ||
| AccountNo: "bshy4859-sdhhj66", | ||
| Status: model.Active, | ||
| UserName: "Himanshu sharma"} | ||
|
|
||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| package atm | ||
|
|
||
| type InsertCard struct { | ||
| atm *ATM | ||
| ATMAbstract | ||
| } | ||
|
|
||
| func (d *InsertCard) InsertCard() error { | ||
| d.atm.SetState(d.atm.readCard) | ||
| return nil | ||
| } | ||
|
|
||
| func (d *InsertCard) StateName() string { | ||
| return "InsertCard" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package atm | ||
|
|
||
| import ( | ||
| "errors" | ||
| "fmt" | ||
| ) | ||
|
|
||
| type InsetPin struct { | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| atm *ATM | ||
| ATMAbstract | ||
| } | ||
|
|
||
| func (s *InsetPin) InsertPin() error { | ||
| // logic to valid pin, we can call api client inside it, i will return sucess and error depend on input | ||
| fmt.Println("Enter 4 letter pin for card:\n") | ||
|
|
||
| var pin string | ||
|
|
||
| fmt.Scanf("%s", &pin) | ||
|
|
||
| if pin == "1111" { | ||
| return nil | ||
| } else { | ||
| return errors.New("Pin is not valid") | ||
| } | ||
| } | ||
|
|
||
| func (d *InsetPin) StateName() string { | ||
| return "InsetPin" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package atm | ||
|
|
||
| type ReadCard struct { | ||
| atm *ATM | ||
| ATMAbstract | ||
| } | ||
|
|
||
| func (d *ReadCard) GetCardDetail() error { | ||
| card := NewCardReader().ReadCard("valid") | ||
| if card.CardNo == "" { | ||
| d.atm.SetState(d.atm.insertCard) | ||
| } | ||
| d.atm.card = card | ||
| d.atm.SetState(d.atm.selectAccount) | ||
| return nil | ||
| } | ||
|
|
||
| func (d *ReadCard) StateName() string { | ||
| return "ReadCard" | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,30 @@ | ||
| package atm | ||
|
|
||
| import ( | ||
| "fmt" | ||
| ) | ||
|
|
||
| type SelectAccount struct { | ||
| atm *ATM | ||
| ATMAbstract | ||
| } | ||
|
|
||
| func (s *SelectAccount) SelectAccount() error { | ||
|
|
||
| var accountType string | ||
| var err error | ||
|
|
||
| fmt.Println("Select account type SAVING OR CURRENT:\n") | ||
| fmt.Scanf("%s", &accountType) | ||
|
|
||
| s.atm.account, err = AccountFactory(AccountType(accountType)) | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| s.atm.SetState(s.atm.dispenserAmount) | ||
|
|
||
| return err | ||
|
|
||
| } | ||
|
|
||
| func (s *SelectAccount) StateName() string { | ||
| return "SelectAccount" | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package atm | ||
|
|
||
| type Withdraw interface { | ||
| ProcessAmount(atm *ATM, atmamount float64) | ||
| } | ||
|
|
||
| func NewWithDrawPipeline() Withdraw { | ||
| return fiveHundreadWithdraw{ | ||
| next: hundreadHundreadWithdraw{}, | ||
| } | ||
codeimmortal marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| type fiveHundreadWithdraw struct { | ||
| next Withdraw | ||
| } | ||
|
|
||
| func (f fiveHundreadWithdraw) ProcessAmount(atm *ATM, atmamount float64) { | ||
| div := atmamount / 500 | ||
| rem := int(atmamount) % 500 | ||
| atm.WithdrawAs.FiveHundread = int(div) | ||
| f.next.ProcessAmount(atm, float64(rem)) | ||
| } | ||
|
|
||
| type hundreadHundreadWithdraw struct { | ||
| next Withdraw | ||
| } | ||
|
|
||
| func (h hundreadHundreadWithdraw) ProcessAmount(atm *ATM, atmamount float64) { | ||
| div := atmamount / 100 | ||
| rem := int(atmamount) % 100 | ||
| atm.WithdrawAs.Hundread = int(div) | ||
| atm.WithdrawAs.Left = rem | ||
|
|
||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.