11package com .devskiller .jfairy .producer .payment ;
22
3- import java .util .IllegalFormatCodePointException ;
43import java .util .List ;
4+ import java .util .Optional ;
55
6- import org . iban4j . CountryCode ;
7- import org . iban4j . Iban ;
8- import org . iban4j . UnsupportedCountryException ;
6+ import de . speedbanking . iban . Iban ;
7+ import de . speedbanking . iban . IbanRegistry ;
8+ import de . speedbanking . iban . RandomIban ;
99import org .jspecify .annotations .Nullable ;
1010
1111import com .devskiller .jfairy .data .DataMaster ;
1212import com .devskiller .jfairy .producer .BaseProducer ;
1313import com .devskiller .jfairy .producer .person .Country ;
1414
15- import static com .devskiller .jfairy .producer .util .StringUtils .isNotEmpty ;
16-
1715/**
1816 * ALPHA: Under development
1917 */
2018public class DefaultIBANProvider implements IBANProvider {
2119
2220 protected final DataMaster dataMaster ;
2321 protected final BaseProducer baseProducer ;
24- protected CountryCode countryCode ;
25- protected String accountNumber ;
26- protected String bankCode ;
27- protected String branchCode ;
28- protected String nationalCheckDigit ;
22+ protected String countryCode ;
2923
3024 public DefaultIBANProvider (BaseProducer baseProducer ,
3125 DataMaster dataMaster ,
@@ -39,72 +33,37 @@ public DefaultIBANProvider(BaseProducer baseProducer,
3933
4034 @ Override
4135 public @ Nullable IBAN get () {
42- try {
43-
44- fillCountryCode ();
45-
46- try {
47-
48- Iban .Builder builder = new Iban .Builder ()
49- .countryCode (countryCode )
50- .bankCode (bankCode )
51- .branchCode (branchCode )
52- .nationalCheckDigit (nationalCheckDigit );
53- if (isNotEmpty (accountNumber )) {
54- builder .accountNumber (accountNumber );
55- }
56- Iban iban = builder .buildRandom ();
57-
58- String identificationNumber = iban .getIdentificationNumber ();
59- String checkDigit = iban .getCheckDigit ();
60- String accountType = iban .getAccountType ();
61- String bban = iban .getBban ();
62- String ownerAccountType = iban .getOwnerAccountType ();
63- String ibanNumber = iban .toString ();
36+ fillCountryCode ();
6437
65- return new IBAN (accountNumber , identificationNumber , branchCode , checkDigit ,
66- accountType , bankCode , bban , countryCode .getName (), nationalCheckDigit ,
67- ownerAccountType , ibanNumber );
68- } catch (UnsupportedCountryException ex ) {
69- return null ;
70- }
71- } catch (IllegalFormatCodePointException ex ) {
72- throw new IllegalArgumentException ("Invalid iban " + ex .getMessage (), ex );
38+ IbanRegistry reg = IbanRegistry .getByCode (countryCode );
39+ if (reg == null ) {
40+ return null ;
7341 }
42+ Iban iban = RandomIban .of (countryCode );
43+
44+ return new IBAN (iban .getAccountNumber (),
45+ iban .getCheckDigits (),
46+ iban .getBankCode (),
47+ iban .getBban (),
48+ iban .getCountryCode (),
49+ iban .getNationalCheckDigit (),
50+ iban .toString ());
7451 }
7552
7653 @ Override
7754 public void fillCountryCode () {
7855 if (countryCode == null ) {
7956 List <Country > countries = Country .findCountryForLanguage (dataMaster .getLanguage ());
8057 Country country = baseProducer .randomElement (countries );
81- countryCode = CountryCode .valueOf (country .getCode ());
82- }
83- }
84-
85- @ Override
86- public void setNationalCheckDigit (String nationalCheckDigit ) {
87- this .nationalCheckDigit = nationalCheckDigit ;
88- }
8958
90- @ Override
91- public void setBranchCode ( String branchCode ) {
92- this . branchCode = branchCode ;
59+ IbanRegistry r = IbanRegistry . getByCode ( country . getCode ());
60+ countryCode = Optional . ofNullable ( r ). map ( IbanRegistry :: name ). orElse ( null );
61+ }
9362 }
9463
9564 @ Override
9665 public void setCountry (String country ) {
97- this .countryCode = CountryCode .valueOf (country );
98- }
99-
100- @ Override
101- public void setAccountNumber (String accountNumber ) {
102- this .accountNumber = accountNumber ;
103- }
104-
105- @ Override
106- public void setBankCode (String bankCode ) {
107- this .bankCode = bankCode ;
66+ this .countryCode = country ;
10867 }
10968
11069}
0 commit comments