示例#1
0
 /// <summary>
 /// продажа товара
 /// </summary>
 /// <param name="sellItem"></param>
 /// <param name="customer"></param>
 /// <returns></returns>
 public virtual bool SellTheItem(GoodsItem sellItem, Customer customer)
 {
     if (ChechGoodsIsEnough(sellItem))
     {
         if (CheckIfEnoughMoney(sellItem, DBalance))
         {
             if (DoesHaveEnoughChange(sellItem, DBalance))
             {
                 return(_SellTheItem(sellItem, customer));
             }
             else
             {
                 CancelOrder(customer);
                 DoesNotHaveEnoughChange();
                 return(false);
             }
         }
         else
         {
             //CancelOrder(customer);
             DoesNotHaveEnoughMoney();
             return(false);
         }
     }
     else
     {
         CancelOrder(customer);
         DoesNotHaveEnoughGoods();
         return(false);
     }
 }
示例#2
0
        /// <summary>
        /// Рассчитываем сдачу
        /// </summary>
        /// <param name="sellItem"></param>
        /// <param name="DBalance"></param>
        /// <returns></returns>
        private List <Coin> CountChange(GoodsItem sellItem, decimal DBalance)
        {
            decimal     changeAmount = GetChangeAmount(sellItem, DBalance);
            Coin        _coin        = null;
            List <Coin> _coins       = new List <Coin> {
            };

            while (changeAmount > 0)
            {
                Func <Coin, bool> changeCondition = a => a.Value <= changeAmount && (changeAmount - a.Value) >= 0 &&
                                                    !_coins.Contains(a);
                _coin = Wallet.GetMaxCoinUnderCondition(a => changeCondition(a));

                if (_coin != null)
                {
                    changeAmount -= _coin.Value;
                    _coins.Add(_coin);
                }
                else
                {
                    break;
                }
            }

            if (changeAmount == 0)
            {
                return(_coins);
            }
            return(null);
        }
示例#3
0
 /// <summary>
 /// Выдаем товар
 /// </summary>
 /// <param name="goodItem"></param>
 /// <param name="customer"></param>
 public virtual void TakeTheGood(GoodsItem goodItem, Customer customer)
 {
     if (customer.GetTheGood(goodItem))
     {
         GoodsStorage.RemoveItems(a => a.Equals(goodItem));
         DBalance = 0;
     }
 }
示例#4
0
 /// <summary>
 /// Проверяем наличие товара
 /// </summary>
 /// <param name="sellItem"></param>
 /// <returns></returns>
 private bool ChechGoodsIsEnough(GoodsItem sellItem)
 {
     if (GoodsStorage.GetItem(a => a.Equals(sellItem)) != null)
     {
         return(true);
     }
     return(false);
 }
示例#5
0
 private decimal GetChangeAmount(GoodsItem sellItem, decimal DBalance)
 {
     if (sellItem != null)
     {
         return(DBalance - sellItem.Value);
     }
     else
     {
         return(DBalance);
     }
 }
示例#6
0
        /// <summary>
        /// проверяем хватает ли денег
        /// </summary>
        /// <param name="sellItem"></param>
        /// <param name="DBalance"></param>
        /// <returns></returns>
        private bool CheckIfEnoughMoney(GoodsItem sellItem, decimal DBalance)
        {
            decimal moneyAmount = DBalance;

            if (moneyAmount >= sellItem.Value)
            {
                return(true);
            }
            else
            {
                //NewMessage("Sorry money amount is not enough");
                return(false);
            }
        }
        private Transaction TransactionPerform(TradeParticipant participant1,
                                               TradeParticipant participant2,
                                               List <Coin> money,
                                               GoodsItem sellItem, TransactionType Ttype)
        {
            var transaction = Transaction.Create(this, participant2, money, sellItem, Ttype);

            if (transaction != null)
            {
                transaction.Perform();
                return(transaction);
            }
            return(null);
        }
        public static Transaction Create(TradeParticipant participant1,
                                         TradeParticipant participant2,
                                         List <Coin> money,
                                         GoodsItem sellItem, TransactionType Ttype)
        {
            Transaction transaction = new Transaction(
                participant1,
                participant2,
                money,
                sellItem,
                Ttype);

            return(transaction);
        }
示例#9
0
        /// <summary>
        /// Проверяем хватает ли сдачи
        /// </summary>
        /// <param name="sellItem"></param>
        /// <param name="DBalance"></param>
        /// <returns></returns>
        private bool DoesHaveEnoughChange(GoodsItem sellItem, decimal DBalance)
        {
            decimal changeAmount = GetChangeAmount(sellItem, DBalance);

            if (changeAmount == 0)
            {
                return(true);
            }

            if (CountChange(sellItem, DBalance) != null)
            {
                return(true);
            }
            return(false);
        }
示例#10
0
 private Transaction(TradeParticipant participant1,
                     TradeParticipant participant2,
                     List <Coin> money,
                     GoodsItem sellItem, TransactionType Ttype)
 {
     if (Ttype == TransactionType.Sell)
     {
         this.participant1 = participant1;
         this.participant2 = participant2;
     }
     else
     {
         this.participant1 = participant2;
         this.participant2 = participant1;
     }
     this.money    = money;
     this.sellItem = sellItem;
     this.Ttype    = Ttype;
 }
示例#11
0
        /// <summary>
        /// продаем товар
        /// </summary>
        /// <param name="sellItem"></param>
        /// <param name="customer"></param>
        /// <returns></returns>
        private bool _SellTheItem(GoodsItem sellItem, Customer customer)
        {
            List <Coin> change;

            if (customer.GetTheGood(sellItem))
            {
                GoodsStorage.RemoveItems(a => a == sellItem);
                change = CountChange(sellItem, DBalance);
            }
            else
            {
                change = CountChange(null, DBalance);
            }
            if (customer.GetCoins(change))
            {
                Wallet.RemoveItems(a => change.Contains(a));
                DBalance = 0;
                NewMessage("Thank you!");
                return(true);
            }
            return(false);
        }
 ////buy from participant2 what _goodsItem, for money
 public Transaction Buy(TradeParticipant participant2, GoodsItem buyItem, List <Coin> money)
 {
     return(TransactionPerform(this, participant2, money, buyItem, TransactionType.Buy));
 }
 //sell to participant2 what sellItem, for money
 public Transaction Sell(TradeParticipant participant2, GoodsItem sellItem, List <Coin> money)
 {
     return(TransactionPerform(this, participant2, money, sellItem, TransactionType.Sell));
 }