Skip to content

Commit 1d6ec85

Browse files
author
Paul D'Ambra
committed
Merge branch 'dezzy30000-master'
2 parents d92426e + 82179fe commit 1d6ec85

File tree

8 files changed

+40146
-99
lines changed

8 files changed

+40146
-99
lines changed

.semver

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
:major: 1
22
:minor: 4
3-
:patch: 0
3+
:patch: 1
44
:special: ""

ModulusChecking/Loaders/ValacdosSource.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,9 +13,10 @@ public class ValacdosSource : IRuleMappingSource
1313
public ValacdosSource()
1414
{
1515
GetModulusWeightMappings = Resources.valacdos
16-
.Split(new[] {"\r\n", "\n"}, StringSplitOptions.None)
16+
.Split(new[] { "\r\n", "\n" }, StringSplitOptions.None)
1717
.Where(row => row.Length > 0)
18-
.Select<string, ModulusWeightMapping>(row => ModulusWeightMapping.From(row));
18+
.Select(row => ModulusWeightMapping.From(row))
19+
.ToArray();
1920
}
2021
}
2122
}
Lines changed: 94 additions & 92 deletions
Original file line numberDiff line numberDiff line change
@@ -1,128 +1,130 @@
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))
1416
{
1517
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+
1820
accountNumber = accountNumber.PadLeft(8, '0');
1921

2022
var parsedAccountNumber = new int[8];
2123
for (var index = 0; index < accountNumber.Count(); index++)
2224
{
2325
var character = accountNumber[index];
2426
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+
{
3941
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
5259
{
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))
6264
&&
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)
6870
{
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
7375
{
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+
7779
/// <summary>
7880
/// 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+
98100
public bool Equals(AccountNumber other)
99101
{
100102
if (ReferenceEquals(null, other)) return false;
101103
if (ReferenceEquals(this, other)) return true;
102104
return Equals(_accountNumber, other._accountNumber);
103-
}
104-
105+
}
106+
105107
public override bool Equals(object obj)
106108
{
107109
if (ReferenceEquals(null, obj)) return false;
108110
if (ReferenceEquals(this, obj)) return true;
109111
if (obj.GetType() != GetType()) return false;
110112
return Equals((AccountNumber) obj);
111-
}
112-
113+
}
114+
113115
public override int GetHashCode()
114116
{
115117
return (_accountNumber != null ? _accountNumber.GetHashCode() : 0);
116-
}
117-
118+
}
119+
118120
public static bool operator ==(AccountNumber left, AccountNumber right)
119121
{
120122
return Equals(left, right);
121-
}
122-
123+
}
124+
123125
public static bool operator !=(AccountNumber left, AccountNumber right)
124126
{
125127
return !Equals(left, right);
126-
}
127-
}
128-
}
128+
}
129+
}
130+
}

ModulusChecking/Models/SortCode.cs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ namespace ModulusChecking.Models
55
{
66
public class SortCode
77
{
8+
private static readonly Regex _sortCodeRegex = new Regex("^[0-9]{6}$", RegexOptions.Compiled);
9+
810
private readonly double _doubleValue;
911
public double DoubleValue { get {return _doubleValue;} }
1012
private readonly string _value;
@@ -16,7 +18,7 @@ public override string ToString()
1618

1719
public SortCode(string s)
1820
{
19-
if (!Regex.IsMatch(s, "^[0-9]{6}$"))
21+
if (!_sortCodeRegex.IsMatch(s))
2022
{
2123
throw new ArgumentException("A Sort Code must be a string consisting of 6 digits. Not " + s);
2224
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
using ModulusChecking;
2+
using NUnit.Framework;
3+
using System.Diagnostics;
4+
using System.IO;
5+
using System.Linq;
6+
7+
namespace PublicInterfaceTests
8+
{
9+
public class PerformanceTests
10+
{
11+
[TestCase(2)]
12+
public void ItCanProcessALargeFileInUnder(int seconds)
13+
{
14+
var stopwatch = new Stopwatch();
15+
var modulusChecker = new ModulusChecker();
16+
17+
using (var sr = new StreamReader("sa.txt"))
18+
{
19+
stopwatch.Start();
20+
21+
while (sr.Peek() >= 0)
22+
{
23+
var segments = sr
24+
.ReadLine()
25+
.Split('\t');
26+
27+
modulusChecker.CheckBankAccount(segments.First(), segments.Last());
28+
}
29+
30+
stopwatch.Stop();
31+
}
32+
33+
Assert.IsTrue(stopwatch.Elapsed.Seconds <= seconds, string.Format("Failed to process a large number of sortcodes and account numbers in under {0} seconds.", seconds));
34+
}
35+
}
36+
}

PublicInterfaceTests/PublicInterfaceTests.csproj

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
</ItemGroup>
4343
<ItemGroup>
4444
<Compile Include="IssueFive.cs" />
45+
<Compile Include="PerformanceTests.cs" />
4546
<Compile Include="VocalinkTestCases.cs" />
4647
<Compile Include="Properties\AssemblyInfo.cs" />
4748
</ItemGroup>
@@ -51,6 +52,11 @@
5152
<Name>ModulusChecking</Name>
5253
</ProjectReference>
5354
</ItemGroup>
55+
<ItemGroup>
56+
<Content Include="sa.txt">
57+
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
58+
</Content>
59+
</ItemGroup>
5460
<ItemGroup>
5561
<None Include="packages.config" />
5662
</ItemGroup>

0 commit comments

Comments
 (0)