コード例 #1
0
        /// <summary>
        /// Adds the given item to the container. An exception is thrown if
        /// an item with the same description is already in the container.
        /// </summary>
        public virtual void AddItem(BackPackItem item)
        {
            if (_items.ContainsKey(item.Description))
            {
                throw new ArgumentException(item.Description + " is already in the " + _containerDescription + "!");
            }

            _items.Add(item.Description, item);
        }
コード例 #2
0
ファイル: BackPack.cs プロジェクト: perl-easj/OOProg
        /// <summary>
        /// Extends the base version with a check for weight capacity
        /// </summary>
        public override void AddItem(BackPackItem item)
        {
            if (item.Weight > WeightCapacityLeft)
            {
                throw new ArgumentException("Oops, you over-stuffed the BackPack!!");
            }

            base.AddItem(item);
        }
コード例 #3
0
        public override void Solve(double capacityLeft)
        {
            string description = PickNextItemFromVault(capacityLeft);

            if (description != string.Empty)
            {
                BackPackItem item = TheVault.RemoveItem(description);
                TheBackPack.AddItem(item);
                Solve(TheBackPack.WeightCapacityLeft);
            }
        }
コード例 #4
0
        /// <summary>
        /// Removes and returns the item corresponding to the given description.
        /// An exception is thrown if no matching item is found.
        /// </summary>
        public virtual BackPackItem RemoveItem(string description)
        {
            if (!_items.ContainsKey(description))
            {
                throw new ArgumentException(description + " is not in the " + _containerDescription + "!");
            }

            BackPackItem removedItem = _items[description];

            _items.Remove(description);

            return(removedItem);
        }
コード例 #5
0
        private string PickNextItemFromVault(double capacityLeft)
        {
            double       bestRatio = 0;
            BackPackItem bestItem  = null;

            foreach (var item in TheVault.Items)
            {
                if (item.Weight <= capacityLeft)
                {
                    double ratio = item.Value / item.Weight;

                    if (ratio > bestRatio)
                    {
                        bestRatio = ratio;
                        bestItem  = item;
                    }
                }
            }

            return((bestItem != null) ? bestItem.Description : string.Empty);
        }