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  
23  /**
24   * @author ninan
25   * 
26   */
27  public class StrategyB9 implements Strategy {
28  
29  	private static final int[] multiplicatorsA = new int[] { 0, 0, 1, 2, 3, 1,
30  			2, 3, 1 };
31  
32  	private static final int[] multiplicatorsB = new int[] { 0, 0, 0, 6, 5, 4,
33  			3, 2, 1 };
34  
35  	/**
36  	 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.BankCode,
37  	 *      net.sf.plausj.bank.german.AccountCode)
38  	 */
39  	public CheckDigit calculateCheckDigit(final BankCode bankCode,
40  			final AccountCode accountCode) {
41  
42  		CheckDigit cd = null;
43  		if (7 == accountCode.getRealLength()) {
44  			cd = calculateCheckDigitMethodB(accountCode);
45  		} else if (8 == accountCode.getRealLength()) {
46  			cd = calculateCheckDigitMethodA(accountCode);
47  		} else {
48  			cd = new CheckDigit(CheckDigit.ALWAYS_INVALID, 0);
49  		}
50  
51  		return cd;
52  
53  	}
54  
55  	private CheckDigit calculateCheckDigitMethodA(final AccountCode accountCode) {
56  
57  		int sum = 0;
58  		for (int i = AccountCode.ACCOUNT_CODE_MAX_LENGTH - 2; i >= 2; --i) {
59  			int p = multiplicatorsA[i] * accountCode.getDigitAtPos(i)
60  					+ multiplicatorsA[i];
61  			int mod = p % 11;
62  			sum += mod;
63  		}
64  
65  		int cd = sum % 10;
66  		CheckDigit cdObj = new CheckDigit(9, cd);
67  		if (!accountCode.matchesCheckDigit(cdObj)) {
68  			cd += 5;
69  			if (cd >= 10) {
70  				cd -= 10;
71  			}
72  			cdObj = new CheckDigit(9, cd);
73  		}
74  
75  		return cdObj;
76  
77  	}
78  
79  	private CheckDigit calculateCheckDigitMethodB(final AccountCode accountCode) {
80  
81  		int sum = 0;
82  		for (int i = AccountCode.ACCOUNT_CODE_MAX_LENGTH - 2; i >= 3; --i) {
83  			sum += accountCode.getDigitAtPos(i) * multiplicatorsB[i];
84  		}
85  		int cd = sum % 11;
86  		CheckDigit cdObj = new CheckDigit(9, cd);
87  		if (!accountCode.matchesCheckDigit(cdObj)) {
88  			cd += 5;
89  			if (cd >= 10) {
90  				cd -= 10;
91  			}
92  			cdObj = new CheckDigit(9, cd);
93  		}
94  
95  		return cdObj;
96  
97  	}
98  }