A MoneyBag defers exchange rate conversions.
For example adding 12 Swiss Francs to 14 US Dollars is represented as a bag containing the two Monies 12 CHF and 14 USD. Adding another 10 Swiss francs gives a bag with 22 CHF and 14 USD. Due to the deferred exchange rate conversion we can later value a MoneyBag with different exchange rates. A MoneyBag is represented as a list of Monies and provides different constructors to create a MoneyBag.
상속: IMoney
예제 #1
0
		public void BagNegate() 
		{
			// {[12 CHF][7 USD]} negate == {[-12 CHF][-7 USD]}
			Money[] bag= { new Money(-12, "CHF"), new Money(-7, "USD") };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, fMB1.Negate());
		}
예제 #2
0
 public void BagSubtract()
 {
     // {[12 CHF][7 USD]} - {[14 CHF][21 USD] == {[-2 CHF][-14 USD]}
     Money[] bag= { new Money(-2, "CHF"), new Money(-14, "USD") };
     MoneyBag expected= new MoneyBag(bag);
     Assert.AreEqual(expected, fMB1.Subtract(fMB2));
 }
예제 #3
0
		public void BagSimpleAdd() 
		{
			// {[12 CHF][7 USD]} + [14 CHF] == {[26 CHF][7 USD]}
			Money[] bag= { new Money(26, "CHF"), new Money(7, "USD") };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, fMB1.Add(f14CHF));
		}
예제 #4
0
		public void BagMultiply() 
		{
			// {[12 CHF][7 USD]} *2 == {[24 CHF][14 USD]}
			Money[] bag = { new Money(24, "CHF"), new Money(14, "USD") };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, fMB1.Multiply(2));
			Assert.AreEqual(fMB1, fMB1.Multiply(1));
			Assert.IsTrue(fMB1.Multiply(0).IsZero);
		}
예제 #5
0
		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);
		}
예제 #6
0
		public void MoneyBagEquals() 
		{
			//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(fMB1.Equals(null)); 

			Assert.IsTrue(fMB1.Equals( fMB1 ));
			MoneyBag equal= new MoneyBag(new Money(12, "CHF"), new Money(7, "USD"));
			Assert.IsTrue(fMB1.Equals(equal));
			Assert.IsTrue(!fMB1.Equals(f12CHF));
			Assert.IsTrue(!f12CHF.Equals(fMB1));
			Assert.IsTrue(!fMB1.Equals(fMB2));
		}
예제 #7
0
		public void SimpleBagAdd() 
		{
			// [14 CHF] + {[12 CHF][7 USD]} == {[26 CHF][7 USD]}
			Money[] bag= { new Money(26, "CHF"), new Money(7, "USD") };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, f14CHF.Add(fMB1));
		}
예제 #8
0
		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));
		}
예제 #9
0
		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));
		}
예제 #10
0
		public void Normalize() 
		{
			Money[] bag= { new Money(26, "CHF"), new Money(28, "CHF"), new Money(6, "CHF") };
			MoneyBag moneyBag= new MoneyBag(bag);
			Money[] expected = { new Money(60, "CHF") };
			// note: expected is still a MoneyBag
			MoneyBag expectedBag= new MoneyBag(expected);
			Assert.AreEqual(expectedBag, moneyBag);
		}
예제 #11
0
 public IMoney AddMoneyBag(MoneyBag s)
 {
     return (new MoneyBag(s, this)).Simplify();
 }
예제 #12
0
		public void BagSumAdd() 
		{
			// {[12 CHF][7 USD]} + {[14 CHF][21 USD]} == {[26 CHF][28 USD]}
			Money[] bag= { new Money(26, "CHF"), new Money(28, "USD") };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, fMB1.Add(fMB2));
		}
예제 #13
0
		public void MixedSimpleAdd() 
		{
			// [12 CHF] + [7 USD] == {[12 CHF][7 USD]}
			Money[] bag= { f12CHF, f7USD };
			MoneyBag expected= new MoneyBag(bag);
			Assert.AreEqual(expected, f12CHF.Add(f7USD));
		}
예제 #14
0
 public MoneyBag(MoneyBag m1, MoneyBag m2)
 {
     AppendBag(m1);
     AppendBag(m2);
 }
예제 #15
0
 public MoneyBag(Money m, MoneyBag bag)
 {
     AppendMoney(m);
     AppendBag(bag);
 }
예제 #16
0
 private void AppendBag(MoneyBag aBag)
 {
     foreach (Money m in aBag.fMonies)
         AppendMoney(m);
 }
예제 #17
0
 public IMoney Negate()
 {
     MoneyBag result= new MoneyBag();
     foreach (Money m in fMonies)
     {
         result.AppendMoney((Money)m.Negate());
     }
     return result;
 }
예제 #18
0
 public IMoney Multiply(int factor)
 {
     MoneyBag result= new MoneyBag();
     if (factor != 0)
     {
         foreach (Money m in fMonies)
         {
             result.AppendMoney((Money)m.Multiply(factor));
         }
     }
     return result;
 }
예제 #19
0
 public IMoney AddMoneyBag(MoneyBag s)
 {
     return s.AddMoney(this);
 }
예제 #20
0
		public void MoneyBagHash() 
		{
			MoneyBag equal= new MoneyBag(new Money(12, "CHF"), new Money(7, "USD"));
			Assert.AreEqual(fMB1.GetHashCode(), equal.GetHashCode());
		}