mausha

Untitled

Jul 24th, 2017
301
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.13 KB | None | 0 0
  1. using System;
  2.  
  3. namespace GenericBox_EXER
  4. {
  5.     public class StartUp
  6.     {
  7.         private static void Main()
  8.         {
  9.             var interpreter = new CommandInterpreter(new Box<string>());
  10.             var input = Console.ReadLine().Split();
  11.  
  12.             while (input[0] != "END")
  13.             {
  14.                 interpreter.Interpred(input);
  15.  
  16.                 input = Console.ReadLine().Split();
  17.             }
  18.         }
  19.     }
  20. }
  21.  
  22. using System;
  23.  
  24. namespace GenericBox_EXER
  25. {
  26.     public class CommandInterpreter
  27.     {
  28.         private Box<string> box;
  29.  
  30.         public CommandInterpreter(Box<string> box)
  31.         {
  32.             this.box = box;
  33.         }
  34.  
  35.         public void Interpred(string[] input)
  36.         {
  37.             switch (input[0])
  38.             {
  39.                 case "Add":
  40.                     box.Add(input[1]);
  41.                     break;
  42.  
  43.                 case "Remove":
  44.                     Console.WriteLine(box.Remove(int.Parse(input[1])));
  45.                     break;
  46.  
  47.                 case "Contains":
  48.                     Console.WriteLine(box.Contains(input[1]));
  49.                     break;
  50.  
  51.                 case "Swap":
  52.                     box.SwapElements(int.Parse(input[1]), int.Parse(input[2]));
  53.                     break;
  54.  
  55.                 case "Greater":
  56.                     Console.WriteLine(box.CountGreaterEmelements(input[1]));
  57.                     break;
  58.  
  59.                 case "Max":
  60.                     Console.WriteLine(box.Max());
  61.                     break;
  62.  
  63.                 case "Min":
  64.                     Console.WriteLine(box.Min());
  65.                     break;
  66.  
  67.                 case "Sort":
  68.  
  69.                     break;
  70.  
  71.                 case "Print":
  72.                     Console.WriteLine(box.Print());
  73.                     break;
  74.             }
  75.         }
  76.     }
  77. }
  78.  
  79. using System;
  80. using System.Collections.Generic;
  81. using System.Linq;
  82. using System.Text;
  83.  
  84. namespace GenericBox_EXER
  85. {
  86.     public class Box<T> where T : IComparable<T>
  87.     {
  88.         private List<T> collection;
  89.  
  90.         public Box()
  91.         {
  92.             this.collection = new List<T>();
  93.         }
  94.  
  95.         public void Add(T element)
  96.         {
  97.             this.collection.Add(element);
  98.         }
  99.  
  100.         public T Remove(int index)
  101.         {
  102.             var removed = this.collection[index];
  103.             this.collection.RemoveAt(index);
  104.             return removed;
  105.         }
  106.  
  107.         public bool Contains(T element)
  108.         {
  109.             return this.collection.Contains(element);
  110.         }
  111.  
  112.         public void SwapElements(int first, int second)
  113.         {
  114.             var temp = this.collection[first];
  115.             this.collection[first] = this.collection[second];
  116.             this.collection[second] = temp;
  117.         }
  118.  
  119.         public int CountGreaterEmelements(T element)
  120.         {
  121.             return this.collection.Count(e => e.CompareTo(element) > 0);
  122.         }
  123.  
  124.         public T Max()
  125.         {
  126.             return this.collection.Max();
  127.         }
  128.  
  129.         public T Min()
  130.         {
  131.             return this.collection.Min();
  132.         }
  133.  
  134.         public string Print()
  135.         {
  136.             var result = new StringBuilder();
  137.             for (int k = 0; k < this.collection.Count; k++)
  138.             {
  139.                 result.AppendLine(this.collection[k].ToString());
  140.             }
  141.             return result.ToString().Trim();
  142.         }
  143.  
  144.         public List<T> Sort()
  145.         {
  146.             return new Sorter().Sort(this.collection);
  147.         }
  148.  
  149.         public override string ToString()
  150.         {
  151.             var result = new StringBuilder();
  152.             for (int j = 0; j < this.collection.Count; j++)
  153.             {
  154.                 result.AppendLine($"{this.collection[j].GetType().FullName}: {this.collection[j]}");
  155.             }
  156.             return result.ToString().Trim();
  157.         }
  158.     }
  159. }
  160.  
  161. using System;
  162. using System.Collections.Generic;
  163.  
  164. namespace GenericBox_EXER
  165. {
  166.     public class Sorter
  167.  
  168.     {
  169.         public List<T> Sort<T>(List<T> collection)
  170.             where T : IComparable<T>
  171.         {
  172.             collection.Sort();
  173.             return collection;
  174.         }
  175.     }
  176. }
Advertisement
Add Comment
Please, Sign In to add comment