MoneyAllocator

8 views
Skip to first unread message

Berryl Hesh

unread,
Jan 9, 2012, 4:59:16 PM1/9/12
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

dgon

unread,
Jan 10, 2012, 2:37:08 AM1/10/12
to NMoneys
Hi,
Some time ago I pondered implementing such feature myself, but, at the
time it did not look that useful to me.
Now it is the time to certainly revisit it.

Thanks a lot for your contribution.
Any chance that we can discuss the changes you performed in case they
are of use to someone else?

kind regards

Berryl Hesh

unread,
Jan 12, 2012, 2:57:16 PM1/12/12
to NMoneys
Yes, be happy to as time permits.

Did you get a chance to go through the code I sent you yet? Please let
me know if you are going to incorporate that into the code base or
not.

Regards,
~ B

dgon

unread,
Jan 30, 2012, 3:27:51 AM1/30/12
to NMoneys
Sorry for my late response,
Yes I went though it and I would very much like to include the code in
the next release.
But I'd like to make some changes to it as ask you to review them, if
you do not mind.
I will let you know about mid-week

Thanks
Reply all
Reply to author
Forward
0 new messages