// Removes a word and definition public void RemoveWordAndDefn(string word) { int index = DictionaryUtilities.IndexFinder(word, WordListString()); words = DictionaryUtilities.ShrinkSizeByOne <TKey>(words, index); definitions = DictionaryUtilities.ShrinkSizeByOne <TValue>(definitions, index); }
public static void Initialize(dynamic dictionary) { Console.WriteLine("The dictionary is empty. Would you like to add a word?"); Console.WriteLine("Type Yes to add a word."); Console.WriteLine("Type No to exit.\n"); while (true) { Console.Write("Enter here: "); string initDictionary = Console.ReadLine(); if (initDictionary.Equals("Yes", StringComparison.OrdinalIgnoreCase)) { Console.Clear(); Console.WriteLine("Welcome to your dictionary!\n"); DictionaryUtilities.AddWordAndDefn(dictionary); break; } else if (initDictionary.Equals("No", StringComparison.OrdinalIgnoreCase)) { Environment.Exit(0); } else { Console.WriteLine("Incorrect choice, try again.\n"); } } Console.Clear(); }
// Returns the definition of the user searched word OR adds definition to the searched word public TValue this[TKey searchedWord] { get { index = DictionaryUtilities.IndexFinder(Convert.ToString(searchedWord), WordListString()); if (index >= 0) { return(definitions[index]); } else { throw new ArgumentNullException("The searched word is not defined in the dictionary."); } } set { index = DictionaryUtilities.IndexFinder(Convert.ToString(searchedWord), WordListString()); if (index >= 0) { definitions[index] = value; AlphabetizeDictionary(); } else { if (words.Length == definitions.Length) { words = DictionaryUtilities.GrowSizeByOne(words); definitions = DictionaryUtilities.GrowSizeByOne(definitions); int newLength = words.Length; words[newLength - 1] = searchedWord; definitions[newLength - 1] = value; AlphabetizeDictionary(); } else { Console.WriteLine("There appears to be a mismatch of words to definitions. Program terminating."); Environment.Exit(0); } } } }
static void Main(string[] args) { InitDictionary.StartMessage(); IndexerDictionary <string, string> dictionary = new IndexerDictionary <string, string>(); string programInput; if (dictionary.EmptyDictionaryChecker()) { InitDictionary.Initialize(dictionary); } // MAIN PROGRAM // while (true) { Console.WriteLine("1. Search a word\n" + "2. Add a word and its definition\n" + "3. Change a word's spelling\n" + "4. Change a word's definition\n" + "5. Remove a word\n" + "6. See all words in the dictionary\n" + "7. Exit program\n"); Console.Write("Type the number of your choice: "); programInput = Console.ReadLine(); if (programInput == "1") { DictionaryUtilities.SearchForWord(dictionary); } else if (programInput == "2") { DictionaryUtilities.AddWordAndDefn(dictionary); } else if (programInput == "3") { DictionaryUtilities.ChangeSpelling(dictionary); } else if (programInput == "4") { DictionaryUtilities.ChangeDefinition(dictionary); } else if (programInput == "5") { DictionaryUtilities.RemoveWordAndDefn(dictionary); } else if (programInput == "6") { DictionaryUtilities.ShowAllWordsAndDefn(dictionary); } else if (programInput == "7") { InitDictionary.ExitProgram(); } else { Console.WriteLine("\nIncorrect choice, try again."); } Console.Write("Press any key to go back to the main menu: "); Console.ReadKey(); Console.Clear(); } }