Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Continued KataPotter

  • Loading branch information...
commit b64af60de0b147541c48c5128205d4459912b4ae 1 parent 2812426
@sklivvz authored
Showing with 107 additions and 146 deletions.
  1. +89 −85 Kata.Tests/PotterBasketTests.cs
  2. +18 −61 Kata/PotterBasket.cs
View
174 Kata.Tests/PotterBasketTests.cs
@@ -1,8 +1,4 @@
-using System;
-using System.Collections.Generic;
-using System.Text;
-using NUnit.Framework;
-
+using NUnit.Framework;
namespace Kata.Tests
{
@@ -12,67 +8,82 @@ namespace Kata.Tests
[TestFixture]
public class PotterBasketTests
{
+ private PotterBasket SetupBasket()
+ {
+ var basket = new PotterBasket();
+ return basket;
+ }
+
[Test]
- [TestCase("Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Chamber of Secrets")]
- [TestCase("Harry Potter and the Prisoner of Azkaban")]
- [TestCase("Harry Potter and the Goblet of Fire")]
- [TestCase("Harry Potter and the Order of the Phoenix")]
- public void OneCopyOfAnyOfTheFiveBooksCosts8Eur(string book)
+ public void HowMuchDoesThisBasketOfBooksCost()
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
- basket.Add(book);
+ basket.Add("Harry Potter and the Philosopher's Stone");
+ basket.Add("Harry Potter and the Philosopher's Stone");
+ basket.Add("Harry Potter and the Chamber of Secrets");
+ basket.Add("Harry Potter and the Chamber of Secrets");
+ basket.Add("Harry Potter and the Prisoner of Azkaban");
+ basket.Add("Harry Potter and the Prisoner of Azkaban");
+ basket.Add("Harry Potter and the Goblet of Fire");
+ basket.Add("Harry Potter and the Order of the Phoenix");
- Assert.AreEqual(8M, basket.Total);
+ Assert.AreEqual(51.20M, basket.Total);
}
[Test]
- [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void IfYouBuyTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooks(string book1, string book2)
+ [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets",
+ "Harry Potter and the Goblet of Fire", "Harry Potter and the Order of the Phoenix",
+ "Harry Potter and the Philosopher's Stone")]
+ public void IfYouBuyAll5YouGetAHuge25PercentDiscount(string book1, string book2, string book3, string book4,
+ string book5)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
basket.Add(book1);
basket.Add(book2);
+ basket.Add(book3);
+ basket.Add(book4);
+ basket.Add(book5);
- Assert.AreEqual(8M * 2 * 0.95M, basket.Total);
+ Assert.AreEqual(8M*5*0.75M, basket.Total);
}
[Test]
- [TestCase("Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Chamber of Secrets")]
- [TestCase("Harry Potter and the Prisoner of Azkaban")]
- [TestCase("Harry Potter and the Goblet of Fire")]
- [TestCase("Harry Potter and the Order of the Phoenix")]
- public void TwoCopiesOfTheSameBookCosts16Eur(string book)
+ [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Prisoner of Azkaban",
+ "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Goblet of Fire",
+ "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Order of the Phoenix",
+ "Harry Potter and the Philosopher's Stone")]
+ public void IfYouBuyThreeDifferentBooksYouGetA10PercentDiscountOnThoseThreeBooks(string book1, string book2,
+ string book3)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
- basket.Add(book);
- basket.Add(book);
+ basket.Add(book1);
+ basket.Add(book2);
+ basket.Add(book3);
- Assert.AreEqual(16M, basket.Total);
+ Assert.AreEqual(8M*3*0.9M, basket.Total);
}
-
[Test]
[TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Philosopher's Stone")]
[TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Philosopher's Stone")]
[TestCase("Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
[TestCase("Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void IfYouBuyTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooksOnly(string book1, string book2)
+ public void IfYouBuyTwoCopiesOfTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooksTwice(string book1,
+ string book2)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
basket.Add(book1);
basket.Add(book2);
+ basket.Add(book2);
basket.Add(book1);
- Assert.AreEqual(8M * 2 * 0.95M + 8M, basket.Total);
+ Assert.AreEqual(8M*4*0.95M, basket.Total);
}
[Test]
@@ -80,86 +91,79 @@ public void IfYouBuyTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooksOnly(
[TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Philosopher's Stone")]
[TestCase("Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
[TestCase("Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void IfYouBuyTwoCopiesOfTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooksTwice(string book1, string book2)
+ public void IfYouBuyTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooks(string book1, string book2)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
basket.Add(book1);
basket.Add(book2);
- basket.Add(book2);
- basket.Add(book1);
- Assert.AreEqual(8M * 4 * 0.95M, basket.Total);
+ Assert.AreEqual(8M*2*0.95M, basket.Total);
}
+
[Test]
- [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void IfYouBuyThreeDifferentBooksYouGetA10PercentDiscountOnThoseThreeBooks(string book1, string book2, string book3)
+ [TestCase("Harry Potter and the Chamber of Secrets", "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
+ public void IfYouBuyTwoDifferentBooksYouGetA5PercentDiscountOnThoseTwoBooksOnly(string book1, string book2)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
basket.Add(book1);
basket.Add(book2);
- basket.Add(book3);
+ basket.Add(book1);
- Assert.AreEqual(8M * 3 * 0.9M, basket.Total);
+ Assert.AreEqual(8M*2*0.95M + 8M, basket.Total);
}
[Test]
- [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets", "Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
- [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void With4DifferentBooksYouGetA20PercentDiscount(string book1, string book2, string book3, string book4)
+ [TestCase("Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Chamber of Secrets")]
+ [TestCase("Harry Potter and the Prisoner of Azkaban")]
+ [TestCase("Harry Potter and the Goblet of Fire")]
+ [TestCase("Harry Potter and the Order of the Phoenix")]
+ public void OneCopyOfAnyOfTheFiveBooksCosts8Eur(string book)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
- basket.Add(book1);
- basket.Add(book2);
- basket.Add(book3);
- basket.Add(book4);
+ basket.Add(book);
- Assert.AreEqual(8M * 4 * 0.8M, basket.Total);
+ Assert.AreEqual(8M, basket.Total);
}
[Test]
- [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets", "Harry Potter and the Goblet of Fire", "Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
- public void IfYouBuyAll5YouGetAHuge25PercentDiscount(string book1, string book2, string book3, string book4, string book5)
+ [TestCase("Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Chamber of Secrets")]
+ [TestCase("Harry Potter and the Prisoner of Azkaban")]
+ [TestCase("Harry Potter and the Goblet of Fire")]
+ [TestCase("Harry Potter and the Order of the Phoenix")]
+ public void TwoCopiesOfTheSameBookCosts16Eur(string book)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
- basket.Add(book1);
- basket.Add(book2);
- basket.Add(book3);
- basket.Add(book4);
- basket.Add(book5);
+ basket.Add(book);
+ basket.Add(book);
- Assert.AreEqual(8M * 5 * 0.75M, basket.Total);
+ Assert.AreEqual(16M, basket.Total);
}
[Test]
- public void HowMuchDoesThisBasketOfBooksCost()
+ [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets",
+ "Harry Potter and the Goblet of Fire", "Harry Potter and the Philosopher's Stone")]
+ [TestCase("Harry Potter and the Prisoner of Azkaban", "Harry Potter and the Chamber of Secrets",
+ "Harry Potter and the Order of the Phoenix", "Harry Potter and the Philosopher's Stone")]
+ public void With4DifferentBooksYouGetA20PercentDiscount(string book1, string book2, string book3, string book4)
{
- var basket = SetupBasket();
+ PotterBasket basket = SetupBasket();
- basket.Add("Harry Potter and the Philosopher's Stone");
- basket.Add("Harry Potter and the Philosopher's Stone");
- basket.Add("Harry Potter and the Chamber of Secrets");
- basket.Add("Harry Potter and the Chamber of Secrets");
- basket.Add("Harry Potter and the Prisoner of Azkaban");
- basket.Add("Harry Potter and the Prisoner of Azkaban");
- basket.Add("Harry Potter and the Goblet of Fire");
- basket.Add("Harry Potter and the Order of the Phoenix");
+ basket.Add(book1);
+ basket.Add(book2);
+ basket.Add(book3);
+ basket.Add(book4);
- Assert.AreEqual(51.20M, basket.Total);
- }
-
- private PotterBasket SetupBasket()
- {
- var basket = new PotterBasket();
- return basket;
+ Assert.AreEqual(8M*4*0.8M, basket.Total);
}
}
-
-
-}
+}
View
79 Kata/PotterBasket.cs
@@ -12,19 +12,13 @@ namespace Kata
/// </summary>
public class PotterBasket
{
- private readonly Dictionary<string, int> _cart = new Dictionary<string, int>();
+ private readonly List<string> _books = new List<string>();
public decimal Total
{
get
{
- var books = new List<string>();
- foreach (string book in _cart.Keys)
- {
- for (int i=0; i<_cart[book]; i++)
- books.Add(book);
- }
- return GetBestPriceFor(books);
+ return GetBestPriceFor(_books);
}
}
@@ -33,7 +27,7 @@ private decimal GetBestPriceFor(List<string> books)
int distinct = books.Distinct().Count();
if (distinct == 1)
{
- return 8m*books.Count;
+ return 8m * books.Count;
}
if (distinct == 2)
{
@@ -44,15 +38,15 @@ private decimal GetBestPriceFor(List<string> books)
books.Remove(second);
if (books.Count == 0)
- return 8M*2*0.95M;
+ return 8M * 2 * 0.95M;
- return 8M*2*0.95M + GetBestPriceFor(books);
+ return 8M * 2 * 0.95M + GetBestPriceFor(books);
}
if (distinct == 3)
{
string first = books[0];
string second = books.First(x => x != first);
- string third = books.First(x => x != first && x!= second);
+ string third = books.First(x => x != first && x != second);
books.Remove(first);
books.Remove(second);
@@ -81,67 +75,30 @@ private decimal GetBestPriceFor(List<string> books)
return 8M * 4 * 0.8M + GetBestPriceFor(books);
}
- var altBooks1 = new List<string>(books);
-
+ var booksLeftWhenTakingFive = new List<string>(books);
foreach (string book in books.Distinct())
{
- altBooks1.Remove(book);
- }
- decimal bestPrice = 8M*5*0.75M;
- if (altBooks1.Count>0)
- bestPrice += GetBestPriceFor(altBooks1);
-
- foreach (string bookToKeep in books.Distinct())
- {
- var altBooks2 = new List<string>(books.ToArray());
- foreach (string bookToRemove in books.Distinct())
- {
- if (bookToRemove!=bookToKeep)
- altBooks2.Remove(bookToRemove);
- }
- var testPrice = GetBestPriceFor(altBooks2) + 8M*4*0.8M;
- if (testPrice < bestPrice)
- bestPrice = testPrice;
+ booksLeftWhenTakingFive.Remove(book);
}
- return bestPrice;
-
-
- }
-
- private void TakeDifferent(int howMany)
- {
- var books = BooksInCart;
- for (int i = 0; i < howMany; i++)
+
+ if (booksLeftWhenTakingFive.Count > 0)
{
- Remove(books[i]);
+ var booksLeftWhenTakingFiveCopy = new List<string>(booksLeftWhenTakingFive);
+ return books
+ .Distinct()
+ .Select(bookToKeep => new List<string>(booksLeftWhenTakingFiveCopy) { bookToKeep })
+ .Select(altBooks3 => GetBestPriceFor(altBooks3) + 8M*4*0.8M)
+ .Concat(new[] { 8M * 5 * 0.75M + GetBestPriceFor(booksLeftWhenTakingFive) }).Min();
}
- }
-
- private string[] BooksInCart
- {
- get
- {
- var keys = new string[_cart.Count];
- _cart.Keys.CopyTo(keys, 0);
- return keys;
- }
+ return 8M * 5 * 0.75M;
}
- private void Remove(string book)
- {
- _cart[book]--;
- if (_cart[book] == 0)
- _cart.Remove(book);
- }
public void Add(string book)
{
- if (_cart.ContainsKey(book))
- _cart[book]++;
- else
- _cart[book] = 1;
+ _books.Add(book);
}
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.