Advertisement
desislava_topuzakova

Fridge

Jun 21st, 2020
1,263
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.87 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Text;
  4.  
  5. namespace ExamPreparation
  6. {
  7.     class Fridge
  8.     {
  9.         private List<Product> products;
  10.         private int count;
  11.  
  12.         public Fridge()
  13.         {
  14.             count = 0;
  15.             products = new List<Product>();
  16.         }
  17.  
  18.         public int Count
  19.         {
  20.             get
  21.             {
  22.                 return count;
  23.             }
  24.             set
  25.             {
  26.                 count = value;
  27.             }
  28.         }
  29.  
  30.         public void AddProduct(string productName)
  31.         {
  32.             //1. създадем продукт
  33.             Product productToAdd = new Product(productName);
  34.             //2. добавим
  35.             products.Add(productToAdd);
  36.         }
  37.  
  38.         public string[] CookMeal(int start, int end)
  39.         {
  40.             //[a, b, c, d, e, f, j]
  41.             List<string> foundProducts = new List<string>();
  42.             if (end > products.Count - 1)
  43.             {
  44.                 end = products.Count - 1;
  45.             }
  46.  
  47.             for (int index = start; index <= end; index++)
  48.             {
  49.                 foundProducts.Add(products[index].Name);
  50.  
  51.             }
  52.  
  53.             return foundProducts.ToArray();
  54.  
  55.         }
  56.  
  57.         public string RemoveProductByIndex(int index)
  58.         {
  59.             //1. взимаме продукта от индекса
  60.             Product productToRemove = products[index];
  61.             //2. премахваме го
  62.             if (products.Remove(productToRemove))
  63.             {
  64.                 return productToRemove.Name;
  65.             }
  66.             else
  67.             {
  68.                 return null;
  69.             }
  70.         }
  71.  
  72.         public string RemoveProductByName(string name)
  73.         {
  74.             for (int i = 0; i < products.Count; i++)
  75.             {
  76.                 if (name == products[i].Name)
  77.                 {
  78.                     if (products.Remove(products[i]))
  79.                     {
  80.                         return name;
  81.                     }
  82.                     else
  83.                     {
  84.                         //няма го продукта
  85.                         return null;
  86.                     }
  87.                 }
  88.             }
  89.             return null;
  90.  
  91.         }
  92.  
  93.         public bool CheckProductIsInStock(string name)
  94.         {
  95.             foreach (Product product in products)
  96.             {
  97.                 if (product.Name == name)
  98.                 {
  99.                     return true;
  100.                 }
  101.             }
  102.             return false;
  103.         }
  104.  
  105.         public string[] ProvideInformationAboutAllProducts()
  106.         {
  107.             List<string> info = new List<string>();
  108.             foreach (Product product in products)
  109.             {
  110.                 info.Add(product.ToString());
  111.             }
  112.  
  113.             return info.ToArray();
  114.         }
  115.  
  116.     }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement