public TestCollection(int length) { // init collections collection_1Int = new List <string>(); collection_1TKey = new List <Goods>(); collection_2StringTValue = new SortedDictionary <string, Goods>(); collection_2TKeyTValue = new SortedDictionary <Goods, Goods>(); Length = length; for (int i = 0; i < Length; i++) { // generate for keys collections string tmpKey = "key number" + i; Random rnd = new Random(); int choice = rnd.Next(1, 4); if (choice == 1) { Toy toy = new Toy(); collection_1Int.Add(tmpKey); collection_1TKey.Add(new Goods() { Cost = i }); collection_2StringTValue.Add(tmpKey, toy); collection_2TKeyTValue.Add(new Goods() { Cost = i }, toy); } else if (choice == 2) { Product toy = new Product(); collection_1Int.Add(tmpKey); collection_1TKey.Add(new Goods() { Cost = i }); collection_2StringTValue.Add(tmpKey, toy); collection_2TKeyTValue.Add(new Goods() { Cost = i }, toy); } else { MilkProduct toy = new MilkProduct(); collection_1Int.Add(tmpKey); collection_1TKey.Add(new Goods() { Cost = i }); collection_2StringTValue.Add(tmpKey, toy); collection_2TKeyTValue.Add(new Goods() { Cost = i }, toy); } } }
public static void MenuTask3(TestCollection collection) { int userChoice; Console.Clear(); do { Console.WriteLine( "1. Добавить элемент \n2. Измерить время нахождения \n3. Вывести коллекцию \n4.Удалить элемент \n0. Выход в главное меню"); userChoice = InputNumber("", 0, 4); Console.Clear(); switch (userChoice) { case 1: string key; Console.WriteLine("Введите уникальный ключ: "); do { key = Console.ReadLine(); } while (collection.collection_1Int.Contains(key)); Console.WriteLine("Выберите тип продукта: \n1)Product \n2)Toy \n3)MilkProduct"); int choose = InputNumber("", 1, 3); switch (choose) { case 1: Product tmp = new Product(); collection.collection_1Int.Add(key); collection.collection_1TKey.Add(tmp.BaseGood); collection.collection_2StringTValue.Add(key, tmp); collection.collection_2TKeyTValue.Add(tmp.BaseGood, tmp); break; case 2: Toy tmp2 = new Toy(); collection.collection_1Int.Add(key); collection.collection_1TKey.Add(tmp2.BaseGood); collection.collection_2StringTValue.Add(key, tmp2); collection.collection_2TKeyTValue.Add(tmp2.BaseGood, tmp2); break; case 3: MilkProduct tmp3 = new MilkProduct(); collection.collection_1Int.Add(key); collection.collection_1TKey.Add(tmp3.BaseGood); collection.collection_2StringTValue.Add(key, tmp3); collection.collection_2TKeyTValue.Add(tmp3.BaseGood, tmp3); break; } break; case 2: Console.WriteLine( "Скорость нахождения первого, центрального, последнего и несуществующего элемента в коллекциях 1 типа:"); //first Console.WriteLine("Collection List<String>"); var first = collection.collection_1Int[0]; var mid = collection.collection_1Int[collection.collection_1Int.Count / 2]; var last = collection.collection_1Int[collection.collection_1Int.Count - 1]; var invalid = "djkfghskgneibnvdfvjeprv"; Stopwatch stopwatch = new Stopwatch(); TimeSpan timeSpan; stopwatch.Start(); var contains = collection.collection_1Int.Contains(first); contains = collection.collection_1Int.Contains(mid); contains = collection.collection_1Int.Contains(last); contains = collection.collection_1Int.Contains(invalid); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); //second Console.WriteLine("Collection List<Goods>"); var f = collection.collection_1TKey[0]; var m = collection.collection_1TKey[collection.collection_1TKey.Count / 2]; var l = collection.collection_1TKey[collection.collection_1TKey.Count - 1]; var inv = new Toy(); stopwatch = new Stopwatch(); stopwatch.Start(); contains = collection.collection_1TKey.Contains(f); contains = collection.collection_1TKey.Contains(m); contains = collection.collection_1TKey.Contains(l); contains = collection.collection_1TKey.Contains(inv); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); //third; key Console.WriteLine("Collection SortedDictionary<string, Goods>"); Console.WriteLine("Введите ключ"); var key2 = Console.ReadLine(); stopwatch = new Stopwatch(); stopwatch.Start(); contains = collection.collection_2StringTValue.ContainsKey(key2); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); //forth; key Console.WriteLine("Collection SortedDictionary<Goods, Goods>"); Goods g = new Goods(); stopwatch = new Stopwatch(); stopwatch.Start(); contains = collection.collection_2TKeyTValue.ContainsKey(g); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); //firth; value Console.WriteLine("Collection SortedDictionary<string, Goods>"); var toy = new Toy(); stopwatch = new Stopwatch(); stopwatch.Start(); contains = collection.collection_2StringTValue.ContainsValue(toy); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); //sixth Console.WriteLine("Collection SortedDictionary<Goods, Goods>"); stopwatch = new Stopwatch(); stopwatch.Start(); contains = collection.collection_2TKeyTValue.ContainsValue(toy); stopwatch.Stop(); timeSpan = stopwatch.Elapsed; Console.WriteLine("Время поиска:" + timeSpan); break; case 3: foreach (var keyPair in collection.collection_2StringTValue) { Console.WriteLine($"{keyPair.Key}) {keyPair.Value.ToString()}"); } break; case 4: Console.WriteLine("Введите уникальный ключ: "); do { key = Console.ReadLine(); } while (!collection.collection_1Int.Contains(key)); collection.collection_1Int.Remove(key); collection.collection_2StringTValue.Remove(key); break; } } while (userChoice != 0); }