Skip to content

Commit

Permalink
Generating US routing number (ABA RTN) in Finance provider (#821). (#823
Browse files Browse the repository at this point in the history
)
  • Loading branch information
polarfish committed May 7, 2023
1 parent d9c2cc2 commit ef8eff4
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
19 changes: 19 additions & 0 deletions src/main/java/net/datafaker/providers/base/Finance.java
Expand Up @@ -77,6 +77,25 @@ public String iban(String countryCode) {
return countryCode + checkSum + basicBankAccountNumber;
}

public String usRoutingNumber() {
String base =
// 01 through 12 are the "normal" routing numbers, and correspond to the 12 Federal Reserve Banks.
String.format("%02d", faker.random().nextInt(12) + 1)
+ faker.regexify("\\d{6}");
int check =
Character.getNumericValue(base.charAt(0)) * 3
+ Character.getNumericValue(base.charAt(1)) * 7
+ Character.getNumericValue(base.charAt(2))
+ Character.getNumericValue(base.charAt(3)) * 3
+ Character.getNumericValue(base.charAt(4)) * 7
+ Character.getNumericValue(base.charAt(5))
+ Character.getNumericValue(base.charAt(6)) * 3
+ Character.getNumericValue(base.charAt(7)) * 7;
check = Math.abs(check % 10 - 10) % 10;

return base + check;
}

private CreditCardType randomCreditCardType() {
return CreditCardType.values()[this.faker.random().nextInt(CreditCardType.values().length)];
}
Expand Down
14 changes: 14 additions & 0 deletions src/test/java/net/datafaker/providers/base/FinanceTest.java
Expand Up @@ -77,4 +77,18 @@ void discoverCard() {
String creditCard = faker.finance().creditCard(CreditCardType.DISCOVER).replace("-", "");
assertThat(creditCard).startsWith("6").hasSize(16);
}

@RepeatedTest(100)
void usRoutingNumber() {
String rtn = faker.finance().usRoutingNumber();
assertThat(rtn).matches("\\d{9}");
int check = 0;
for (int index = 0; index < 3; index++) {
int pos = index * 3;
check += Character.getNumericValue(rtn.charAt(pos)) * 3;
check += Character.getNumericValue(rtn.charAt(pos + 1)) * 7;
check += Character.getNumericValue(rtn.charAt(pos + 2));
}
assertThat(check % 10).isEqualTo(0);
}
}

0 comments on commit ef8eff4

Please sign in to comment.