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.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 }