Skip to content

Instantly share code, notes, and snippets.

@stoolrossa
Last active December 19, 2015 03:59
Show Gist options
  • Save stoolrossa/5894325 to your computer and use it in GitHub Desktop.
Save stoolrossa/5894325 to your computer and use it in GitHub Desktop.
// code that's testable
public class Checkout
{
private IPriceList drinksMenu;
private IClock clock;
public Checkout(IPriceList drinksMenu, IClock clock)
{
this.drinksMenu = drinksMenu;
this.clock = clock;
}
public decimal CalculateCost(List<Order> orders)
{
var total = 0.0m;
// total the orders
foreach(var order in orders)
{
total += this.drinksMenu.LookupPrice(order.Drink) * order.Quantity;
}
// half price on Friday 5pm-6pm
var dateTimeNow = this.clock.GetDateTimeNow();
if (dateTimeNow.DayOfWeek == DayOfWeek.Friday && dateTimeNow.Hour == 17)
{
total /= 2;
}
// calculate the tax
total += total * 0.1m;
return total;
}
}
public class Order
{
public string Drink { get; set; }
public int Quantity { get; set; }
}
public interface IPriceList
{
decimal LookupPrice(string itemName);
}
public interface IClock
{
DateTime GetDateTimeNow();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment