Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- namespace GenericBox_EXER
- {
- public class StartUp
- {
- private static void Main()
- {
- var interpreter = new CommandInterpreter(new Box<string>());
- var input = Console.ReadLine().Split();
- while (input[0] != "END")
- {
- interpreter.Interpred(input);
- input = Console.ReadLine().Split();
- }
- }
- }
- }
- using System;
- namespace GenericBox_EXER
- {
- public class CommandInterpreter
- {
- private Box<string> box;
- public CommandInterpreter(Box<string> box)
- {
- this.box = box;
- }
- public void Interpred(string[] input)
- {
- switch (input[0])
- {
- case "Add":
- box.Add(input[1]);
- break;
- case "Remove":
- Console.WriteLine(box.Remove(int.Parse(input[1])));
- break;
- case "Contains":
- Console.WriteLine(box.Contains(input[1]));
- break;
- case "Swap":
- box.SwapElements(int.Parse(input[1]), int.Parse(input[2]));
- break;
- case "Greater":
- Console.WriteLine(box.CountGreaterEmelements(input[1]));
- break;
- case "Max":
- Console.WriteLine(box.Max());
- break;
- case "Min":
- Console.WriteLine(box.Min());
- break;
- case "Sort":
- break;
- case "Print":
- Console.WriteLine(box.Print());
- break;
- }
- }
- }
- }
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace GenericBox_EXER
- {
- public class Box<T> where T : IComparable<T>
- {
- private List<T> collection;
- public Box()
- {
- this.collection = new List<T>();
- }
- public void Add(T element)
- {
- this.collection.Add(element);
- }
- public T Remove(int index)
- {
- var removed = this.collection[index];
- this.collection.RemoveAt(index);
- return removed;
- }
- public bool Contains(T element)
- {
- return this.collection.Contains(element);
- }
- public void SwapElements(int first, int second)
- {
- var temp = this.collection[first];
- this.collection[first] = this.collection[second];
- this.collection[second] = temp;
- }
- public int CountGreaterEmelements(T element)
- {
- return this.collection.Count(e => e.CompareTo(element) > 0);
- }
- public T Max()
- {
- return this.collection.Max();
- }
- public T Min()
- {
- return this.collection.Min();
- }
- public string Print()
- {
- var result = new StringBuilder();
- for (int k = 0; k < this.collection.Count; k++)
- {
- result.AppendLine(this.collection[k].ToString());
- }
- return result.ToString().Trim();
- }
- public List<T> Sort()
- {
- return new Sorter().Sort(this.collection);
- }
- public override string ToString()
- {
- var result = new StringBuilder();
- for (int j = 0; j < this.collection.Count; j++)
- {
- result.AppendLine($"{this.collection[j].GetType().FullName}: {this.collection[j]}");
- }
- return result.ToString().Trim();
- }
- }
- }
- using System;
- using System.Collections.Generic;
- namespace GenericBox_EXER
- {
- public class Sorter
- {
- public List<T> Sort<T>(List<T> collection)
- where T : IComparable<T>
- {
- collection.Sort();
- return collection;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment