コード例 #1
0
ファイル: Calculator.cs プロジェクト: h0lger/KataPotterDavid
    /// <summary>
    /// Calculates total price incl discount
    /// </summary>
    /// <param name="sCart"></param>
    /// <returns></returns>
    public static double CalcTotalPrice(ShoppingCart sCart)
    {
      double totPrice = 0;

      sCart.Items.ToList().ForEach(x => totPrice += x.Price);

      return (totPrice - CalcDiscount(sCart, totPrice));
    }
コード例 #2
0
ファイル: UnitTests.cs プロジェクト: h0lger/KataPotterDavid
        public void AddRemoveBooks()
        {
            ShoppingCart SCart = new ShoppingCart();
              //Creation of 100 books
              for (int i = 0; i < 100; i++)
              {
            Book tmpBook = new Book(
            (100 * i), "TestBook", Guid.NewGuid().ToString(), "Harry", Book.SeriesEnum.HarryPotter);
            SCart.Items.Add(tmpBook);
              }
              Assert.AreEqual<int>(100, SCart.Count());

              //Add 20 copies
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 20);
              Assert.AreEqual<int>(120, SCart.Count());
        }
コード例 #3
0
ファイル: Calculator.cs プロジェクト: h0lger/KataPotterDavid
    public static double CalcDiscount(ShoppingCart sCart, double totPrice)
    {
      //Calc Harry Potter series discount
      Dictionary<IList<int>, double> combinations = new Dictionary<IList<int>, double>();

      //Search for all combination between 2-5 (the one's you got discount for)
      for (int i = 5; i >= 2; i--)
      {
        double discount = 0;
        var uniqueTitles = sCart.Items.Select(x => x.Title).Distinct();
        HashSet<Guid> handled = new HashSet<Guid>(); //To know calculated
        IList<int> uniqueSets = new List<int>();

        while (sCart.Items.Any(x => !handled.Contains(x.Id)))
        {
          HashSet<string> tmpTitles = new HashSet<string>();
          var uniques = sCart.Items.Where(x => !handled.Contains(x.Id));
          foreach (var sItem in uniques)
          {
            if (tmpTitles.Count <= i && !tmpTitles.Contains(sItem.Title))
            {
              tmpTitles.Add(sItem.Title);
              handled.Add(sItem.Id);
            }
          }

          uniqueSets.Add(tmpTitles.Count);
        }

        //Calculate discount for each set
        foreach (int uni in uniqueSets)
        {
          double price = sCart.Items.First().Price;
          discount += CalcSetDiscount(uni, price);
        }

        //Save the total discount for this combination
        combinations.Add(uniqueSets, discount);
      }     

      //Pick the comination with highest possible discount
      return combinations.Max(x => x.Value);
    }
コード例 #4
0
ファイル: UnitTests.cs プロジェクト: h0lger/KataPotterDavid
        public void CalcTotPrice()
        {
            ShoppingCart SCart = new ShoppingCart();
              /*
              ////1 copy, 1 book
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 1);
              Assert.AreEqual<double>(8, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              ////2 copies, 1book
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 2);
              Assert.AreEqual<double>(16, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              ////5 copies, 1book
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 5);
              Assert.AreEqual<double>(40, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();
               */

              ////2 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 1);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 1);
              Assert.AreEqual<double>(15.2, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //3 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 1);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 1);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 1);
              Assert.AreEqual<double>(21.6, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //4 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 1);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 1);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 1);
              Assert.AreEqual<double>(25.6, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //5 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 1);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 1);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 1);
              Assert.AreEqual<double>(30, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //5 different books with 2 copies
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 2);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 2);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 2);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 2);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 2);
              Assert.AreEqual<double>(60, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //6 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 2);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 1);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 1);
              Assert.AreEqual<double>(38, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //7 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 2);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 2);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 1);
              Assert.AreEqual<double>(45.2, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //8 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 2);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 2);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 2);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 1);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 1);
              Assert.AreEqual<double>(51.2, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();

              //80 different books
              SCart.AddToShoppingCart(Utils.GetFirstBook(), 20);
              SCart.AddToShoppingCart(Utils.GetSecondBook(), 20);
              SCart.AddToShoppingCart(Utils.GetThirdBook(), 20);
              SCart.AddToShoppingCart(Utils.GetFourthBook(), 10);
              SCart.AddToShoppingCart(Utils.GetFifthBook(), 10);
              Assert.AreEqual<double>(512, Calculator.CalcTotalPrice(SCart));
              SCart.Items.Clear();
        }