Berryl Hesh
unread,Jan 9, 2012, 4:59:16 PM1/9/12Sign in to reply to author
Sign in to forward
You do not have permission to delete messages in this group
Either email addresses are anonymous for this group or you need the view member email addresses permission to view the original message
to NMoneys
Hi Daniel et al,
I wrote a MoneyAllocator and some support classes/extensions to make
it easy to use. I'm guessing that you have read Fowler's PoEA, but
between the comments and tests (samples of each below) you should
pick up on it without effort in any case.
I made some other minor changes to your library for my own use, but on
the whole I like your implementation quite a lot!
Cheers,
Berryl
P.S. I sent the patch to the google group and you as an email
[Test]
public void Allocate_BySplitCount_WhenRemainder() {
var m = new Money(1, "USD");
const int splitCount = 3; // three-way split
var firstToLast = m.Allocate(splitCount,
RemainderOrdering.FirstToLast);
Assert.That(firstToLast.Length, Is.EqualTo(3));
Assert.That(firstToLast[0].Amount, Is.EqualTo(0.34m));
Assert.That(firstToLast[1].Amount, Is.EqualTo(0.33m));
Assert.That(firstToLast[2].Amount, Is.EqualTo(0.33m));
Assert.That(firstToLast.Sum(x => x.Amount),
Is.EqualTo(1));
var lastToFirst = m.Allocate(splitCount,
RemainderOrdering.LastToFirst);
Assert.That(lastToFirst,
Is.EqualTo(firstToLast.Reverse()));
var random = m.Allocate(splitCount,
RemainderOrdering.Random);
Assert.That(random.Sum(x => x.Amount), Is.EqualTo(1));
}
/// <summary>
/// Allocates a sum of money fully and 'fairly'.
/// </summary>
/// <remarks>
/// <para>
/// A sum of money that can be allocated to each recipient exactly
evenly is inherently 'fair'. For example, a US
/// Dollar split four (4) ways leaves each recipient with 25
cents.</para>
/// <para>
/// A US Dollar split three (3) ways cannot be distributed evenly
and is therefore inherently 'unfair'. The
/// best we can do is minimize the amount of the remainder (in
this case a penny) and allocate it in a way
/// that seems random and thus fair to the recipients.</para>
/// <seealso cref="RemainderOrdering"/>
/// </remarks>
public class MoneyAllocator