Example #1
0
        public void CalculateReturnsTotalWithSumOfDiscounts()
        {
            var priceProviderMock = new Mock <IPriceProvider>();

            priceProviderMock
            .Setup(x => x.GetPrice(It.IsAny <string>()))
            .Returns(100m);

            var basketItems = new List <BasketItem>
            {
                new BasketItem("test_product")
            };

            var firstDiscountMock  = new Mock <IDiscount>();
            var secondDiscountMock = new Mock <IDiscount>();

            firstDiscountMock
            .Setup(x => x.GetDiscount(basketItems))
            .Returns(10m);
            secondDiscountMock
            .Setup(x => x.GetDiscount(basketItems))
            .Returns(2m);

            var totalCalculator = new TotalCalculator(
                new List <IDiscount>
            {
                firstDiscountMock.Object, secondDiscountMock.Object
            },
                priceProviderMock.Object);

            var total = totalCalculator.Calculate(basketItems);

            Assert.AreEqual(88m, total);
        }
Example #2
0
        private IList <BroAmount> GetTotal()
        {
            var amounts = TotalCalculator.Calculate(repository.GetTotal(), repository.GetPendingTransactions());
            var bros    = repository.GetBros();

            return(amounts.OrderBy(x => bros.First(b => b.Id == x.Bro).Name).ToArray());
        }
Example #3
0
        public void MustCalculateTotalBasedOnNumberOfBooksSold(int quantitySold, double expectedTotal, Category bookCategory)
        {
            var sut    = new TotalCalculator();
            var result = sut.CalculateTotal(GenerateListWith(quantitySold, bookCategory));

            Assert.AreEqual(expectedTotal, result);
        }
        public ActionResult GetTotalInCurrency(int currencyId)
        {
            using (var db = DbFactory.Open())
            {
                var now = DateTime.UtcNow.Date;

                Transaction[]             transactions;
                Bro[]                     bros;
                Dictionary <Bro, decimal> total;
                ExchangeRate              rate;

                using (new PerfCounter("total-transactions"))
                {
                    // loading transactions and their targets with one query to avoid N+1 query for each transaction targets
                    transactions = db.Query <Transaction>()
                                   .SelectMany(t => t.Targets, (transaction, bro) => new { transaction, bro })
                                   .AsEnumerable()
                                   .GroupBy(p => p.transaction, p => p.bro, (transaction, targets) =>
                    {
                        transaction.Targets = targets.ToArray();
                        return(transaction);
                    })
                                   .ToArray();
                }

                using (new PerfCounter("total-bros")) bros = db.Query <Bro>().ToArray();
                using (new PerfCounter("total-rate")) rate = ExchangeRateProvider.Get(db.Query <ExchangeRate>(), currencyId, now);
                using (new PerfCounter("total-calc")) total = TotalCalculator.CalculateInCurrency(rate, transactions, bros);
                var broTotalsRest = total.Keys.Select(bro => new BroTotalInCurrencyRest(bro, total[bro])).ToList();
                return(Json(new TotalInCurrencyRest(broTotalsRest, rate), JsonRequestBehavior.AllowGet));
            }
        }
Example #5
0
        public void CalculateCallsAllDiscounts()
        {
            var firstDiscountMock  = new Mock <IDiscount>();
            var secondDiscountMock = new Mock <IDiscount>();
            var priceProviderMock  = new Mock <IPriceProvider>();

            priceProviderMock
            .Setup(x => x.GetPrice(It.IsAny <string>()))
            .Returns(0m);
            var totalCalculator = new TotalCalculator(
                new List <IDiscount>
            {
                firstDiscountMock.Object, secondDiscountMock.Object
            },
                priceProviderMock.Object);

            var basketItems = new List <BasketItem>
            {
                new BasketItem("test_product")
            };

            totalCalculator.Calculate(basketItems);

            firstDiscountMock.Verify(x => x.GetDiscount(basketItems), Times.Once);
            secondDiscountMock.Verify(x => x.GetDiscount(basketItems), Times.Once);
        }
 public void Given_item_d_is_scanned_Then_price_is_15()
 {
     var display = this;
     var totalCalculator = new TotalCalculator(display, new PriceListRepository());
     var checkout = new Checkout(totalCalculator, new DiscountCalculator(totalCalculator, new ItemDiscountRepository()));
     const string item = "D";
     checkout.Scan(item);
     checkout.Finish();
     Assert.That(_totalRecieved, Is.EqualTo(15));
 }
Example #7
0
        public void MustCalculateTotalCombiningSciFiCategoryAndOtherCategories()
        {
            var sut          = new TotalCalculator();
            var completeList = new List <Book>();

            completeList.AddRange(GenerateListWith(3, Category.ScienceFiction));
            completeList.AddRange(GenerateListWith(5, Category.Math));

            var result = sut.CalculateTotal(completeList);

            Assert.AreEqual(268.80, result);
        }
 public void Given_A_and_B_items_are_scanned_Then_total_price_is_80()
 {
     var display = this;
     var totalCalculator = new TotalCalculator(display, new PriceListRepository());
     var checkout = new Checkout(totalCalculator, new DiscountCalculator(totalCalculator, new ItemDiscountRepository()));
     const string itemA = "A";
     const string itemB = "B";
     checkout.Scan(itemA);
     checkout.Scan(itemB);
     checkout.Finish();
     Assert.That(_totalRecieved, Is.EqualTo(80));
 }
        //Automatic draw for house turn
        private void houseTurn()
        {
            while (TotalCalculator.CalculateTotal(house.GetHand()) <= 16)
            {
                house.SetHand(house.TakeHit(cards, house.GetHand()));
                cards = Resizer.ResizeDeck(cards, 1);
            }
            displayHouseHand(house, labelHouseName, labelHouseCards, labelHouseTotal);

            panelBet.Hide();
            panelGameOver.Show();
            panelScore.Show();
            displayResults();
        }
        public ActionResult GetTotal()
        {
            using (var db = DbFactory.Open())
            {
                List <Transaction> transactions;
                List <Bro>         bros;
                Dictionary <Bro, Dictionary <Currency, decimal> > total;

                using (new PerfCounter("total-transactions")) transactions = db.Query <Transaction>().ToList();
                using (new PerfCounter("total-bros")) bros = db.Query <Bro>().ToList();
                using (new PerfCounter("total-calcall")) total = TotalCalculator.Calculate(transactions, bros);

                var result = total.Keys.Select(bro => new BroTotalRest(bro, total[bro])).ToList();
                return(Json(result, JsonRequestBehavior.AllowGet));
            }
        }
 private void displayHand(Player player, Label labelName, Label labelCards, Label labelTotal)
 {
     labelName.Text = player.Name;
     displayCards(player, labelCards);
     labelTotal.Text = TotalDisplayer.DisplayTotal(TotalCalculator.CalculateTotal(player.GetHand()));
 }
Example #12
0
 public Checkout(TotalCalculator totalCalculator, DiscountCalculator discountCalculator)
 {
     _basket = new Basket(discountCalculator, totalCalculator);
 }
 private void displayHouseHand(House house, Label labelName, Label labelCards, Label labelTotal)
 {
     labelName.Text = house.Name;
     displayHouseCards(house, labelCards);
     labelTotal.Text = TotalDisplayer.DisplayTotal(TotalCalculator.CalculateTotal(house.GetHand()));
 }
            public void TestRunningTotalList()
            {
                ICollection <int> list = new TotalCalculator <int>(new List <int>());

                DoCollection(list);
            }
        //Panel hit or stay
        private void btnHit_Click(object sender, EventArgs e)
        {
            //Not bust or blackjack, your turn
            if (TotalCalculator.CalculateTotal(players[betCounter].GetHand()) < 21)
            {
                players[betCounter].SetHand(players[betCounter].TakeHit(cards, players[betCounter].GetHand()));
                cards = Resizer.ResizeDeck(cards, 1);

                if (players.Length == 1)
                {
                    display1Player();
                }
                else if (players.Length == 2)
                {
                    display2Player();
                }
                else if (players.Length == 3)
                {
                    display3Player();
                }
                else if (players.Length == 4)
                {
                    display4Player();
                }
                else if (players.Length == 5)
                {
                    display5Player();
                }
                else if (players.Length == 6)
                {
                    display6Player();
                }
                else
                {
                    display7Player();
                }

                //blackjack or bust, next players turn
                if (TotalCalculator.CalculateTotal(players[betCounter].GetHand()) >= 21)
                {
                    //Any players left? No, house turn
                    if (betCounter < players.Length - 1)
                    {
                        betCounter++;
                        labelPlayerTurnName.Text = players[betCounter].Name;
                    }
                    else
                    {
                        houseTurn();
                    }
                }
            }
            //blackjack or bust, next players turn
            else
            {
                //Any players left? No, house turn
                if (betCounter < players.Length - 1)
                {
                    betCounter++;
                    labelPlayerTurnName.Text = players[betCounter].Name;
                }
                else
                {
                    houseTurn();
                }
            }
        }
        //display the results in the score panel
        private void displayResults()
        {
            scoreLabelPlayerList.Add(labelScorePlayer1);
            scoreLabelPlayerList.Add(labelScorePlayer2);
            scoreLabelPlayerList.Add(labelScorePlayer3);
            scoreLabelPlayerList.Add(labelScorePlayer4);
            scoreLabelPlayerList.Add(labelScorePlayer5);
            scoreLabelPlayerList.Add(labelScorePlayer6);
            scoreLabelPlayerList.Add(labelScorePlayer7);

            outcomeLabelPlayerList.Add(outcomePlayer1);
            outcomeLabelPlayerList.Add(outcomePlayer2);
            outcomeLabelPlayerList.Add(outcomePlayer3);
            outcomeLabelPlayerList.Add(outcomePlayer4);
            outcomeLabelPlayerList.Add(outcomePlayer5);
            outcomeLabelPlayerList.Add(outcomePlayer6);
            outcomeLabelPlayerList.Add(outcomePlayer7);

            //Find players' highest score
            int highScore = 0;

            for (int i = 0; i < players.Length; i++)
            {
                if (TotalCalculator.CalculateTotal(players[i].GetHand()) > highScore && TotalCalculator.CalculateTotal(players[i].GetHand()) < 22)
                {
                    highScore = TotalCalculator.CalculateTotal(players[i].GetHand());
                }
            }

            //Player outcome
            for (int i = 0; i < players.Length; i++)
            {
                scoreLabelPlayerList[i].Show();
                scoreLabelPlayerList[i].Text = players[i].Name;

                outcomeLabelPlayerList[i].Show();
                if (TotalCalculator.CalculateTotal(players[i].GetHand()) < 22)
                {
                    if (TotalCalculator.CalculateTotal(house.GetHand()) > 21)
                    {
                        outcomeLabelPlayerList[i].Text = "Wins!";
                    }
                    else if (TotalCalculator.CalculateTotal(players[i].GetHand()) > TotalCalculator.CalculateTotal(house.GetHand()))
                    {
                        outcomeLabelPlayerList[i].Text = "Wins!";
                    }
                    else if (TotalCalculator.CalculateTotal(players[i].GetHand()) == TotalCalculator.CalculateTotal(house.GetHand()) &&
                             TotalCalculator.CalculateTotal(players[i].GetHand()) == highScore)
                    {
                        outcomeLabelPlayerList[i].Text = "Tie!";
                    }
                    else
                    {
                        outcomeLabelPlayerList[i].Text = "Loses!";
                    }
                }
                else
                {
                    outcomeLabelPlayerList[i].Text = "Loses!";
                }
            }

            //House outcome
            labelScoreHouse.Show();
            outcomeHouse.Show();
            labelScoreHouse.Text = house.Name;
            if (TotalCalculator.CalculateTotal(house.GetHand()) == highScore)
            {
                outcomeHouse.Text = "Tie!";
            }
            else if (TotalCalculator.CalculateTotal(house.GetHand()) > highScore && TotalCalculator.CalculateTotal(house.GetHand()) < 22)
            {
                outcomeHouse.Text = "Wins!";
            }
            else
            {
                outcomeHouse.Text = "Loses!";
            }
        }
Example #17
0
 public Basket(DiscountCalculator discountCalculator, TotalCalculator totalCalculator)
 {
     _discountCalculator = discountCalculator;
     _totalCalculator = totalCalculator;
 }
        //Panel enter names
        private void btnOK2_Click(object sender, EventArgs e)
        {
            //Set player name
            if (count < playerCount)
            {
                players[count].Name = txtPlayerName.Text;

                txtPlayerName.Clear();

                count++;
                if (count < playerCount)
                {
                    labelPlayerCount2.Text = (count + 1).ToString();
                    players[count].Name    = txtPlayerName.Text;
                }
            }

            //All players are named
            if (count >= playerCount)
            {
                panelAddName.Hide();

                cards = deck.DealDeck(cards, players, house);

                //Display player hands
                panelPlayerHands.Show();
                for (int i = 0; i < players.Length; i++)
                {
                    playerPanelList[i].Show();
                }
                panelHouse.Show();

                if (players.Length == 1)
                {
                    display1Player();
                }
                else if (players.Length == 2)
                {
                    display2Player();
                }
                else if (players.Length == 3)
                {
                    display3Player();
                }
                else if (players.Length == 4)
                {
                    display4Player();
                }
                else if (players.Length == 5)
                {
                    display5Player();
                }
                else if (players.Length == 6)
                {
                    display6Player();
                }
                else
                {
                    display7Player();
                }

                //Display House hand
                string houseCards = "";
                int    houseTotal = TotalCalculator.CalculateTotal(house.GetHand());
                for (int i = 0; i < house.GetHand().Length; i++)
                {
                    if (i == 0 && houseTotal < 21)
                    {
                        houseCards += "XX\n";
                        i++;
                    }
                    houseCards += house.GetHand()[i].GetSuit().ToString() +
                                  house.GetHand()[i].GetFace();
                }
                labelHouseCards.Text = houseCards;
                if (houseTotal == 21)
                {
                    labelHouseTotal.Text = TotalDisplayer.DisplayTotal(TotalCalculator.CalculateTotal(house.GetHand()));
                }

                panelBet.Show();
                labelPlayerTurnName.Text = players[betCounter].Name;
            }
        }