Advertisement
Savonarolla

Untitled

Jan 9th, 2021
654
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.45 KB | None | 0 0
  1. /*Делегаты
  2. Реализовать метод, который осуществляет поиск элемента в
  3. массиве. Метод должен принимать массив Object [] array, в котором
  4.     должен осуществляться поиск, и делегат, определяющий, является
  5.     ли элемент искомым.*/
  6.  
  7. using System;
  8. namespace Delegate
  9. {
  10.     public struct MyItem
  11.     {
  12.         public string Name;
  13.         public string Code;
  14.         public float Price;
  15.            
  16.         public MyItemType Type;
  17.         public enum MyItemType
  18.         {
  19.             Furniture,
  20.             Consumable, Clothes,
  21.             Mechanical, TrashItem
  22.         }
  23.         public MyItem(string Code, string Name, float Price, MyItem.MyItemType Type)
  24.         {
  25.             this.Code = Code;
  26.             this.Name = Name;
  27.             this.Price = Price;
  28.             this.Type = Type;
  29.         }
  30.         public override string ToString()
  31.         {
  32.             return string.Format("Type: " + Type + " \t  Name:" + Name + "\t ID:" + Code + " \t Price:" + Price + "$");
  33.         }
  34.     }
  35.    
  36.     class Program
  37.     {
  38.         static public void Find(Object[] array, Comparer comparer)
  39.         {
  40.             int i = 0;
  41.             bool flag = false;
  42.             foreach (var item in array)
  43.             {
  44.                 i++;
  45.                 if (comparer(array))
  46.                 {
  47.                     flag = true;
  48.                     Console.WriteLine("\n Result: index = {0}\n{1}", i, array[i]);
  49.                     Console.WriteLine(array[i].ToString());
  50.                 }
  51.             }
  52.             if (!flag)
  53.             {
  54.                 Console.WriteLine("Not Found!");
  55.                 throw new Exception("Invalid Argument");
  56.             }
  57.         }
  58.  
  59.         public delegate Boolean Comparer(Object elem1);
  60.  
  61.         static public Boolean CodeComparer(Object goods)
  62.         {
  63.             return ((MyItem)goods).Code == "01";
  64.         }
  65.  
  66.         static public Boolean TypeComparer(Object goods)
  67.         {
  68.             return ((MyItem)goods).Type == MyItem.MyItemType.TrashItem;
  69.         }
  70.  
  71.         static public Boolean NameComparer (Object goods)
  72.         {
  73.             return ((MyItem)goods).Name == "Arduino";
  74.         }
  75.  
  76.         static void Main(string[] args)
  77.         {
  78.             MyItem good1 = new MyItem("01", "Table", 22.9F, MyItem.MyItemType.Furniture);
  79.             MyItem good2 = new MyItem("02", "Chips", 12.5F, MyItem.MyItemType.Consumable);
  80.             MyItem good3 = new MyItem("03", "Arduino", 59.9F, MyItem.MyItemType.Mechanical);
  81.             MyItem good4 = new MyItem("04", "Jeans",9.99F, MyItem.MyItemType.Clothes);
  82.             MyItem good5 = new MyItem(null, null,0.0F, MyItem.MyItemType.TrashItem);
  83.  
  84.  
  85.             Object[] GoodsArray = { good1, good2, good3, good4, good5};
  86.            
  87.             uint i = 0;
  88.             foreach (Object goods in GoodsArray)
  89.             {
  90.                 Console.WriteLine("{0}.{1}", i, goods);
  91.                 i++;
  92.             }
  93.  
  94.             Console.WriteLine("\n\n\nFind Code of Article 01");
  95.             Find(GoodsArray, new Comparer(CodeComparer));
  96.  
  97.             Console.WriteLine("\n\n\nFind Type of Trash Article");
  98.             Find(GoodsArray, new Comparer(TypeComparer));
  99.  
  100.             Console.WriteLine("\n\n\nFind Name of Article Arduino");
  101.             Find(GoodsArray, new Comparer(NameComparer));
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement