/usr/share/doc/nunit/examples/csharp/money/MoneyBag.cs is in libnunit-doc 2.5.10.11092+dfsg-3.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 | // ****************************************************************
// This is free software licensed under the NUnit license. You
// may obtain a copy of the license as well as information regarding
// copyright ownership at http://nunit.org/?p=license&r=2.4.
// ****************************************************************
namespace NUnit.Samples.Money
{
using System;
using System.Collections;
using System.Text;
/// <summary>A MoneyBag defers exchange rate conversions.</summary>
/// <remarks>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.</remarks>
class MoneyBag: IMoney
{
private ArrayList fMonies= new ArrayList(5);
private MoneyBag()
{
}
public MoneyBag(Money[] bag)
{
for (int i= 0; i < bag.Length; i++)
{
if (!bag[i].IsZero)
AppendMoney(bag[i]);
}
}
public MoneyBag(Money m1, Money m2)
{
AppendMoney(m1);
AppendMoney(m2);
}
public MoneyBag(Money m, MoneyBag bag)
{
AppendMoney(m);
AppendBag(bag);
}
public MoneyBag(MoneyBag m1, MoneyBag m2)
{
AppendBag(m1);
AppendBag(m2);
}
public IMoney Add(IMoney m)
{
return m.AddMoneyBag(this);
}
public IMoney AddMoney(Money m)
{
return (new MoneyBag(m, this)).Simplify();
}
public IMoney AddMoneyBag(MoneyBag s)
{
return (new MoneyBag(s, this)).Simplify();
}
private void AppendBag(MoneyBag aBag)
{
foreach (Money m in aBag.fMonies)
AppendMoney(m);
}
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);
}
private bool Contains(Money aMoney)
{
Money m= FindMoney(aMoney.Currency);
return m.Amount == aMoney.Amount;
}
public override bool Equals(Object anObject)
{
if (IsZero)
if (anObject is IMoney)
return ((IMoney)anObject).IsZero;
if (anObject is MoneyBag)
{
MoneyBag aMoneyBag= (MoneyBag)anObject;
if (aMoneyBag.fMonies.Count != fMonies.Count)
return false;
foreach (Money m in fMonies)
{
if (!aMoneyBag.Contains(m))
return false;
}
return true;
}
return false;
}
private Money FindMoney(String currency)
{
foreach (Money m in fMonies)
{
if (m.Currency.Equals(currency))
return m;
}
return null;
}
public override int GetHashCode()
{
int hash= 0;
foreach (Money m in fMonies)
{
hash^= m.GetHashCode();
}
return hash;
}
public bool IsZero
{
get { return fMonies.Count == 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;
}
public IMoney Negate()
{
MoneyBag result= new MoneyBag();
foreach (Money m in fMonies)
{
result.AppendMoney((Money)m.Negate());
}
return result;
}
private IMoney Simplify()
{
if (fMonies.Count == 1)
return (IMoney)fMonies[0];
return this;
}
public IMoney Subtract(IMoney m)
{
return Add(m.Negate());
}
public override String ToString()
{
StringBuilder buffer = new StringBuilder();
buffer.Append("{");
foreach (Money m in fMonies)
buffer.Append(m);
buffer.Append("}");
return buffer.ToString();
}
}
}
|