1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package net.sf.plausj.bank.german.strategy;
18
19 import net.sf.plausj.bank.german.AccountCode;
20 import net.sf.plausj.bank.german.BankCode;
21 import net.sf.plausj.bank.german.CheckDigit;
22 import net.sf.plausj.bank.german.strategy.util.Strategy52AlikeHelper;
23
24 /**
25 * @author ninan
26 *
27 */
28 public class Strategy52 implements Strategy {
29
30 private static final int[] multiplicators = new int[] { 4, 2, 1, 6, 3, 7,
31 9, 10, 5, 8, 4, 2 };
32
33 /**
34 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.BankCode,
35 * net.sf.plausj.bank.german.AccountCode)
36 */
37 public CheckDigit calculateCheckDigit(final BankCode bankCode,
38 final AccountCode accountCode) {
39
40 if (9 == accountCode.getDigitAtPos(0)) {
41 return new Strategy20().calculateCheckDigit(bankCode, accountCode);
42 }
43
44 int offset = calculateOffset(bankCode, accountCode);
45 int cdWeight = multiplicators[5 + offset];
46 int[] legacyAccountNumber = buildEserLegacyAccountCode(bankCode,
47 accountCode, offset);
48
49 int mod = Strategy52AlikeHelper.calculateSum(legacyAccountNumber,
50 multiplicators);
51
52 return Strategy52AlikeHelper.getCheckSumFromMod(mod, cdWeight, 1);
53 }
54
55 /**
56 * Calculates an ESER legacy account code
57 *
58 * @param bankCode
59 * The bank code used
60 * @param accountCode
61 * The account code used
62 * @param offset
63 * the offset to use for insertation
64 * @return An integer array with the digits of the eser legacy account code
65 * as array
66 */
67 private int[] buildEserLegacyAccountCode(final BankCode bankCode,
68 final AccountCode accountCode, final int offset) {
69
70 int[] elac = new int[12];
71
72 for (int i = 0; i < 4; ++i) {
73 elac[i + offset] = bankCode.getDigitAtPos(i + 4);
74 }
75
76 elac[4 + offset] = accountCode.getDigitAtPos(2);
77
78 for (int i = offset; i < 6; ++i) {
79 elac[i + 6] = accountCode.getDigitAtPos(i + 4);
80 }
81
82 return elac;
83 }
84
85 /**
86 * Calculates the offset for the variable length part of the account code
87 *
88 * @param bankCode
89 * The bank code to use
90 * @param accountCode
91 * The account code to use
92 * @return The offset to use for insertation into ESER legacy account code
93 */
94 private int calculateOffset(final BankCode bankCode,
95 final AccountCode accountCode) {
96 int offset = 0;
97 for (int i = 0; i < 6; ++i) {
98 if (0 != accountCode.getDigitAtPos(i + 4)) {
99 offset = i;
100 break;
101 }
102 }
103 return offset;
104 }
105
106 }