public MoneyBag(Money[] bag) { for (int i= 0; i < bag.Length; i++) { if (!bag[i].IsZero) AppendMoney(bag[i]); } }
protected void SetUp() { f12CHF= new Money(12, "CHF"); f14CHF= new Money(14, "CHF"); f7USD= new Money( 7, "USD"); f21USD= new Money(21, "USD"); fMB1= new MoneyBag(f12CHF, f7USD); fMB2= new MoneyBag(f14CHF, f21USD); }
private void AppendMoney(Money aMoney) { IMoney old= FindMoney(aMoney.Currency); if (old == null) { fMonies.Add(aMoney); return; } fMonies.Remove(old); IMoney sum= old.Add(aMoney); if (sum.IsZero) return; fMonies.Add(sum); }
public void SimpleNegate() { // [14 CHF] negate == [-14 CHF] Money expected= new Money(-14, "CHF"); Assert.AreEqual(expected, f14CHF.Negate()); }
public void SimpleMultiply() { // [14 CHF] *2 == [28 CHF] Money expected= new Money(28, "CHF"); Assert.AreEqual(expected, f14CHF.Multiply(2)); }
public void SimpleAdd() { // [12 CHF] + [14 CHF] == [26 CHF] Money expected= new Money(26, "CHF"); Assert.AreEqual(expected, f12CHF.Add(f14CHF)); }
public void Normalize4() { // [12 CHF] - {[12 CHF][3 USD]} == [-3 USD] Money[] s1 = { new Money(12, "CHF"), new Money(3, "USD") }; MoneyBag ms1= new MoneyBag(s1); Money expected= new Money(-3, "USD"); Assert.AreEqual(expected, f12CHF.Subtract(ms1)); }
public void Normalize3() { // {[12 CHF][7 USD]} - {[12 CHF][3 USD]} == [4 USD] Money[] s1 = { new Money(12, "CHF"), new Money(3, "USD") }; MoneyBag ms1= new MoneyBag(s1); Money expected= new Money(4, "USD"); Assert.AreEqual(expected, fMB1.Subtract(ms1)); }
public void MoneyHash() { Assert.IsFalse(f12CHF.Equals(null)); Money equal= new Money(12, "CHF"); Assert.AreEqual(f12CHF.GetHashCode(), equal.GetHashCode()); }
public void MoneyEquals() { //NOTE: Normally we use Assert.AreEqual to test whether two // objects are equal. But here we are testing the MoneyBag.Equals() // method itself, so using AreEqual would not serve the purpose. Assert.IsFalse(f12CHF.Equals(null)); Money equalMoney= new Money(12, "CHF"); Assert.IsTrue(f12CHF.Equals( f12CHF )); Assert.IsTrue(f12CHF.Equals( equalMoney )); Assert.IsFalse(f12CHF.Equals(f14CHF)); }
public MoneyBag(Money m, MoneyBag bag) { AppendMoney(m); AppendBag(bag); }
public MoneyBag(Money m1, Money m2) { AppendMoney(m1); AppendMoney(m2); }
private bool Contains(Money aMoney) { Money m= FindMoney(aMoney.Currency); return m.Amount == aMoney.Amount; }
public IMoney AddMoney(Money m) { return (new MoneyBag(m, this)).Simplify(); }
public void SimpleSubtract() { // [14 CHF] - [12 CHF] == [2 CHF] Money expected= new Money(2, "CHF"); Assert.AreEqual(expected, f14CHF.Subtract(f12CHF)); }
public IMoney AddMoney(Money m) { if (m.Currency.Equals(Currency) ) return new Money(Amount+m.Amount, Currency); return new MoneyBag(this, m); }
public void Normalize2() { // {[12 CHF][7 USD]} - [12 CHF] == [7 USD] Money expected= new Money(7, "USD"); Assert.AreEqual(expected, fMB1.Subtract(f12CHF)); }