| 9 | import java.util.Map; |
| 10 | |
| 11 | public class Finance { |
| 12 | private final Faker faker; |
| 13 | |
| 14 | protected Finance(Faker faker) { |
| 15 | this.faker = faker; |
| 16 | } |
| 17 | |
| 18 | |
| 19 | private static final Map<String, String> countryCodeToBasicBankAccountNumberPattern = |
| 20 | createCountryCodeToBasicBankAccountNumberPatternMap(); |
| 21 | |
| 22 | public String creditCard(CreditCardType creditCardType) { |
| 23 | final String key = String.format("finance.credit_card.%s", creditCardType.toString().toLowerCase()); |
| 24 | String value = faker.fakeValuesService().resolve(key, this, faker); |
| 25 | final String template = faker.numerify(value); |
| 26 | |
| 27 | String[] split = template.replaceAll("[^0-9]", "").split(""); |
| 28 | List<Integer> reversedAsInt = new ArrayList<Integer>(); |
| 29 | for (int i = 0; i < split.length; i++) { |
| 30 | final String current = split[split.length - 1 - i]; |
| 31 | if (!current.isEmpty()) { |
| 32 | reversedAsInt.add(Integer.valueOf(current)); |
| 33 | } |
| 34 | } |
| 35 | int luhnSum = 0; |
| 36 | int multiplier = 1; |
| 37 | for (Integer digit : reversedAsInt) { |
| 38 | multiplier = (multiplier == 2 ? 1 : 2); |
| 39 | luhnSum += sum(String.valueOf(digit * multiplier).split("")); |
| 40 | } |
| 41 | int luhnDigit = (10 - (luhnSum % 10)) % 10; |
| 42 | return template.replace('\\', ' ').replace('/', ' ').trim().replace('L', String.valueOf(luhnDigit).charAt(0)); |
| 43 | } |
| 44 | |
| 45 | public String creditCard() { |
| 46 | CreditCardType type = randomCreditCardType(); |
| 47 | return creditCard(type); |
| 48 | } |
| 49 | |
| 50 | /** |
| 51 | * Generates a random Business Identifier Code |
| 52 | */ |
| 53 | public String bic() { |
| 54 | return faker.regexify("([A-Z]){4}([A-Z]){2}([0-9A-Z]){2}([0-9A-Z]{3})?"); |
| 55 | } |
| 56 | |
| 57 | public String iban() { |
| 58 | List<String> countryCodes = new ArrayList<String>(countryCodeToBasicBankAccountNumberPattern.keySet()); |
| 59 | String randomCountryCode = countryCodes.get(faker.random().nextInt(countryCodes.size())); |
| 60 | return iban(randomCountryCode); |
| 61 | } |
| 62 | |
| 63 | public String iban(String countryCode) { |
| 64 | String basicBankAccountNumber = faker.regexify(countryCodeToBasicBankAccountNumberPattern.get(countryCode)); |
| 65 | String checkSum = calculateIbanChecksum(countryCode, basicBankAccountNumber); |
| 66 | return countryCode + checkSum + basicBankAccountNumber; |
| 67 | } |
| 68 |
nothing calls this directly
no test coverage detected