public static Money operator +(Money money1, Money money2) { Money addedMoney = new Money(0, money1.CurrencyUnit); double total = addedMoney.Conversion(money2.MoneyAmount, money2.CurrencyUnit, money1.CurrencyUnit) + money1.MoneyAmount; addedMoney.MoneyAmount = total; return addedMoney; }
static void Main() { try { Money mon = new Money(1000, "Euro"); Money monTwo = new Money(500, "Dollars"); Console.WriteLine(mon.MoneyAmount + " " + mon.CurrencyUnit); mon.Conversion(mon.MoneyAmount, "Euro", "Yen"); Console.WriteLine(mon.MoneyAmount + " " + mon.CurrencyUnit); Money addedMoney = mon + monTwo; Console.WriteLine("Operator overloading result: " + addedMoney.MoneyAmount + " " + addedMoney.CurrencyUnit); Money added = mon.CurrencyAddition(mon, monTwo); Console.WriteLine(added.MoneyAmount + " " + added.CurrencyUnit); } catch(ArgumentException ae) { Console.WriteLine(ae.Message); } Console.ReadLine(); }
public Money CurrencyAddition(Money baseMoney, Money toAdd) { Money converted = new Money(0, baseMoney.CurrencyUnit); double total = converted.Conversion(toAdd.MoneyAmount, toAdd.CurrencyUnit, baseMoney.CurrencyUnit) + baseMoney.MoneyAmount; converted.MoneyAmount = total; return converted; }