Esempio n. 1
0
        public void ShouldFindItemInteresting()
        {
            var item  = new Item(1);
            var buyer = PersonFactory.BuyerBuilder().WithStartBalance(400).Build();
            var collectorsDecoratorItem = new CollectorsDecorator(item);

            Assert.True(buyer.IsInteresting(collectorsDecoratorItem));
        }
Esempio n. 2
0
        public void ShouldFindItemNotInterestingBecauseOfLackOfMoney()
        {
            var item  = new Item(1);
            var buyer = PersonFactory.BuyerBuilder().WithStartBalance(10).Build();
            var collectorsDecoratorItem = new CollectorsDecorator(item);

            Assert.False(buyer.IsInteresting(collectorsDecoratorItem));
        }
Esempio n. 3
0
        public void ShouldHaveOneMoreItemAfterBuying()
        {
            var seller = PersonFactory.SellerBuilder(false).WithRandomBalance().Build();
            var buyer  = PersonFactory.BuyerBuilder().WithNumberOfBelongings(0).WithStartBalance(100).Build();

            IItem item = new Item(1);

            buyer.BuyItem(item, seller);

            Assert.Equal(1, buyer.Belongings.Count);
        }
Esempio n. 4
0
        public void cannotSellBecauseIsNotListedForSale()
        {
            var seller = PersonFactory.SellerBuilder(false).WithRandomBalance().WithNumberOfBelongings(10).Build();
            var Buyer  = PersonFactory.BuyerBuilder().WithStartBalance(100).Build();

            var items = seller.Belongings.ToList();

            items.ForEach(i => seller.BuyItem(i));

            Assert.Equal(10, seller.Belongings.Count);
        }
Esempio n. 5
0
        public void NoBuyersIsEqualToEachOther()
        {
            var listOfBuyers = new List <Buyer>();

            for (var i = 0; i < 100; i++)
            {
                listOfBuyers.Add(PersonFactory.BuyerBuilder().WithNumberOfBelongings(0).WithStartBalance(100).Build());
            }

            var num = listOfBuyers.Distinct().Count();

            Assert.Equal(100, num);
        }
Esempio n. 6
0
        public void ShouldHavePriceOfBoughtItemLessInBalance()
        {
            var startBalance = 100;
            var seller       = PersonFactory.SellerBuilder(false).WithRandomBalance().Build();
            var buyer        = PersonFactory.BuyerBuilder().WithNumberOfBelongings(0).WithStartBalance(startBalance).Build();

            IItem item      = new Item(1);
            var   itemPrice = item.Price;

            buyer.BuyItem(item, seller);

            Assert.Equal(startBalance - itemPrice, buyer.Balance);
        }
Esempio n. 7
0
        public void shouldHaveThreeLessItemAfterSellingThree()
        {
            var seller = PersonFactory.SellerBuilder(false).WithRandomBalance().WithNumberOfBelongings(10).Build();
            var Buyer  = PersonFactory.BuyerBuilder().WithStartBalance(100).Build();

            var items = seller.Belongings.ToList();

            seller.SellItems(3);

            items.ForEach(i => seller.BuyItem(i));

            Assert.Equal(7, seller.Belongings.Count);
        }
Esempio n. 8
0
        public void CanBuyItemThatCostsSameAsBalance()
        {
            IItem item         = new Item(1);
            var   startBalance = (int)item.Price;
            var   seller       = PersonFactory.SellerBuilder(false).WithRandomBalance().Build();
            var   buyer        = PersonFactory.BuyerBuilder().WithNumberOfBelongings(0).WithStartBalance(startBalance).Build();

            if (buyer.IsInteresting(item))
            {
                buyer.BuyItem(item, seller);
            }

            Assert.Equal(1, buyer.NumberOfItemsBought);
        }
Esempio n. 9
0
        public void BuyersIsDuplicate()
        {
            var listOfBuyers = new List <Buyer>();
            var buyer        = PersonFactory.BuyerBuilder().WithNumberOfBelongings(0).WithStartBalance(100).Build();

            for (var i = 0; i < 100; i++)
            {
                listOfBuyers.Add(buyer);
            }

            var num = listOfBuyers.Distinct().Count();

            Assert.Equal(1, num);
        }
Esempio n. 10
0
        public void StartSimulation()
        {
            IsRunningCheck();
            List <Seller> sellers;
            List <Buyer>  buyers;

            lock (RunningCheckLock) {
                RunningSimulation   = true;
                _isCurrentlyRunning = true;

                var buyerBuilder = PersonFactory.BuyerBuilder()
                                   .WithStartBalance(_buyerStartBalance);

                buyers = new string[_numberOfBuyers]
                         .Select(_null => buyerBuilder.Build()).ToList();


                var sellerBuilder = PersonFactory.SellerBuilder(false)
                                    .WithNumberOfBelongings(_numberOfSellerBelongings);

                sellers = new string[_numberOfSellers]
                          .Select(_null => sellerBuilder.Build()).ToList();

                var loopCount = 1;
                while (sellers.Any(seller => seller.HasMoreNotListedItems))
                {
                    WaitHandle[] waitHandles = sellers.FindAll(seller => seller.HasMoreNotListedItems)
                                               .OrderBy(seller => Guid.NewGuid())
                                               .Select(seller => {
                        var waitHandle = new ManualResetEvent(false);
                        ThreadPool.QueueUserWorkItem(delegate(object state) {
                            var mre = (ManualResetEvent)state;
                            var threadSafeRandom = new ThreadSafeRandom();
                            seller.SellItems(threadSafeRandom.Next(1, 50));
                            mre.Set();
                        }, waitHandle);
                        return(waitHandle);
                    }).ToArray();

                    WaitHandle.WaitAll(waitHandles);
                    Console.WriteLine($"All sellers have now sold it's #{loopCount++} batch");
                }

                _isCurrentlyRunning = false;
                RunningSimulation   = false;
                Console.WriteLine("Everything is sold");
            }
            PrintStatistics(sellers, buyers);
        }