Ejemplo n.º 1
0
        // Constructor with a parameter.
        public TestCollections(int capacity)
        {
            ListState        = new List <State>(capacity);
            ListString       = new List <string>(capacity);
            DictionaryState  = new Dictionary <State, Monarchy>(capacity);
            DictionaryString = new Dictionary <string, Monarchy>(capacity);

            for (int i = 0; i < capacity; i++)
            {
                // Random monarchy object.
                Monarchy bufMon = Generate(i);

                // State object with the same fields.
                State bufState = bufMon.BaseState;

                // Adding the state object to the List<TKey>.
                ListState.Add(bufState);

                // Adding the state and the monarchy object to the Dictionary<TKey, TValue>.
                DictionaryState.Add(bufState, bufMon);

                // Adding the state's string to the List<string>.
                ListString.Add(bufState.ToString());

                // Adding the state's string and the monarchy object to the Dictionary<string, TValue>.
                DictionaryString.Add(bufState.ToString(), bufMon);
            }
        }
Ejemplo n.º 2
0
        // Method to search a monarchy object in Dictionary<TKey, TValue>.
        public static long MonarchyGetTicks(TestCollections testCollections, Monarchy searchThis)
        {
            // A StopWatch object to count milliseconds.
            Stopwatch sw = new Stopwatch();

            // Counting the search time.
            sw.Start();
            testCollections.DictionaryState.ContainsValue(searchThis);
            sw.Stop();

            return(sw.ElapsedTicks);
        }
Ejemplo n.º 3
0
 // Method to add an element to the collections.
 public void Add(Monarchy monarchy)
 {
     try
     {
         ListState.Add(monarchy.BaseState);
         ListString.Add(monarchy.BaseState.ToString());
         DictionaryState.Add(monarchy.BaseState, monarchy);
         DictionaryString.Add(monarchy.BaseState.ToString(), monarchy);
     }
     catch (Exception e)
     {
         Console.WriteLine(e.Message);
     }
 }
Ejemplo n.º 4
0
        // Function to input a monarchy object.
        public static Monarchy ObjectInput()
        {
            Monarchy monarchy = new Monarchy();

            // Name input.
            Console.Write("Enter the name: ");
            monarchy.Name = Console.ReadLine();

            // Leader name input.
            Console.Write("Enter the leader's name: ");
            monarchy.LeaderName = Console.ReadLine();

            // Population input.
            bool ok;
            int  buf;

            do
            {
                Console.Write("Enter the population: ");
                ok = Int32.TryParse(Console.ReadLine(), out buf);
                if (!ok || buf < 0)
                {
                    Console.WriteLine("Input error! Perhaps you didn't enter a natural number");
                }
            } while (!ok || buf < 0);

            monarchy.Population = buf;

            // Age input.
            do
            {
                Console.Write("Enter the age: ");
                ok = Int32.TryParse(Console.ReadLine(), out buf);
                if (!ok || buf < 0)
                {
                    Console.WriteLine("Input error! Perhaps you didn't enter a natural number");
                }
            } while (!ok || buf < 0);

            monarchy.Age = buf;

            // Continent input.
            monarchy.Continent = ContinentsInput();

            Console.Write("Enter the current rulling clan's name: ");
            monarchy.CurrentRullingClanName = Console.ReadLine();

            return(monarchy);
        }
Ejemplo n.º 5
0
        // Method to add an element.
        public static void AddElem(ref TestCollections testCollections)
        {
            Console.Clear();

            Console.WriteLine("Enter the adding object:");
            Monarchy monarchy = ObjectInput();

            testCollections.Add(monarchy);

            Console.WriteLine("The element is successfully added!");


            Console.WriteLine("Press ENTER to go back");
            Console.ReadLine();
        }
Ejemplo n.º 6
0
        public static Monarchy Generate(int a)
        {
            Monarchy monarchy = new Monarchy();

            string[] continents = { "Asia", "Africa", "America", "Oceania", "Europe" };

            // Creating the element.
            for (int i = 0; i < rnd.Next(4, 20); i++)
            {
                monarchy.Name                   += (char)rnd.Next(65, 91);
                monarchy.LeaderName             += (char)rnd.Next(97, 123);
                monarchy.Age                    += rnd.Next(0, 150);
                monarchy.Population             += rnd.Next(0, 1000);
                monarchy.CurrentRullingClanName += (char)rnd.Next(65, 91);
            }

            monarchy.Continent = continents[rnd.Next(0, 5)];

            return(monarchy);
        }
Ejemplo n.º 7
0
        public static void GetTime(TestCollections testCollections)
        {
            // Array of state objects that are stored in collections.
            State[] stateArr = testCollections.ListState.ToArray();

            // Array of monarchy objects that are stored in collections.
            Monarchy[] monArr = testCollections.DictionaryState.Values.ToArray();

            // The monarchy object that doesn't exist in the collections.
            Monarchy alienMon = new Monarchy("Jamaica", "Elizabeth II", 43847430, 56, "America", "House of Windsor");


            // The first state object.
            State firstState = new State(stateArr[0].Name, stateArr[0].LeaderName, stateArr[0].Population, stateArr[0].Age, stateArr[0].Continent);

            // The middle state object.
            State middleState = new State(stateArr[stateArr.Length / 2].Name, stateArr[stateArr.Length / 2].LeaderName,
                                          stateArr[stateArr.Length / 2].Population, stateArr[stateArr.Length / 2].Age, stateArr[stateArr.Length / 2].Continent);

            // The last state object.
            State lastState = new State(stateArr[stateArr.Length - 1].Name, stateArr[stateArr.Length - 1].LeaderName,
                                        stateArr[stateArr.Length - 1].Population, stateArr[stateArr.Length - 1].Age, stateArr[stateArr.Length - 1].Continent);

            // The state object that doesn't exist in the collections.
            State alienState = alienMon.BaseState;


            // The first monarchy object.
            Monarchy firstMon = new Monarchy(monArr[0].Name, monArr[0].LeaderName, monArr[0].Population, monArr[0].Age,
                                             monArr[0].Continent, monArr[0].CurrentRullingClanName);

            // The middle monarchy object.
            Monarchy middleMon = new Monarchy(monArr[monArr.Length / 2].Name, monArr[monArr.Length / 2].LeaderName, monArr[monArr.Length / 2].Population,
                                              monArr[monArr.Length / 2].Age, monArr[monArr.Length / 2].Continent, monArr[monArr.Length / 2].CurrentRullingClanName);

            // The last monarchy object.
            Monarchy lastMon = new Monarchy(monArr[monArr.Length - 1].Name, monArr[monArr.Length - 1].LeaderName, monArr[monArr.Length - 1].Population,
                                            monArr[monArr.Length - 1].Age, monArr[monArr.Length - 1].Continent, monArr[monArr.Length - 1].CurrentRullingClanName);


            // Stores all the search times of the 1st object.
            long[] stateSearch = StateGetTicks(testCollections, firstState);
            long[] firstSearch = { stateSearch[0], stateSearch[1],
                                   stateSearch[2], stateSearch[3],MonarchyGetTicks(testCollections, firstMon) };

            // Stores all the search times of the middle object.
            stateSearch = StateGetTicks(testCollections, middleState);
            long[] middleSearch = { stateSearch[0], stateSearch[1],
                                    stateSearch[2], stateSearch[3],MonarchyGetTicks(testCollections, middleMon) };

            // Stores all the search times of the last object.
            stateSearch = StateGetTicks(testCollections, lastState);
            long[] lastSearch = { stateSearch[0], stateSearch[1],
                                  stateSearch[2], stateSearch[3],MonarchyGetTicks(testCollections, lastMon) };

            // Stores all the search times of the alien object.
            stateSearch = StateGetTicks(testCollections, alienState);
            long[] alienSearch = { stateSearch[0], stateSearch[1],
                                   stateSearch[2], stateSearch[3],MonarchyGetTicks(testCollections, alienMon) };

            // Showing the objects.
            Console.Clear();
            Console.WriteLine("The first object: ");
            firstState.Show();
            Console.WriteLine("The middle object: ");
            middleState.Show();
            Console.WriteLine("The last object: ");
            lastState.Show();
            Console.WriteLine("The alien object: ");
            alienState.Show();

            Console.WriteLine("Time to search for the first element:\n In List<TKey>: {0}\n In List<string>: {1}\n " +
                              "In Dictionary<TKey, TValue> (key): {2}\n In Dictionary<string, TValue> (key): {3}\n In In Dictionary<TKey, TValue> (value) : {4}\n",
                              firstSearch[0], firstSearch[1], firstSearch[2], firstSearch[3], firstSearch[4]);

            Console.WriteLine("Time to search for the middle element:\n In List<TKey>: {0}\n In List<string>: {1}\n " +
                              "In Dictionary<TKey, TValue> (key): {2}\n In Dictionary<string, TValue> (key): {3}\n In In Dictionary<TKey, TValue> (value) : {4}\n",
                              middleSearch[0], middleSearch[1], middleSearch[2], middleSearch[3], middleSearch[4]);

            Console.WriteLine("Time to search for the last element:\n In List<TKey>: {0}\n In List<string>: {1}\n " +
                              "In Dictionary<TKey, TValue> (key): {2}\n In Dictionary<string, TValue> (key): {3}\n In In Dictionary<TKey, TValue> (value) : {4}\n",
                              lastSearch[0], lastSearch[1], lastSearch[2], lastSearch[3], lastSearch[4]);

            Console.WriteLine("Time to search for the alien element:\n In List<TKey>: {0}\n In List<string>: {1}\n " +
                              "In Dictionary<TKey, TValue> (key): {2}\n In Dictionary<string, TValue> (key): {3}\n In In Dictionary<TKey, TValue> (value) : {4}\n",
                              alienSearch[0], alienSearch[1], alienSearch[2], alienSearch[3], alienSearch[4]);

            Console.WriteLine("Press ENTER to go back");
            Console.ReadLine();
        }