コード例 #1
0
 private static void InsertItem(string type, long amount, Bag bag)
 {
     if (type.Length == 3)
     {
         Cash cash = new Cash(type, amount);
         bag.AddCash(cash);
     }
     else if (type.Length >= 4 && type.ToLower().EndsWith("gem"))
     {
         Gem gem = new Gem(type, amount);
         bag.AddGem(gem);
     }
     else if (type.ToLower().Equals("gold"))
     {
         Gold gold = new Gold(type, amount);
         bag.AddGold(gold);
     }
 }
コード例 #2
0
 private void AddItem(string name, long quantity)
 {
     if (quantity > bag.Capacity)
     {
         return;
     }
     else if (name.Length == 3)
     {
         bag.AddCash(new Cash(name, quantity));
     }
     else if (name.ToLower() == "gold")
     {
         bag.AddGold(quantity);
     }
     else if (name.ToLower().EndsWith("gem"))
     {
         bag.AddGem(new Gem(name, quantity));
     }
 }
コード例 #3
0
ファイル: Program.cs プロジェクト: radomirKrastev/OOP
        static void Main()
        {
            var bagLimit = long.Parse(Console.ReadLine());
            var vault    = Console.ReadLine().Split(new char[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            var newBag = new Bag();

            for (int i = 0; i < vault.Length; i += 2)
            {
                var treasureName = vault[i];
                var category     = GetTreasureCategory(treasureName);

                var treasuryValue = long.Parse(vault[i + 1]);
                var bagIsFull     = ValidateValue(treasuryValue, bagLimit, newBag);

                if (bagIsFull)
                {
                    continue;
                }

                switch (category)
                {
                case "Gem":
                    newBag.AddGem(treasureName, treasuryValue);
                    break;

                case "Cash":
                    newBag.AddCash(treasureName, treasuryValue);
                    break;

                case "Gold":
                    newBag.AddGold(treasuryValue);
                    break;
                }
            }

            Console.Write(newBag.ToString());
        }