コード例 #1
0
        /// <summary>
        /// Removes an item from inventory, subtracts cost from user balance, and logs the transaction.
        /// </summary>
        /// <param name="userSelection">Slot number</param>
        public bool BuyItem(string userSelection)
        {
            if (Inventory[userSelection].Count == 0)       //  PREVENT THE USER FROM GETTING AN ERROR
            {
                return(false);
            }

            //IDENTIFY ITEM TO BUY
            var itemToBuy = Inventory[userSelection][0];

            if (itemToBuy.Price > Balance)                        //  "                                       "
            {
                return(false);
            }

            else if (!Inventory.ContainsKey(userSelection))      //  "                                       "
            {
                return(false);
            }

            //ADD TO LIST OF ITEMS BOUGHT
            this.ItemsBought.Add(itemToBuy);
            //REMOVE FROM INVENTORY
            this.Inventory[userSelection].Remove(itemToBuy);
            //REMOVE COST OF ITEM FROM MONEY USER INSERTED
            Balance -= itemToBuy.Price;
            //IF USER HAS BOUGHT THE LAST ITEM, REMOVE FROM INVENTORY DICTIONARY
            if (Inventory[userSelection].Count == 0)
            {
                Inventory.Remove(userSelection);
            }
            //CREATE A TRANSACTION OBJECT
            Transaction itemSold = new Transaction(itemToBuy);

            //ADD TRANSACTION TO LOG
            FW.AddLog(itemSold);
            this.TransactionLog.Add(itemSold);
            //return true;
            return(true);
        }