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 Strategy75 implements Strategy {
30  
31  	private static final int[] multiplicators = new int[] { 0, 0, 0, 0, 2, 1,
32  			2, 1, 2, 0 };
33  
34  	private static final int[] multiplicatorsNine = new int[] { 0, 2, 1, 2, 1,
35  			2, 0, 0, 0, 0 };
36  
37  	private static final int[] multiplicatorsNineFirstNine = new int[] { 0, 0,
38  			2, 1, 2, 1, 2, 0, 0, 0 };
39  
40  	private static final int[] multiplicatorsTen = new int[] { 2, 1, 2, 1, 2,
41  			1, 0, 0, 0, 0 };
42  
43  	/**
44  	 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.BankCode,
45  	 *      net.sf.plausj.bank.german.AccountCode)
46  	 */
47  	public CheckDigit calculateCheckDigit(final BankCode bankCode,
48  			final AccountCode accountCode) {
49  
50  		int sum = 0;
51  		int pos = 0;
52  
53  		if (9 > accountCode.getRealLength()) {
54  			sum = calculateSum(accountCode, multiplicators);
55  			pos = 9;
56  		} else if (9 == accountCode.getRealLength()) {
57  			if (9 == accountCode.getDigitAtPos(1)) {
58  				sum = calculateSum(accountCode, multiplicatorsNineFirstNine);
59  				pos = 7;
60  			} else {
61  				sum = calculateSum(accountCode, multiplicatorsNine);
62  				pos = 6;
63  			}
64  		} else {
65  			sum = calculateSum(accountCode, multiplicatorsTen);
66  			pos = 6;
67  		}
68  
69  		int cd = Strategy00AlikeHelper.getCheckDigitFromSum(sum);
70  
71  		return new CheckDigit(pos, cd);
72  
73  	}
74  
75  	private int calculateSum(AccountCode ac, int[] multiplicators) {
76  
77  		int sum = 0;
78  
79  		for (int i = 0; i < AccountCode.ACCOUNT_CODE_MAX_LENGTH; ++i) {
80  			int p = ac.getDigitAtPos(i) * multiplicators[i];
81  			while (p > 9) {
82  				p = MathHelper.calcCrossTotal(p);
83  			}
84  			sum += p;
85  		}
86  
87  		return sum;
88  
89  	}
90  
91  }