Beispiel #1
0
        public void SucessfullyBarterWithInsufficientBarterButEnoughToGetSomething()
        {
            MarketMock.Setup(x => x.GetPrice(JobInput.Object))
            .Returns(20);

            JobInputs.SetProductAmount(JobInput.Object, 6);

            var changeRet = new ProductAmountCollection();

            changeRet.SetProductAmount(JobInput.Object, 5);

            // Setup Market Change for price.
            MarketMock.Setup(x => x.ChangeForPrice(JobInputs, 100))
            .Returns(changeRet);

            // Add Storage and ForSale
            sut.Storage.AddProducts(LifeNeed.Object, 1);
            sut.ForSale.AddProducts(LifeNeed.Object, 1);

            // Barter 1 LifeNeed for 1 JobInput.
            var result = sut.BarterGood(JobInputs, LifeNeed.Object, 2, MarketMock.Object);

            // check transaction
            AssertProductAmountIsEqual(result, LifeNeed, 1);
            AssertProductAmountIsEqual(result, JobInput, -5);

            // Ensure no change in storage or for sale
            AssertProductAmountIsEqual(sut.Storage, LifeNeed, 1);
            AssertProductAmountIsEqual(sut.ForSale, LifeNeed, 1);

            // Ensure that receipt doesn't go above storage or ForSale
            Assert.That(sut.Storage.GetProductValue(LifeNeed.Object),
                        Is.GreaterThanOrEqualTo(result.GetProductValue(LifeNeed.Object)));
            Assert.That(sut.ForSale.GetProductValue(LifeNeed.Object),
                        Is.GreaterThanOrEqualTo(result.GetProductValue(LifeNeed.Object)));
        }
        /// <summary>
        /// Consumes the given set of goods.
        /// </summary>
        /// <param name="goods">The goods to attempt to consume.</param>
        /// <param name="satisfaction">The satisfaction we'll be filling out as we go.</param>
        /// <returns>The change in products stored.</returns>
        private IProductAmountCollection ConsumeGoods(IProductAmountCollection goods,
                                                      IProductAmountCollection satisfaction)
        {
            var result = new ProductAmountCollection();

            // for each good to consume.
            foreach (var pair in goods)
            {
                // Get the item and amount of the person.
                var product = pair.Item1;
                var amount  = pair.Item2;

                // Assume All items being consumed are in storage already,
                // if they aren't we have a consistency problem.
                // get the satisfaction, capping it at 1.
                var sat = Math.Min(1, Storage.GetProductValue(product) / amount);

                // If satisfaction can't be met, subtract what you can.
                if (sat < 1)
                {
                    result.SubtractProducts(product,
                                            Storage.GetProductValue(product));

                    Storage.SubtractProducts(product,
                                             Storage.GetProductValue(product));
                }
                else // If greater than 1, then substract everything needed.
                {
                    result.SubtractProducts(product, amount);

                    Storage.SubtractProducts(product, amount);
                }

                // Finally, set it's satisfaction.
                satisfaction.SetProductAmount(product, sat);
            }

            // Return change in products stored.
            return(result);
        }