Ejemplo n.º 1
0
        static public int menu()
        {
            Queue <string> myQueue = new Queue <string>();

            System.Diagnostics.Stopwatch stopwatch = new System.Diagnostics.Stopwatch();



            int iSelect = 3; //random number to get in the while loop

            while (iSelect != 7)
            {
                displayMenu();

                ErrorHandling check = new ErrorHandling();

                iSelect = check.Input("Menu Selector: ", 7);

                #region addOne
                if (iSelect == 1)
                {
                    Console.Write("\nEnter a string: ");
                    string sEntry;
                    try
                    {
                        sEntry = Console.ReadLine();
                        if (sEntry != "")
                        {
                            myQueue.Enqueue(sEntry);
                            //Display status
                            Console.WriteLine("\n\"" + myQueue.Peek() + "\" has been added to stack.");
                        }
                        else
                        {
                            throw new InvalidOperationException("No input detected");
                        }
                    }
                    catch
                    {
                        Console.WriteLine("\nERROR: No input or invalid input detected. Please enter valid input.");
                    }
                }
                #endregion

                #region addList
                else if (iSelect == 2)
                {
                    //Cycle through and input 2000 new entries
                    myQueue.Clear();
                    for (int iCount = 1; iCount <= 2000; iCount++)
                    {
                        myQueue.Enqueue("New Entry " + iCount);
                    }
                }
                #endregion

                #region Display
                else if (iSelect == 3)
                {
                    foreach (String s in myQueue)
                    {
                        Console.WriteLine(s);
                    }
                }
                #endregion

                #region Delete
                else if (iSelect == 4)
                {
                    int iDelete = 0;

                    Console.WriteLine("\nWhich entry do you want to delete?");
                    try
                    {
                        iDelete = Convert.ToInt32(Console.ReadLine());
                    }
                    //Catch invalid inputs
                    catch
                    {
                        Console.WriteLine("\nERROR: Selection invalid or does not exist. Please input valid selection.");
                    }

                    //find and delete selected entry
                    int            iNum     = myQueue.Count;
                    Queue <string> myQueue2 = new Queue <string>();
                    for (int iCount2 = 0; iCount2 < iNum; iCount2++)
                    {
                        if (iCount2 == iDelete - 1)
                        {
                            myQueue.Dequeue();
                        }
                        else
                        {
                            myQueue2.Enqueue(myQueue.Dequeue());
                        }
                    }
                    myQueue = myQueue2; //restore queue without condemned entry
                }
                #endregion

                else if (iSelect == 5)
                {
                    myQueue.Clear();
                    Console.WriteLine("\nQueue has been cleared");
                }
                else if (iSelect == 6)
                {
                    Console.WriteLine("\nWhat entry do you want to search for?");
                    string sSearch = Console.ReadLine();
                    stopwatch.Start();

                    /*for (int iCount2 = 0; iCount2 < iSearch; iCount2++)
                     * {
                     *  myQueue.Peek();
                     *  if (iCount2 == iSearch)
                     *  {
                     *      Console.WriteLine(myQueue.Peek());
                     *  }
                     * }
                     * */

                    if (myQueue.Contains(sSearch))
                    {
                        Console.WriteLine("\nItem found!");
                        stopwatch.Stop();
                        Console.WriteLine("\nTime elapsed: {0}", stopwatch.Elapsed);
                    }
                    else
                    {
                        Console.WriteLine("\nItem not found.");
                        stopwatch.Stop();
                        Console.WriteLine("\nTime elapsed: {0}", stopwatch.Elapsed);
                    }
                }
                else if (iSelect == 7)
                {
                    return(0);
                }//Return to main Menu
            }

            return(0);
        }
        static public int menu()
        {
            int iDictionaryMenu = 0;
            Dictionary <string, int> myDictionary = new Dictionary <string, int>();

            while (iDictionaryMenu != 7)
            {
                //////////ENTER CODE FOR A MENU OF A DICTIONARY
                Console.WriteLine("\n\n-------DICTIONARY MENU-------");
                Console.WriteLine("1. Add one time to Dictionary");
                Console.WriteLine("2. Add Huge List of Items to Dictionary");
                Console.WriteLine("3. Display Dictionary");
                Console.WriteLine("4. Delete from Dictionary");
                Console.WriteLine("5. Clear Dictionary");
                Console.WriteLine("6. Search Dictionary");
                Console.WriteLine("7. Return to Main Menu");
                Console.Write("Menu Selector: ");
                ErrorHandling check = new ErrorHandling();

                iDictionaryMenu = check.Input("Menu Selector: ", 7);



                string sDictionaryString;
                int    iDictionaryCount;



                #region addOne
                if (iDictionaryMenu == 1)
                {
                    try
                    {
                        //ManageDictionary.addOne
                        //Add one string to the dicitonary
                        Console.Write("\nEnter a string into the data structure: ");
                        sDictionaryString = Console.ReadLine();
                        if (sDictionaryString != "")
                        {
                            myDictionary.Add(sDictionaryString, myDictionary.Count + 1);
                        }
                        else
                        {
                            throw new InvalidOperationException("No input detected");
                        }
                    }
                    catch
                    {
                        Console.WriteLine("\nERROR: No input or invalid input detected. Please enter valid input.");
                    }
                    #endregion

                    #region addList
                }//Dictionary Menu 1 One
                else if (iDictionaryMenu == 2)
                {
                    //ManageDictionary.AddList
                    myDictionary.Clear();
                    for (iDictionaryCount = 1; iDictionaryCount <= 2000; iDictionaryCount++)
                    {
                        myDictionary.Add("New entry " + (iDictionaryCount), iDictionaryCount);
                    }
                }//DictionaryMenu 2 list
                #endregion

                #region display

                else if (iDictionaryMenu == 3)
                {
                    //ManageDictionary.Display
                    foreach (KeyValuePair <string, int> s in myDictionary)
                    {
                        Console.WriteLine(s.Key + " ");
                        //Console.WriteLine(s.Value);
                    }
                }//Dictionary Menu 3 Display

                #endregion



                else if (iDictionaryMenu == 4)
                {
                    //ManageDictionary.Delete
                    Console.WriteLine("\nWhat entry would you like to delete? ");
                    string sInput = Console.ReadLine();

                    if (myDictionary.ContainsKey(sInput))
                    {
                        myDictionary.Remove(sInput);
                    }//Delete Dictionary
                    else
                    {
                        Console.WriteLine("\nThe entry you would like to remove was not found. Please enter a valid entry.");
                    }
                }//Dictionary menu 4


                else if (iDictionaryMenu == 5)
                {
                    //ManageDictionary.Clear
                    myDictionary.Clear();
                }//Clear Dictionary Menu 5


                else if (iDictionaryMenu == 6)
                {
                    //ManageDictionary.Search

                    Console.WriteLine("\nEnter an item you would like to search for:\n");
                    string sSearchEntry = Console.ReadLine();

                    System.Diagnostics.Stopwatch sw = new System.Diagnostics.Stopwatch();

                    sw.Start();

                    if (myDictionary.ContainsKey(sSearchEntry))
                    {
                        Console.WriteLine("\nValue for " + sSearchEntry + " is " + myDictionary[sSearchEntry]);
                        sw.Stop();
                    }
                    else
                    {
                        Console.WriteLine("\nItem not found.");
                    }

                    sw.Stop();
                    Console.WriteLine("\nTime elapsed: " + sw.Elapsed);
                }//Search Dictionary Menu 6


                else if (iDictionaryMenu == 7)
                {
                    //ManageDictionary.Return
                    Console.WriteLine("\n");
                    return(0);
                } //Return to main Menu
            }     //end of while loop

            return(0);
        }//end of menu