Skip to content

Instantly share code, notes, and snippets.

@FilipDeVos
Forked from svenschelfaut/MoneyTest
Last active August 29, 2015 13:57
Show Gist options
  • Save FilipDeVos/9686677 to your computer and use it in GitHub Desktop.
Save FilipDeVos/9686677 to your computer and use it in GitHub Desktop.
using System;
namespace MoneyTest
{
public static class Program
{
static void Main(string[] args)
{
var twentyEuro = new Money(20.0, Currency.EUR);
var twentyDollar = new Money(20.0, Currency.USD);
var handler = new ConversionHandler(new ExchangeRateService());
var command = new ConversionCommand { Money = twentyDollar, ToCurrency = Currency.EUR };
var twentyDollarInEuro = handler.Handle(command);
Console.WriteLine("20 USD in euro: " + twentyDollarInEuro);
var sum = twentyEuro + twentyDollarInEuro;
Console.WriteLine("Result 20 EUR + 20 USD: " + sum);
Console.ReadLine();
}
}
public enum Currency
{
USD,
EUR
}
public class ConversionCommand
{
public Money Money { get; set; }
public Currency ToCurrency { get; set; }
}
public sealed class ConversionHandler
{
private readonly IDetermineExchangeRate _exchangeRateService;
public ConversionHandler(IDetermineExchangeRate exchangeRateService)
{
_exchangeRateService = exchangeRateService;
}
public Money Handle(ConversionCommand command)
{
var exchangeRate = _exchangeRateService.DetermineExchangeRate(command.Money.Currency, command.ToCurrency);
return new Money(exchangeRate * command.Money.Amount, command.ToCurrency);
}
}
public interface IDetermineExchangeRate
{
double DetermineExchangeRate(Currency fromCurrency, Currency toCurrency);
}
public class ExchangeRateService : IDetermineExchangeRate
{
public double DetermineExchangeRate(Currency fromCurrency, Currency toCurrency)
{
if (fromCurrency == toCurrency)
return 1;
if (fromCurrency == Currency.EUR && toCurrency == Currency.USD)
return 1.37;
if (fromCurrency == Currency.USD && toCurrency == Currency.EUR)
return 0.72;
throw new ArgumentException(string.Format("No exchange rate available for {0} to {1}", fromCurrency, toCurrency));
}
}
public sealed class Money
{
public double Amount { get; private set; }
public Currency Currency { get; private set; }
public Money(double amount, Currency currency)
{
Amount = amount;
Currency = currency;
}
public static Money operator +(Money money1, Money money2)
{
CheckThatCurrenciesAreTheSame(money1, money2);
return new Money(money1.Amount + money2.Amount, money1.Currency);
}
public static Money operator -(Money money1, Money money2)
{
CheckThatCurrenciesAreTheSame(money1, money2);
return new Money(money1.Amount - money2.Amount, money1.Currency);
}
private static void CheckThatCurrenciesAreTheSame(Money money1, Money money2)
{
if (money1.Currency != money2.Currency)
{
throw new ArgumentException(
string.Format("Can't add {0} to {1}", money2.Currency, money1.Currency));
}
}
private bool Equals(Money other)
{
return Amount.Equals(other.Amount) && Currency == other.Currency;
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != GetType()) return false;
return Equals((Money)obj);
}
public override int GetHashCode()
{
unchecked
{
return (Amount.GetHashCode() * 397) ^ (int)Currency;
}
}
public override string ToString()
{
return string.Format("{0} {1}", Amount, Currency);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment