View Javadoc

1   /*
2    * Copyright 2006 Christian Kalkhoff <me@ninan.info>
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
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  			// Always invalid
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  			// Variant 1
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  			// Variant 2
71  			cd = calculationVariant2(accountCode);
72  		} else if (firstTwo == 40 || firstTwo == 50 || firstTwo == 91
73  				|| firstTwo == 99) {
74  			// Variant 3
75  			cd = calculationVariant3(accountCode);
76  		} else if (firstTwo >= 01 && firstTwo <= 31) {
77  			// Variant 4
78  			cd = calculationVariant4(accountCode);
79  		}
80  
81  		return cd;
82  
83  	}
84  
85  	private CheckDigit calculationVariant1(AccountCode accountCode) {
86  
87  		// TODO: Refactor to "for"-case
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 }