示例#1
0
 private static Backpack BackPackAction(ConsoleKeyInfo key, Backpack backPack)
 {
     try
     {
         Console.WriteLine();
         if (key.Key == ConsoleKey.A)
         {
             string name;
             string weight;
             string size;
             int    weightValue;
             int    sizeValue;
             Console.WriteLine("To add an item, enter the following: \"item Name, item Weight, item Size \"");
             Console.Write("Item Name: ");
             name = Console.ReadLine();
             Console.Write("Item Weight: ");
             weight = Console.ReadLine();
             bool isWeightNumeric = int.TryParse(weight, out weightValue);
             Console.Write("Item Size: ");
             size = Console.ReadLine();
             bool isSizeNumeric = int.TryParse(size, out sizeValue);
             if (isWeightNumeric && isSizeNumeric)
             {
                 backPack.AddNewItem(name, weightValue, sizeValue);
             }
             else
             {
                 Console.WriteLine("Invalid input, weight and size should be numeric");
             }
         }
         else if (key.Key == ConsoleKey.D)
         {
             Console.WriteLine("Your backpack contains the following:");
             backPack.Display();
         }
     }
     catch (Exception e)
     {
         Console.WriteLine("Invalid Input entered: '{0}'", e);
     }
     return(backPack);
 }