A simple Money.
Inheritance: IMoney
示例#1
0
文件: MoneyBag.cs 项目: jhare/MDNA
 public MoneyBag(Money[] bag)
 {
     for (int i= 0; i < bag.Length; i++) {
     if (!bag[i].IsZero)
       AppendMoney(bag[i]);
       }
 }
示例#2
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);
		}
示例#3
0
 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);
 }
示例#4
0
		public void SimpleNegate() 
		{
			// [14 CHF] negate == [-14 CHF]
			Money expected= new Money(-14, "CHF");
			Assert.AreEqual(expected, f14CHF.Negate());
		}
示例#5
0
		public void SimpleMultiply() 
		{
			// [14 CHF] *2 == [28 CHF]
			Money expected= new Money(28, "CHF");
			Assert.AreEqual(expected, f14CHF.Multiply(2));
		}
示例#6
0
		public void SimpleAdd() 
		{
			// [12 CHF] + [14 CHF] == [26 CHF]
			Money expected= new Money(26, "CHF");
			Assert.AreEqual(expected, f12CHF.Add(f14CHF));
		}
示例#7
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));
		}
示例#8
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));
		}
示例#9
0
		public void MoneyHash() 
		{
			Assert.IsFalse(f12CHF.Equals(null)); 
			Money equal= new Money(12, "CHF");
			Assert.AreEqual(f12CHF.GetHashCode(), equal.GetHashCode());
		}
示例#10
0
		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));
		}
示例#11
0
 public MoneyBag(Money m, MoneyBag bag)
 {
     AppendMoney(m);
     AppendBag(bag);
 }
示例#12
0
 public MoneyBag(Money m1, Money m2)
 {
     AppendMoney(m1);
     AppendMoney(m2);
 }
示例#13
0
 private bool Contains(Money aMoney)
 {
     Money m= FindMoney(aMoney.Currency);
     return m.Amount == aMoney.Amount;
 }
示例#14
0
 public IMoney AddMoney(Money m)
 {
     return (new MoneyBag(m, this)).Simplify();
 }
示例#15
0
		public void SimpleSubtract() 
		{
			// [14 CHF] - [12 CHF] == [2 CHF]
			Money expected= new Money(2, "CHF");
			Assert.AreEqual(expected, f14CHF.Subtract(f12CHF));
		}
示例#16
0
 public IMoney AddMoney(Money m)
 {
     if (m.Currency.Equals(Currency) )
         return new Money(Amount+m.Amount, Currency);
     return new MoneyBag(this, m);
 }
示例#17
0
		public void Normalize2() 
		{
			// {[12 CHF][7 USD]} - [12 CHF] == [7 USD]
			Money expected= new Money(7, "USD");
			Assert.AreEqual(expected, fMB1.Subtract(f12CHF));
		}