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.Strategy00AlikeHelper;
23 import net.sf.plausj.bank.german.util.MathHelper;
24
25 /**
26 * @author ninan
27 *
28 */
29 public class Strategy57 implements Strategy {
30
31 private static final int[] multiplicators1 = new int[] { 1, 2, 1, 2, 1, 2,
32 1, 2, 1 };
33
34 private static final int[] multiplicators2 = new int[] { 1, 2, 0, 1, 2, 1,
35 2, 1, 2, 1 };
36
37 /**
38 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.BankCode,
39 * net.sf.plausj.bank.german.AccountCode)
40 */
41 public CheckDigit calculateCheckDigit(final BankCode bankCode,
42 final AccountCode accountCode) {
43
44 int firstTwo = accountCode.getDigitAtPos(0) * 10
45 + accountCode.getDigitAtPos(1);
46
47 CheckDigit cd = null;
48
49 if (accountCode.getDigitAtPos(0) == 0
50 && accountCode.getDigitAtPos(1) == 0) {
51
52 cd = new CheckDigit(CheckDigit.ALWAYS_INVALID, 0);
53 } else if (firstTwo == 51 || firstTwo == 55 || firstTwo == 61
54 || firstTwo == 64 || firstTwo == 65 || firstTwo == 66
55 || firstTwo == 70 || firstTwo == 73
56 || (firstTwo >= 75 && firstTwo <= 82) || firstTwo == 88
57 || firstTwo == 94 || firstTwo == 95) {
58
59 cd = calculationVariant1(accountCode);
60 } else if ((firstTwo >= 32 && firstTwo <= 39)
61 || (firstTwo >= 41 && firstTwo <= 49) || firstTwo == 52
62 || firstTwo == 53 || firstTwo == 54
63 || (firstTwo >= 56 && firstTwo <= 60) || firstTwo == 62
64 || firstTwo == 63 || firstTwo == 67 || firstTwo == 68
65 || firstTwo == 69 || firstTwo == 71 || firstTwo == 72
66 || firstTwo == 74 || (firstTwo >= 83 && firstTwo <= 87)
67 || firstTwo == 89 || firstTwo == 90 || firstTwo == 92
68 || firstTwo == 93 || firstTwo == 96 || firstTwo == 97
69 || firstTwo == 98) {
70
71 cd = calculationVariant2(accountCode);
72 } else if (firstTwo == 40 || firstTwo == 50 || firstTwo == 91
73 || firstTwo == 99) {
74
75 cd = calculationVariant3(accountCode);
76 } else if (firstTwo >= 01 && firstTwo <= 31) {
77
78 cd = calculationVariant4(accountCode);
79 }
80
81 return cd;
82
83 }
84
85 private CheckDigit calculationVariant1(AccountCode accountCode) {
86
87
88 int firstSix = accountCode.getDigitAtPos(0) * 100000
89 + accountCode.getDigitAtPos(1) * 10000
90 + accountCode.getDigitAtPos(2) * 1000
91 + accountCode.getDigitAtPos(3) * 100
92 + accountCode.getDigitAtPos(4) * 10
93 + accountCode.getDigitAtPos(5);
94
95 if (777777 == firstSix || 888888 == firstSix) {
96 return CheckDigit.NO_CHECK;
97 }
98
99 int sum = Strategy00AlikeHelper.calculateSum(accountCode,
100 multiplicators1);
101 int cd = Strategy00AlikeHelper.getCheckDigitFromSum(sum);
102
103 return new CheckDigit(9, cd);
104
105 }
106
107 private CheckDigit calculationVariant2(AccountCode accountCode) {
108
109 int sum = 0;
110 for (int i = AccountCode.ACCOUNT_CODE_MAX_LENGTH - 1; i >= 0; --i) {
111 int p = accountCode.getDigitAtPos(i) * multiplicators2[i];
112 while (p > 9) {
113 p = MathHelper.calcCrossTotal(p);
114 }
115 sum += p;
116 }
117 int cd = Strategy00AlikeHelper.getCheckDigitFromSum(sum);
118
119 return new CheckDigit(2, cd);
120
121 }
122
123 private CheckDigit calculationVariant3(AccountCode accountCode) {
124
125 return CheckDigit.NO_CHECK;
126
127 }
128
129 private CheckDigit calculationVariant4(AccountCode accountCode) {
130
131 if ("0185125434".equals(accountCode.getAccountCode())) {
132 return new CheckDigit(CheckDigit.ALWAYS_VALID, 0);
133 }
134
135 int thirdAndForth = 10 * accountCode.getDigitAtPos(2)
136 + accountCode.getDigitAtPos(3);
137
138 int seventhToNinth = 100 * accountCode.getDigitAtPos(6) + 10
139 * accountCode.getDigitAtPos(7) + accountCode.getDigitAtPos(8);
140
141 if ((thirdAndForth >= 1 && thirdAndForth <= 31) && seventhToNinth < 500) {
142 return new CheckDigit(CheckDigit.ALWAYS_VALID, 0);
143 } else {
144 return new CheckDigit(CheckDigit.ALWAYS_INVALID, 0);
145 }
146
147 }
148 }