|
1 |
| -using System; |
2 |
| -using System.Linq; |
3 |
| -using System.Text.RegularExpressions; |
4 |
| - |
5 |
| -namespace ModulusChecking.Models |
6 |
| -{ |
7 |
| - internal class AccountNumber : IEquatable<AccountNumber> |
8 |
| - { |
9 |
| - private readonly int[] _accountNumber; |
10 |
| - |
11 |
| - public static AccountNumber Parse(string accountNumber) |
12 |
| - { |
13 |
| - if (!Regex.IsMatch(accountNumber, @"^[0-9]{6,8}$")) |
| 1 | +using System; |
| 2 | +using System.Linq; |
| 3 | +using System.Text.RegularExpressions; |
| 4 | + |
| 5 | +namespace ModulusChecking.Models |
| 6 | +{ |
| 7 | + internal class AccountNumber : IEquatable<AccountNumber> |
| 8 | + { |
| 9 | + private static readonly Regex _accountNumberRegex = new Regex("^[0-9]{6,8}$", RegexOptions.Compiled); |
| 10 | + |
| 11 | + private readonly int[] _accountNumber; |
| 12 | + |
| 13 | + public static AccountNumber Parse(string accountNumber) |
| 14 | + { |
| 15 | + if (!_accountNumberRegex.IsMatch(accountNumber)) |
14 | 16 | {
|
15 | 17 | throw new ArgumentException(string.Format("The provided account number {0} must be a string of between six and eight digits", accountNumber));
|
16 |
| - } |
17 |
| - |
| 18 | + } |
| 19 | + |
18 | 20 | accountNumber = accountNumber.PadLeft(8, '0');
|
19 | 21 |
|
20 | 22 | var parsedAccountNumber = new int[8];
|
21 | 23 | for (var index = 0; index < accountNumber.Count(); index++)
|
22 | 24 | {
|
23 | 25 | var character = accountNumber[index];
|
24 | 26 | parsedAccountNumber[index] = int.Parse(character.ToString());
|
25 |
| - } |
26 |
| - |
27 |
| - return new AccountNumber(parsedAccountNumber); |
28 |
| - } |
29 |
| - |
30 |
| - private AccountNumber(int[] accountNumber) |
31 |
| - { |
32 |
| - _accountNumber = accountNumber; |
33 |
| - } |
34 |
| - |
35 |
| - public int GetExceptionFourCheckValue |
36 |
| - { |
37 |
| - get |
38 |
| - { |
| 27 | + } |
| 28 | + |
| 29 | + return new AccountNumber(parsedAccountNumber); |
| 30 | + } |
| 31 | + |
| 32 | + private AccountNumber(int[] accountNumber) |
| 33 | + { |
| 34 | + _accountNumber = accountNumber; |
| 35 | + } |
| 36 | + |
| 37 | + public int GetExceptionFourCheckValue |
| 38 | + { |
| 39 | + get |
| 40 | + { |
39 | 41 | var checkString = string.Format("{0}{1}", _accountNumber[6], _accountNumber[7]);
|
40 |
| - return int.Parse(checkString); |
41 |
| - } |
42 |
| - } |
43 |
| - |
44 |
| - /// <summary> |
45 |
| - /// Exception Six Indicates that these sorting codes may contain foreign currency accounts which cannot be checked. |
46 |
| - /// Perform the first and second checks, except: |
47 |
| - /// • If a = 4, 5, 6, 7 or 8, and g and h are the same, the accounts are for a foreign currency and the checks |
48 |
| - /// cannot be used. |
49 |
| - /// This method returns true if this is a foreign currency account and therefore this account cannot be checked. |
50 |
| - /// </summary> |
51 |
| - public bool IsForeignCurrencyAccount |
| 42 | + return int.Parse(checkString); |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + /// <summary> |
| 47 | + /// Exception Six Indicates that these sorting codes may contain foreign currency accounts which cannot be checked. |
| 48 | + /// Perform the first and second checks, except: |
| 49 | + /// • If a = 4, 5, 6, 7 or 8, and g and h are the same, the accounts are for a foreign currency and the checks |
| 50 | + /// cannot be used. |
| 51 | + /// This method returns true if this is a foreign currency account and therefore this account cannot be checked. |
| 52 | + /// </summary> |
| 53 | + public bool IsForeignCurrencyAccount |
| 54 | + { |
| 55 | + get { return _accountNumber[0] >= 4 && _accountNumber[0] <= 8 && _accountNumber[6] == _accountNumber[7]; } |
| 56 | + } |
| 57 | + |
| 58 | + public bool ExceptionTenShouldZeroiseWeights |
52 | 59 | {
|
53 |
| - get { return _accountNumber[0] >= 4 && _accountNumber[0] <= 8 && _accountNumber[6] == _accountNumber[7]; } |
54 |
| - } |
55 |
| - |
56 |
| - public bool ExceptionTenShouldZeroiseWeights |
57 |
| - { |
58 |
| - get |
59 |
| - { |
60 |
| - return |
61 |
| - (MatchFirstTwoCharacters(0, 9) || MatchFirstTwoCharacters(9, 9)) |
| 60 | + get |
| 61 | + { |
| 62 | + return |
| 63 | + (MatchFirstTwoCharacters(0, 9) || MatchFirstTwoCharacters(9, 9)) |
62 | 64 | &&
|
63 |
| - _accountNumber[6] == 9; |
64 |
| - } |
65 |
| - } |
66 |
| - |
67 |
| - private bool MatchFirstTwoCharacters(int first, int second) |
| 65 | + _accountNumber[6] == 9; |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + private bool MatchFirstTwoCharacters(int first, int second) |
68 | 70 | {
|
69 |
| - return _accountNumber[0] == first && _accountNumber[1] == second; |
70 |
| - } |
71 |
| - |
72 |
| - public bool IsValidCouttsNumber |
| 71 | + return _accountNumber[0] == first && _accountNumber[1] == second; |
| 72 | + } |
| 73 | + |
| 74 | + public bool IsValidCouttsNumber |
73 | 75 | {
|
74 |
| - get { return _accountNumber[7] == 0 || _accountNumber[7] == 1 || _accountNumber[7] == 9; } |
75 |
| - } |
76 |
| - |
| 76 | + get { return _accountNumber[7] == 0 || _accountNumber[7] == 1 || _accountNumber[7] == 9; } |
| 77 | + } |
| 78 | + |
77 | 79 | /// <summary>
|
78 | 80 | /// For second exception 14 check a new account number is created by removing the final digit and prepending a 0
|
79 |
| - /// </summary> |
80 |
| - public AccountNumber SlideFinalDigitOff() |
81 |
| - { |
82 |
| - var newNumber = new int[8]; |
83 |
| - newNumber[0] = 0; |
84 |
| - Array.Copy(_accountNumber, 0, newNumber, 1, 7); |
85 |
| - return new AccountNumber(newNumber); |
86 |
| - } |
87 |
| - |
88 |
| - public int IntegerAt(int i) |
89 |
| - { |
90 |
| - return _accountNumber[i]; |
91 |
| - } |
92 |
| - |
93 |
| - public override string ToString() |
94 |
| - { |
95 |
| - return string.Join("", _accountNumber.Select(el => el.ToString()).ToArray()); |
96 |
| - } |
97 |
| - |
| 81 | + /// </summary> |
| 82 | + public AccountNumber SlideFinalDigitOff() |
| 83 | + { |
| 84 | + var newNumber = new int[8]; |
| 85 | + newNumber[0] = 0; |
| 86 | + Array.Copy(_accountNumber, 0, newNumber, 1, 7); |
| 87 | + return new AccountNumber(newNumber); |
| 88 | + } |
| 89 | + |
| 90 | + public int IntegerAt(int i) |
| 91 | + { |
| 92 | + return _accountNumber[i]; |
| 93 | + } |
| 94 | + |
| 95 | + public override string ToString() |
| 96 | + { |
| 97 | + return string.Join("", _accountNumber.Select(el => el.ToString()).ToArray()); |
| 98 | + } |
| 99 | + |
98 | 100 | public bool Equals(AccountNumber other)
|
99 | 101 | {
|
100 | 102 | if (ReferenceEquals(null, other)) return false;
|
101 | 103 | if (ReferenceEquals(this, other)) return true;
|
102 | 104 | return Equals(_accountNumber, other._accountNumber);
|
103 |
| - } |
104 |
| - |
| 105 | + } |
| 106 | + |
105 | 107 | public override bool Equals(object obj)
|
106 | 108 | {
|
107 | 109 | if (ReferenceEquals(null, obj)) return false;
|
108 | 110 | if (ReferenceEquals(this, obj)) return true;
|
109 | 111 | if (obj.GetType() != GetType()) return false;
|
110 | 112 | return Equals((AccountNumber) obj);
|
111 |
| - } |
112 |
| - |
| 113 | + } |
| 114 | + |
113 | 115 | public override int GetHashCode()
|
114 | 116 | {
|
115 | 117 | return (_accountNumber != null ? _accountNumber.GetHashCode() : 0);
|
116 |
| - } |
117 |
| - |
| 118 | + } |
| 119 | + |
118 | 120 | public static bool operator ==(AccountNumber left, AccountNumber right)
|
119 | 121 | {
|
120 | 122 | return Equals(left, right);
|
121 |
| - } |
122 |
| - |
| 123 | + } |
| 124 | + |
123 | 125 | public static bool operator !=(AccountNumber left, AccountNumber right)
|
124 | 126 | {
|
125 | 127 | return !Equals(left, right);
|
126 |
| - } |
127 |
| - } |
128 |
| -} |
| 128 | + } |
| 129 | + } |
| 130 | +} |
0 commit comments