Skip to content
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

Generating US routing number (ABA RTN) in Finance provider. #821

Merged
merged 1 commit into from May 6, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/main/java/net/datafaker/providers/base/Finance.java
@@ -1,7 +1,11 @@
package net.datafaker.providers.base;

import java.math.BigInteger;
import java.util.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.regex.Pattern;

/**
Expand Down Expand Up @@ -92,6 +96,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
15 changes: 15 additions & 0 deletions src/test/java/net/datafaker/providers/base/FinanceTest.java
Expand Up @@ -82,4 +82,19 @@ void discoverCard() {
String creditCard = finance.creditCard(CreditCardType.DISCOVER).replace("-", "");
assertThat(creditCard).startsWith("6").hasSize(16);
}

@RepeatedTest(100)
void usRoutingNumber() {
String rtn = finance.usRoutingNumber();
System.out.println(rtn);
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);
}
}