Skip to content

Instantly share code, notes, and snippets.

@koistya
Created December 23, 2013 21:56
Show Gist options
  • Select an option

  • Save koistya/8105558 to your computer and use it in GitHub Desktop.

Select an option

Save koistya/8105558 to your computer and use it in GitHub Desktop.
using System;
public class Coin
{
public Coin(CoinType type, int volume, decimal amount)
{
if (volume <= 0)
{
throw new ArgumentOutOfRangeException("volume");
}
if (amount <= 0)
{
throw new ArgumentOutOfRangeException("value");
}
this.Type = type;
this.Volume = volume;
this.Amount = amount;
}
public CoinType Type { get; private set; }
public int Volume { get; private set; }
public decimal Amount { get; private set; }
}
using System;
using System.Linq;
public class CoinJar
{
private readonly int[] acceptedVolumes;
private readonly CoinType[] acceptedTypes;
public CoinJar()
: this(new[] { 25 }, new[] { CoinType.US })
{
}
public CoinJar(int[] acceptedVolumes, CoinType[] acceptedTypes)
{
this.acceptedVolumes = acceptedVolumes;
this.acceptedTypes = acceptedTypes;
}
public decimal TotalAmount { get; private set; }
public void Add(Coin coin)
{
if (!this.acceptedTypes.Contains(coin.Type))
{
throw new ArgumentOutOfRangeException("coin");
}
if (!this.acceptedVolumes.Contains(coin.Volume))
{
throw new ArgumentOutOfRangeException("coin");
}
this.TotalAmount += coin.Amount;
}
public void Resete()
{
this.TotalAmount = 0;
}
}
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
[TestClass]
public class CoinJarTest
{
[TestMethod]
public void Initialize_a_New_Coin()
{
var coin = new Coin(CoinType.US, 25, 100);
Assert.IsNotNull(coin);
Assert.AreEqual(CoinType.US, coin.Type);
Assert.AreEqual(25, coin.Volume);
Assert.AreEqual(100, coin.Amount);
}
[TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CoinJar_Should_Only_Accept_US_Coins()
{
var coin = new Coin(CoinType.Unknown, 25, 100);
var coinJar = new CoinJar();
coinJar.Add(coin);
}
[TestMethod, ExpectedException(typeof(ArgumentOutOfRangeException))]
public void CoinJar_Should_Only_Accept_25_Ounces_Coins()
{
var coin = new Coin(CoinType.US, 50, 100);
var coinJar = new CoinJar();
coinJar.Add(coin);
}
[TestMethod]
public void CoinJar_Should_Keep_Track_of_Total_Amount()
{
var coin1 = new Coin(CoinType.US, 25, 0.25m);
var coin2 = new Coin(CoinType.US, 25, 0.50m);
var coinJar = new CoinJar();
Assert.AreEqual(0, coinJar.TotalAmount);
coinJar.Add(coin1);
Assert.AreEqual(0.25m, coinJar.TotalAmount);
coinJar.Add(coin2);
Assert.AreEqual(0.75m, coinJar.TotalAmount);
coinJar.Resete();
Assert.AreEqual(0, coinJar.TotalAmount);
}
}
public enum CoinType
{
Unknown,
US
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment