Skip to content

Commit

Permalink
Merge pull request #10 from masihtehrani/9
Browse files Browse the repository at this point in the history
bank : fix several problem in CardInfo
  • Loading branch information
nimahkh authored Apr 20, 2021
2 parents 67a46ce + fe49809 commit c9b19ba
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 276 deletions.
15 changes: 7 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -59,16 +59,15 @@ Negative := digits.DigitToWord("-156789") // منفی صد پنجاه و شش ه

###### CardInfo Method

This method has two key : `bankName` and `isValid`
If the bank account number is not valid, bankName will be empty
Method has one entry card number and return bank name and error.

```go
card:= Card{Number:"6219861034529008"}
falseResult := card.CardInfo() // { false} -> bankName: "", isValid: false
card= Card{Number:"6037701689095443"}
bankKeshavarzi := card.CardInfo() // {بانک کشاورزی true} -> bankName: "بانک کشاورزی", isValid: true
card= Card{Number:"6219861034529007"}
bankSaman := card.CardInfo() // {بانک سامان true} -> bankName: "بانک سامان", isValid: true
bank,error := CardInfo("") // "", error
bank,error := CardInfo("345345") // "", error
bank,error := CardInfo("6219861034529008") // "", error
bank,error := CardInfo("6037701689095443") // keshavarzi, nil
bank,error := CardInfo("6219861034529007") // saman, nil

```

###### Check Iranian Sheba
Expand Down
106 changes: 67 additions & 39 deletions bank/bank.go
Original file line number Diff line number Diff line change
@@ -1,55 +1,83 @@
package bank

import (
"errors"
"regexp"
"strconv"
)

type response struct {
bankName string
isValid bool
var bankCode = map[string]string{
"636214": "ayandeh",
"627412": "eghtesad-novin",
"627381": "ansar",
"505785": "iran-zamin",
"622106": "parsian",
"627884": "parsian",
"639194": "parsian",
"502229": "pasargad",
"502249": "pasargad",
"639347": "pasargad",
"585983": "tejarat",
"627353": "tejarat",
"502908": "tosee-taavon",
"207177": "export-development",
"627648": "export-development",
"636949": "hekmat-iranian",
"505809": "middle-east",
"585947": "middle-east",
"502938": "dey",
"589463": "refah",
"621986": "saman",
"589210": "sepah",
"604932": "sepah",
"639607": "sarmayeh",
"639346": "sina",
"502806": "shahr",
"504706": "shahr",
"603769": "saderat",
"903769": "saderat",
"627961": "industry-and-mine",
"504172": "resalat",
"606373": "mehr-iran",
"639599": "ghavamin",
"502910": "karafarin",
"627488": "karafarin",
"603770": "keshavarzi",
"639217": "keshavarzi",
"505416": "tourism",
"636795": "central-bank",
"628023": "maskan",
"610433": "mellat",
"991975": "mellat",
"170019": "melli",
"603799": "melli",
"639370": "mehr-eqtesad",
"627760": "post-bank",
"628157": "tosseh",
"606256": "melal",
"505801": "kosar",
"507677": "noor",
}

type Card struct {
Number string
}

func (c *Card) CardInfo() response {
verify := c.verifyCardNum()
bankName := c.getBankName()
return response{
bankName: bankName,
isValid: verify,
// CardInfo find card's bank name.
func CardInfo(card string) (string, error) {
matched, err := regexp.MatchString("^\\d{16}$", card)
if !matched || err != nil || !validateCard(card) {
return "", errors.New("data not valid")
}
}

func (c *Card) getBankName() string {
digits := c.Number
length := len(digits)
if !c.verifyCardNum() {
return ""
bank, ok := bankCode[card[0:6]]
if !ok {
return "", errors.New("bank not found")
}
if length == 16 {
findBank := bankCode{}
code := digits[0:6]
for _, v := range getBankCodes() {
if v.code == code {
findBank = v
}
}

if findBank.name != "" {
return findBank.name
}
}
return ""
return bank, nil
}

func (c *Card) verifyCardNum() bool {
digitsResult := c.Number

length := len(digitsResult)
base1, _ := strconv.Atoi(digitsResult[1:10])
base2, _ := strconv.Atoi(digitsResult[10:16])
func validateCard(card string) bool {
length := len(card)
base1, _ := strconv.Atoi(card[1:10])
base2, _ := strconv.Atoi(card[10:16])
if length < 16 || base1 == 0 || base2 == 0 {
return false
}
Expand All @@ -62,7 +90,7 @@ func (c *Card) verifyCardNum() bool {
if i%2 == 0 {
even = 2
}
base, _ := strconv.ParseInt(digitsResult[i:i+1], 10, 0)
base, _ := strconv.ParseInt(card[i:i+1], 10, 0)
subDigit = int(base) * even
if subDigit > 9 {
sum += subDigit - 9
Expand Down
192 changes: 0 additions & 192 deletions bank/bank_code.go

This file was deleted.

Loading

0 comments on commit c9b19ba

Please sign in to comment.