public void BaseBuyTest()
        {
            TradeParticipant customer = new TradeParticipant("customer");
            Coin coin = new Coin(10);
            Coin coin1 = new Coin(10);
            Coin coin2 = new Coin(10);
            Coin coin3 = new Coin(5);
            Coin coin4 = new Coin(3);
            Coin coin5 = new Coin(2);
            Coin coin6 = new Coin(1);

            customer.Wallet.AddItems(new List<Coin>{coin,coin1,coin2,coin3,coin4,coin5,coin6});

            TradeParticipant vmachine = new TradeParticipant("vmachine");
            GoodsItem item = new GoodsItem(10, "coffee");
            GoodsItem item1 = new GoodsItem(10, "coffee");
            GoodsItem item2= new GoodsItem(10, "coffee");
            GoodsItem item3 = new GoodsItem(10, "coffee");
            GoodsItem item4 = new GoodsItem(7, "green tea");
            GoodsItem item5 = new GoodsItem(3, "tea");
            GoodsItem item6 = new GoodsItem(3, "tea");
            GoodsItem item7 = new GoodsItem(1, "napkin");
            GoodsItem item8 = new GoodsItem(1, "napkin");

            vmachine.GoodsStorage.AddItems(new List<GoodsItem> 
            { item, item1, item2, item3, item4, item5, item6, item7, item8, item });

            
            Coin coin0 = new Coin(10);
            Coin coin01 = new Coin(10);
            Coin coin02 = new Coin(10);
            Coin coin03 = new Coin(5);
            Coin coin04 = new Coin(3);
            Coin coin05 = new Coin(2);
            Coin coin06 = new Coin(1);

            vmachine.Wallet.AddItems(new List<Coin> { coin0, coin01, coin02, coin03, coin04, coin05, coin06 });

            var cmoney_amount = customer.Wallet.MoneyAmount;
            var cgoods_amount = customer.GoodsStorage.GoodsCount;

            var vmoney_amount = vmachine.Wallet.MoneyAmount;
            var vgoods_count = vmachine.GoodsStorage.GoodsCount;

            customer.Buy(vmachine, item4, new List<Coin>() { coin3,coin4 }); //8-1 coin06 in

            Assert.AreEqual(cmoney_amount - 7, customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(vgoods_count - 1, vmachine.GoodsStorage.GoodsCount, "vm goods storage");
            Assert.AreEqual(vmoney_amount + 7, vmachine.Wallet.MoneyAmount, "vm wallet");

            customer.Buy(vmachine, item5, new List<Coin>() { coin5,coin6 });// 0 
            customer.Buy(vmachine, item1, new List<Coin>() { coin1 });//
            customer.Sell(vmachine, item4, new List<Coin>() { coin05,coin03 });//

            Assert.AreEqual(cmoney_amount-7-3-10+7,
                customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(2, customer.GoodsStorage.GoodsCount, "customer goods storage");

            
        }
 private bool CheckWhoesMoneyIs(TradeParticipant from_participant, List <Coin> _money)
 {
     foreach (var coin in _money)
     {
         if (from_participant.Wallet.GetItem(a => a == coin) == null)
         {
             return(false);
         }
     }
     return(true);
 }
 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;
 }
        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;
        }
        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);
        }
        public void PrimaryBuyTest()
        {
            TradeParticipant customer = new TradeParticipant("customer");
            Coin coin = new Coin(10);
            customer.Wallet.AddItem(coin);

            TradeParticipant vmachine = new TradeParticipant("vmachine");
            GoodsItem item = new GoodsItem(10,"coffee");
            vmachine.GoodsStorage.AddItem(item);

            customer.Buy(vmachine, item, new List<Coin>() { coin });

            
            Assert.AreEqual(0,customer.Wallet.MoneyAmount,"customer wallet");
            Assert.AreEqual(1,customer.GoodsStorage.GoodsCount, "customer goods storage");
            Assert.AreEqual(10,vmachine.Wallet.MoneyAmount, "vm wallet");
            Assert.AreEqual(0,vmachine.GoodsStorage.GoodsCount, "vm goods storage");
        }
 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;
 }
 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;
 }
        private List <Coin> CountTheChange(decimal changeAmount, TradeParticipant participant,
                                           List <Coin> additionalList = null)
        {
            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 = participant.Wallet.GetMaxCoinUnderCondition(a => changeCondition(a));

                if (additionalList != null)
                {
                    var _coin1 = additionalList.Where(a => changeCondition(a)).Max();
                    if (_coin1 != null && (_coin == null || _coin1.Value > _coin.Value))
                    {
                        _coin = _coin1;
                    }
                }

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

            if (changeAmount == 0)
            {
                return(_coins);
            }
            return(null);
        }
 private bool _EnrollMoney(TradeParticipant to_participant, TradeParticipant from_participant, List <Coin> _money)
 {
     try
     {
         if (CheckWhoesMoneyIs(from_participant, _money))
         {
             to_participant.Wallet.AddItems(_money);
             from_participant.Wallet.RemoveItems(a => _money.Contains(a));
             return(true);
         }
         else
         {
             AddMessage("Sorry, cannot sell this is not your money");
             return(false);
         }
     }catch (Exception ex)
     {
         AddMessage("Can't enroll money");
         AddMessage(AddExceptionInfo(ex));
         return(false);
     }
 }
Exemple #12
0
        public void ReturnChangeTest3()
        {
            TradeParticipant customer = new TradeParticipant("customer");
            Coin coin = new Coin(3);
            Coin coin1 = new Coin(5);
            Coin coin2 = new Coin(2);

            customer.Wallet.AddItem(coin);
            customer.Wallet.AddItem(coin1);
            customer.Wallet.AddItem(coin2);

            TradeParticipant vmachine = new TradeParticipant("vmachine");
            GoodsItem item = new GoodsItem(10, "coffee");
            vmachine.GoodsStorage.AddItem(item);

            customer.Buy(vmachine, null, new List<Coin>() { coin, coin1,coin2 });

            Assert.AreEqual(10, customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(3, customer.Wallet.GetItems(a => a.Value >0, -1).Count, "customer wallet");

            Coin coin3 = new Coin(10);
            vmachine.Wallet.AddItem(coin3);

            customer.Buy(vmachine, null, new List<Coin>() { coin, coin1, coin2 });

            Assert.AreEqual(10, customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(1, customer.Wallet.GetItems(a => a.Value == 10, -1).Count, "customer wallet");

            Assert.AreEqual(10, vmachine.Wallet.MoneyAmount, "vm wallet");
            Assert.AreEqual(3, vmachine.Wallet.GetItems(a => a.Value > 0, -1).Count, "vm wallet");
        }
 //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);
 }
 ////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);
 }
        private List<Coin> CountTheChange(decimal changeAmount,TradeParticipant participant,
            List<Coin> additionalList=null)
        {
            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 = participant.Wallet.GetMaxCoinUnderCondition(a=>changeCondition(a));

                if (additionalList != null)
                {
                    var _coin1 = additionalList.Where(a => changeCondition(a)).Max();
                    if (_coin1!=null && (_coin==null || _coin1.Value > _coin.Value)) { _coin = _coin1; }
                }

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

            if (changeAmount == 0)
            {
                return _coins;
            }
            return null;
        }
 //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));
 }
 ////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));
 }
 private bool CheckWhoesMoneyIs(TradeParticipant from_participant, List<Coin> _money)
 {
     foreach(var coin in _money)
     {
         if (from_participant.Wallet.GetItem(a => a == coin) == null)
         {
             return false;
         }
     }
     return true;
 }
 private bool _EnrollMoney(TradeParticipant to_participant,TradeParticipant from_participant,List<Coin> _money)
 {
     try
     {
         if (CheckWhoesMoneyIs(from_participant, _money))
         {
             to_participant.Wallet.AddItems(_money);
             from_participant.Wallet.RemoveItems(a => _money.Contains(a));
             return true;
         }
         else
         {
             AddMessage("Sorry, cannot sell this is not your money");
             return false;
         }
     }catch(Exception ex)
     {
         AddMessage("Can't enroll money");
         AddMessage(AddExceptionInfo(ex));
         return false;
     }
 }
Exemple #20
0
        public void ReturnChangeTest2()
        {
            TradeParticipant customer = new TradeParticipant("customer");
            Coin coin = new Coin(10);
            Coin coin1 = new Coin(5);
            customer.Wallet.AddItem(coin);
            customer.Wallet.AddItem(coin1);

            TradeParticipant vmachine = new TradeParticipant("vmachine");
            GoodsItem item = new GoodsItem(10, "coffee");
            vmachine.GoodsStorage.AddItem(item);

            customer.Buy(vmachine, null, new List<Coin>() { coin, coin1 });

            Assert.AreEqual(15, customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(0, customer.GoodsStorage.GoodsCount, "customer goods storage");
            Assert.AreEqual(0, vmachine.Wallet.MoneyAmount, "vm wallet");
            Assert.AreEqual(1, vmachine.GoodsStorage.GoodsCount, "vm goods storage");
        }
Exemple #21
0
        public void DoesHaveEnoughChangeTest()
        {
            TradeParticipant customer = new TradeParticipant("customer");
            Coin coin = new Coin(10);
            Coin coin1 = new Coin(5);
            customer.Wallet.AddItem(coin);
            customer.Wallet.AddItem(coin1);

            TradeParticipant vmachine = new TradeParticipant("vmachine");
            GoodsItem item = new GoodsItem(7, "green tea");
            vmachine.GoodsStorage.AddItem(item);

            customer.Buy(vmachine, item, new List<Coin>() { coin, coin1 });

            Assert.AreEqual(15, customer.Wallet.MoneyAmount, "customer wallet");
            Assert.AreEqual(0, customer.GoodsStorage.GoodsCount, "customer goods storage");
            Assert.AreEqual(0, vmachine.Wallet.MoneyAmount, "vm wallet");
            Assert.AreEqual(1, vmachine.GoodsStorage.GoodsCount, "vm goods storage");
        }