tags:

views:

43

answers:

1

Requirements:

Return total count of files (records) for date. Assign a modifier (single character A-Z 0-9) to represent the count.

Info: This modifier along with the date / time will be used to trace the file through out the system.

Current Solution:

[Test]
    public void FileIDModifierShouldReturn9()
    {
        var filecountfordate = 35;
        var chararray = new[]{
                               'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'
                               , 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
                               '9'
                           };

        var expected = "9";
        var actual = chararray[filecountfordate].ToString();

        Assert.AreEqual(expected, actual);
    }

Problem: When the file count is greater than the index of the array an exception is thrown obviously.

2nd Test:

[Test]
    public void WhenFileIDModifierIsBiggerThanArrayArrayShouldStartOverAndReturnValue()
    {
        var filecountfordate = 71;
        var chararray = new[]{
                               'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R'
                               , 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
                               '9'
                           };
        if (filecountfordate > 35)
        {
            filecountfordate = filecountfordate%36;
        }

        var expected = "9";
        var actual = chararray[filecountfordate].ToString();

        Assert.AreEqual(expected, actual);
    }

My problem is I need to be able to restart at the beginning of the array pragmatically, with out having endless "if" statements. Its late and my brain is dead and therefore just can't come up with a reasonable solution.

Any help is appreciated.

+1  A: 

without any loss you can always use modulo of file count over 36

[Test]
public void WhenFileIDModifierIsBiggerThanArrayArrayShouldStartOverAndReturnVal()
{
    var filecountfordate = 71;
    var chararray = new[]{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
      'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 
      'Y', 'Z', '0', '1', '2', '3', '4', '5', '6', '7', '8',
                           '9'
                       };

    filecountfordate = filecountfordate % chararray.Length;


    var expected = "9";
    var actual = chararray[filecountfordate].ToString();

    Assert.AreEqual(expected, actual);
}
TheVillageIdiot
Or, perhaps, filecountfordate % chararray.Length
Jim Mischel
So I had the right solution all along, just didn't need the if statement...Note to self: Dont code when tired :PThanks Village:)
David Yancey
for sure @Jim I've thought of it but just copied and ..... anyway changed it.
TheVillageIdiot
The power of TDD / Pair programming (through SO). Thanks guys.
David Yancey