desislava_topuzakova

Program.cs -> PRODUCTS

Jun 21st, 2020
238
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.11 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace Exam
  8. {
  9.     class Program
  10.     {
  11.         static Fridge fridge = new Fridge();
  12.  
  13.         static void Main(string[] args)
  14.         {
  15.             string line;
  16.  
  17.             while ("END" != (line = Console.ReadLine()))
  18.             {
  19.                 string[] cmdArgs = line.Split(' ');
  20.  
  21.                 switch (cmdArgs[0])
  22.                 {
  23.                     case "Add":
  24.                         AddProduct(cmdArgs[1]);
  25.                         break;
  26.                     case "Check":
  27.                         CheckProductIsInStock(cmdArgs[1]);
  28.                         break;
  29.                     case "Remove":
  30.                         try
  31.                         {
  32.                             int index = int.Parse(cmdArgs[1]);
  33.                             RemoveProductByIndex(index);
  34.                         }
  35.                         catch (FormatException e)
  36.                         {
  37.                             RemoveProductByName(cmdArgs[1]);
  38.                         }
  39.                         break;
  40.                     case "Print":
  41.                         ProvideInformationAboutAllProducts();
  42.                         break;
  43.                     case "Cook":
  44.                         CookMeal(cmdArgs.Skip(1).ToArray());
  45.                         break;
  46.                 }
  47.             }
  48.         }
  49.  
  50.         private static void CookMeal(string[] indexes)
  51.         {
  52.             string[] products = fridge.CookMeal(int.Parse(indexes[0]), int.Parse(indexes[1]));
  53.             Console.WriteLine("Meal cooked. Used Products: " + string.Join(", ", products));
  54.         }
  55.  
  56.         private static void ProvideInformationAboutAllProducts()
  57.         {
  58.             string[] info = fridge.ProvideInformationAboutAllProducts();
  59.             foreach (var item in info)
  60.             {
  61.                 Console.WriteLine(item);
  62.             }
  63.         }
  64.  
  65.         private static void RemoveProductByName(string name)
  66.         {
  67.             string ProductName = fridge.RemoveProductByName(name);
  68.             if (ProductName != null)
  69.             {
  70.                 Console.WriteLine("Removed: " + ProductName);
  71.             }
  72.             else
  73.             {
  74.                 Console.WriteLine("Product not found!");
  75.             }
  76.         }
  77.  
  78.         private static void RemoveProductByIndex(int index)
  79.         {
  80.             string ProductName = fridge.RemoveProductByIndex(index);
  81.             if (ProductName != null)
  82.             {
  83.                 Console.WriteLine("Removed: " + ProductName);
  84.             }
  85.             else
  86.             {
  87.                 Console.WriteLine("Product not found!");
  88.             }
  89.         }
  90.  
  91.         private static void CheckProductIsInStock(string name)
  92.         {
  93.             bool isInStock = fridge.CheckProductIsInStock(name);
  94.  
  95.             Console.WriteLine(isInStock ? $"Product {name} is in stock."
  96.                 : "Not in stock");
  97.         }
  98.  
  99.         private static void AddProduct(string name)
  100.         {
  101.             fridge.AddProduct(name);
  102.         }
  103.     }
  104. }
Add Comment
Please, Sign In to add comment