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 Strategy83 implements Strategy {
29  
30  	private static final int[] multiplicatorsPersonalAccountMethodA = new int[] {
31  			0, 0, 0, 7, 6, 5, 4, 3, 2, 0 };
32  
33  	private static final int[] multiplicatorsPersonalAccountMethodB = new int[] {
34  			0, 0, 0, 0, 6, 5, 4, 3, 2, 0 };
35  
36  	private static final int[] multiplicatorsPersonalAccountMethodC = new int[] {
37  			0, 0, 0, 0, 6, 5, 4, 3, 2, 0 };
38  
39  	private static final int[] multiplicatorsImpersonalAccountMethodA = new int[] {
40  			0, 0, 8, 7, 6, 5, 4, 3, 2, 0 };
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  		if (9 == accountCode.getDigitAtPos(2)
50  				&& 9 == accountCode.getDigitAtPos(3)) {
51  			// Impersonal account
52  			return calculateImpersonalAccountMethodA(accountCode);
53  		} else {
54  			// Personal account
55  			CheckDigit cd = null;
56  			cd = calculatePersonalAccountMethodA(accountCode);
57  			if (!accountCode.matchesCheckDigit(cd)) {
58  				cd = calculatePersonalAccountMethodB(accountCode);
59  				if (!accountCode.matchesCheckDigit(cd)) {
60  					cd = calculatePersonalAccountMethodC(accountCode);
61  				}
62  			}
63  			return cd;
64  		}
65  
66  	}
67  
68  	private CheckDigit calculatePersonalAccountMethodA(
69  			final AccountCode accountCode) {
70  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
71  				multiplicatorsPersonalAccountMethodA);
72  		int cd = Strategy06AlikeHelper.getCheckDigitFromSum(sum);
73  		return new CheckDigit(9, cd);
74  	}
75  
76  	private CheckDigit calculatePersonalAccountMethodB(
77  			final AccountCode accountCode) {
78  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
79  				multiplicatorsPersonalAccountMethodB);
80  		int cd = Strategy06AlikeHelper.getCheckDigitFromSum(sum);
81  		return new CheckDigit(9, cd);
82  	}
83  
84  	private CheckDigit calculatePersonalAccountMethodC(
85  			final AccountCode accountCode) {
86  		int sum = Strategy06AlikeHelper.calculateSum(accountCode,
87  				multiplicatorsPersonalAccountMethodC);
88  		int mod = sum % 7;
89  		int cd = 7 - mod;
90  		return new CheckDigit(9, cd);
91  	}
92  
93  	private CheckDigit calculateImpersonalAccountMethodA(
94  			final AccountCode accountCode) {
95  		int sum = 0;
96  		for (int i = AccountCode.ACCOUNT_CODE_MAX_LENGTH - 1; i >= 0; --i) {
97  			sum += accountCode.getDigitAtPos(i)
98  					* multiplicatorsImpersonalAccountMethodA[i];
99  		}
100 		int mod = sum % 11;
101 		int cd = 11 - mod;
102 		if (10 == cd) {
103 			cd = 0;
104 		}
105 		return new CheckDigit(9, cd);
106 	}
107 
108 }