public static AbstrState RandomState() { Monarchy monarchy = new Monarchy(); // 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.Continent += (char)rnd.Next(97, 123); monarchy.CurrentRullingClanName += (char)rnd.Next(65, 91); } return(monarchy); }
// Function to input a monarchy object. public static AbstrState 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(); return(monarchy); }
// Function to add element to the stack. public static void AddElem(ref Stack <AbstrState> stack) { Console.Clear(); // Objects that will be added. Republic republicCongo = new Republic("The Republic of the Congo", "Denis Sassou Nguesso", 5125821, 58, "Africa", 7, 72, 6); Monarchy monarchyBelgium = new Monarchy("Belgium", "Philippe", 11358357, 189, "Europe", "The House of Saxe-Coburg and Gotha"); Kingdom kingdomSaudiArabia = new Kingdom("The Kingdom of Saudi Arabia", "Salman", 33000000, 87, "Asia", "The Sudairi Seven"); Console.Clear(); Console.WriteLine(@"Choose the class of the adding object 1. Republic. 2. Monarchy. 3. Kingdom. 4. Back."); // Getting the class of the adding object. int choice = Program.ChoiceInput(4); switch (choice) { case 1: stack.Push(republicCongo); Console.WriteLine("The Republic of the Congo has been successfully added to the stack!\nPress ENTER to go back."); Console.ReadLine(); break; case 2: stack.Push(monarchyBelgium); Console.WriteLine("Belgium has been successfully added to the stack!\nPress ENTER to go back."); Console.ReadLine(); break; case 3: stack.Push(kingdomSaudiArabia); Console.WriteLine("The Kingdom of Saudi Arabia has been successfully added to the stack!\nPress ENTER to go back."); Console.ReadLine(); break; case 4: break; } }
// Function for binary search. public static void BinarySearchArrL(ArrayList arrayList) { Console.Clear(); Console.WriteLine(@"By what field you want to search: 1. Name. 2. Leader's name. 3. Population. 4. Age."); int choice = Program.ChoiceInput(4); int index = -1; switch (choice) { // Using special classes to compare by different fields. // Search by name. case 1: Console.Write("Enter the name: "); string name = Console.ReadLine(); // Just a buffer variable to compare with. Monarchy buffer = new Monarchy(name, "", 0, 0, "", ""); index = arrayList.BinarySearch(buffer, new CompareName()); break; // Search by leader's name. case 2: Console.Write("Enter the leader's name: "); string leaderName = Console.ReadLine(); buffer = new Monarchy("", leaderName, 0, 0, "", ""); index = arrayList.BinarySearch(buffer, new CompareLeaderName()); break; // Search by population. case 3: int population = InputPopulation(); buffer = new Monarchy("", "", population, 0, "", ""); index = arrayList.BinarySearch(buffer, new ComparePopulation()); break; // Search by age. case 4: int age = InputAge(); buffer = new Monarchy("", "", 0, age, "", ""); index = arrayList.BinarySearch(buffer, new CompareAge()); break; } Console.WriteLine(); if (index < 0) { Console.WriteLine("The element doesn't exist in the arraylist\nPerhaps you didn't sort the arraylist before the search."); } else { Console.WriteLine("The index of the element is {0}", index); (arrayList[index] as AbstrState).Show(); } Console.WriteLine("Press ENTER to go back"); Console.ReadLine(); }
public static void Demostrate() { // Republic class. //Republic republicCongo = new Republic("The Republic of the Congo", "Denis Sassou Nguesso", 5125821, 58, "Africa", 7, 72, 6); Republic republicCzech = new Republic("The Czech Republic", "Milos Zeman", 10610947, 25, "Europe", 10, 200, 4); Republic republicTurkey = new Republic("The Republic of Turkey", "Recep Tayyip Erdogan", 80810525, 95, "Asia", 5, 550, 4); Republic republicArgentina = new Republic("The Argentine Republic", "Mauricio Macri", 43847430, 102, "America", 4, 329, 2); Republic republicNauru = new Republic("The Republic of Nauru", "Baron Waqa", 10084, 50, "Oceania", 3, 19, 3); // Monarchy class. //Monarchy monarchyBelgium = new Monarchy("Belgium", "Philippe", 11358357, 189, "Europe", "The House of Saxe-Coburg and Gotha"); Monarchy monarchyAustralia = new Monarchy("Australia", "Elizabeth II", 24067700, 117, "Oceania", "House of Windsor"); Monarchy monarchyJamaica = new Monarchy("Jamaica", "Elizabeth II", 43847430, 56, "America", "House of Windsor"); // Kingdom class. //Kingdom kingdomSaudiArabia = new Kingdom("The Kingdom of Saudi Arabia", "Salman", 33000000, 87, "Asia", "The Sudairi Seven"); Kingdom kingdomMarocco = new Kingdom("The Kingdom of Morocco", "Mohammed VI", 33848242, 62, "Africa", "The Alaouite dynasty"); // Creating new arraylist. ArrayList arrayList = new ArrayList { //republicCongo, republicCzech, republicTurkey, republicArgentina, republicNauru, //monarchyBelgium, monarchyAustralia, monarchyJamaica, //kingdomSaudiArabia, kingdomMarocco, }; int choice = -1; do { Console.Clear(); // Showing the created arraylist. Console.WriteLine("CURRENT ARRAYLIST:"); foreach (AbstrState state in arrayList) { state.Show(); } Console.WriteLine(@"Choose one of the options: 1. Add element. 2. Delete element. 3. Show the number of republics. 4. Show all the monarchy states. 5. Show all the African states. 6. Clone the arraylist. 7. Sort the arraylist. 8. Search element. 9. Back."); // Getting the number of the chosen option. choice = Program.ChoiceInput(9); switch (choice) { case 1: AddElem(ref arrayList); break; case 2: DelElem(ref arrayList); break; case 3: ShowNumOfRep(arrayList); break; case 4: ShowMon(arrayList); break; case 5: ShowAfrSt(arrayList); break; case 6: CloneArrayList(arrayList); break; case 7: SortArrayList(ref arrayList); break; case 8: BinarySearchArrL(arrayList); break; } } while (choice != 9); //Console.ReadLine(); }