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.Strategy06AlikeHelper;
23  
24  /**
25   * @author ninan
26   * 
27   */
28  public class Strategy51 implements Strategy {
29  
30  	private static final int[] multiplicatorsA = new int[] { 0, 0, 0, 7, 6, 5,
31  			4, 3, 2 };
32  
33  	private static final int[] multiplicatorsBC = new int[] { 0, 0, 0, 0, 6, 5,
34  			4, 3, 2 };
35  
36  	private static final int[] multiplicatorsException1 = new int[] { 0, 0, 8,
37  			7, 6, 5, 4, 3, 2 };
38  
39  	private static final int[] multiplicatorsException2 = new int[] { 10, 9, 8,
40  			7, 6, 5, 4, 3, 2 };
41  
42  	/**
43  	 * @see net.sf.plausj.bank.german.strategy.Strategy#calculateCheckDigit(net.sf.plausj.bank.german.BankCode,
44  	 *      net.sf.plausj.bank.german.AccountCode)
45  	 */
46  	public CheckDigit calculateCheckDigit(final BankCode bankCode,
47  			final AccountCode accountCode) {
48  
49  		CheckDigit cd = null;
50  
51  		if (9 == accountCode.getDigitAtPos(2)) {
52  			cd = calculateCheckDigitExceptionMethod1(accountCode);
53  			if (!accountCode.matchesCheckDigit(cd)) {
54  				cd = calculateCheckDigitExceptionMethod2(accountCode);
55  			}
56  		} else {
57  			cd = calculateCheckDigitMethodA(accountCode);
58  			if (!accountCode.matchesCheckDigit(cd)) {
59  				cd = calculateCheckDigitMethodB(accountCode);
60  				if (!accountCode.matchesCheckDigit(cd)) {
61  					cd = calculateCheckDigitMethodC(accountCode);
62  				}
63  			}
64  		}
65  
66  		return cd;
67  
68  	}
69  
70  	private CheckDigit calculateCheckDigitMethodA(final AccountCode accountCode) {
71  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
72  				multiplicatorsA);
73  		int cd = Strategy06AlikeHelper.getCheckDigitFromSum(sum);
74  		return new CheckDigit(9, cd);
75  	}
76  
77  	private CheckDigit calculateCheckDigitMethodB(final AccountCode accountCode) {
78  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
79  				multiplicatorsBC);
80  		int cd = Strategy06AlikeHelper.getCheckDigitFromSum(sum);
81  		return new CheckDigit(9, cd);
82  	}
83  
84  	private CheckDigit calculateCheckDigitMethodC(final AccountCode accountCode) {
85  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
86  				multiplicatorsBC);
87  		int mod = sum % 7;
88  		int cd = 0;
89  		if (mod != 0) {
90  			cd = 7 - mod;
91  		}
92  
93  		return new CheckDigit(9, cd);
94  	}
95  
96  	private CheckDigit calculateCheckDigitExceptionMethod1(
97  			final AccountCode accountCode) {
98  		return calculateCheckDigitExceptionMethodTemplate(accountCode,
99  				multiplicatorsException1);
100 	}
101 
102 	private CheckDigit calculateCheckDigitExceptionMethod2(
103 			final AccountCode accountCode) {
104 		return calculateCheckDigitExceptionMethodTemplate(accountCode,
105 				multiplicatorsException2);
106 	}
107 
108 	private CheckDigit calculateCheckDigitExceptionMethodTemplate(
109 			final AccountCode accountCode, final int[] multiplicators) {
110 
111 		int sum = 0;
112 
113 		for (int i = AccountCode.ACCOUNT_CODE_MAX_LENGTH - 2; i >= 0; --i) {
114 			sum += accountCode.getDigitAtPos(i) * multiplicators[i];
115 		}
116 
117 		int mod = sum % 11;
118 
119 		int cd = 0;
120 
121 		if (mod > 1) {
122 			cd = 11 - mod;
123 		}
124 
125 		return new CheckDigit(9, cd);
126 
127 	}
128 
129 }