Ejemplo n.º 1
0
        /// <summary>runs Name menu which controls and allows the user to change and edit the name list</summary>
        public static void NameMenu()
        {
            String FileName = Tools.OpenDialog("Find Names", "text files|*.txt"); //opens file dialog to get chosen filename

            if (FileName == null)                                                 //if file name is null return to previous menu
            {
                return;
            }
            bool ListChanged = false;                             //other redundant listchanged boolean

            String[] FileContents = Tools.FileToString(FileName); //transfers file contents to a string array
            NameList Names        = new NameList(FileContents);   //creates the name list

            for (int i = 0; i < Names.Count; i++)                 //displays contents of file
            {
                Console.WriteLine(Names[i].NameToString(NameFormat.ORIGINAL));
            }

            Tools.PressAnyKey();
            UtilityNamespace.Menu NameMenu = new UtilityNamespace.Menu("Name Menu");                               //creats name menu to  help user interact with name list
            NameMenu = NameMenu + "Add Name" + "Delete Name" + "List Names" + "Find Name" + "Return to Main Menu"; //defines choices for name menu
            NameChoice nameChoice = (NameChoice)NameMenu.GetChoice();                                              //displays name menu and gets choice

            while (nameChoice != NameChoice.QUIT)                                                                  //if name choice is quit it returns to main menu
            {
                switch (nameChoice)                                                                                //switch statement for dealing with user choice
                {
                case NameChoice.ADD:                                                                               //choice to add name to NameList
                    Console.WriteLine("Enter name to add");
                    Name newName = new Name(Console.ReadLine());                                                   //gets name to add
                    Names = Names + newName;                                                                       //adds name to namelist
                    Names.ListChange();
                    ListChanged = true;                                                                            //sets list as changed
                    break;

                case NameChoice.DELETE:                          //choice to delete a name
                    Console.WriteLine("Which name would you like to remove?");
                    Name RemoveName = Names[Console.ReadLine()]; //gets name to remove
                    Names       = Names - RemoveName;
                    ListChanged = true;                          //sets list as changed
                    break;

                case NameChoice.LIST:                                                       //choice to list the names
                    Console.WriteLine("Which format would you like the name(s) to be in?"); //prompts for user prefered name format
                    FormatMenu(Names);                                                      //calls to display format menu
                    break;

                case NameChoice.NAME:        //choice to find a name
                    Console.WriteLine("Which Name would you like to find?");
                    String   nameToFind = Console.ReadLine();
                    NameList FoundNames = Names.FindNames(nameToFind); //gets names similar to name given
                    Name     FoundName;
                    if (FoundNames.Count != 0)                         //if no names found
                    {
                        for (int i = 1; i <= FoundNames.Count; i++)    //displays each name found
                        {
                            Console.WriteLine("{0}. {1}", i, FoundNames[i - 1].NameToString(NameFormat.FIRST));
                        }
                        Console.WriteLine("These names were found, please select one. \n0 is if you did not see the name you were looking for");        //asks user to pick name from names found
                        String s = Console.ReadLine();
                        try
                        {
                            int input = Convert.ToInt32(Console.ReadLine());    //converts user input to int


                            if (input != 0 && input <= FoundNames.Count)                                      //validates name chosen
                            {
                                Console.Clear();                                                              //clears console
                                FoundName = FoundNames[input];                                                //gets name chosen from found names
                                Console.WriteLine("Found the name {0}\nWhat would you like to do with it?", FoundName.NameToString(NameFormat.ORIGINAL));
                                UtilityNamespace.Menu NameAction = new UtilityNamespace.Menu("Name Actions"); //options to remove name
                                NameAction = NameAction + "Delete The Name" + "Nothing";                      //menu for single found name
                                int choice = NameAction.GetChoice();
                                if (choice == 1)
                                {
                                    Names = Names - FoundName;    //removes name from NameList
                                }
                            }
                        }
                        catch (Exception e)
                        {
                            Console.WriteLine("Invalid choice");    //throws exception if user choice is invalid
                        }
                    }
                    else
                    {
                        Console.WriteLine("Name was not found");         //if name is not found
                        Tools.PressAnyKey();
                    }
                    break;
                }
                nameChoice = (NameChoice)NameMenu.GetChoice();                                                                                 //prompts for name choice again
            }
            if (Names.ListChanged)                                                                                                             //if nameList is change ask to save
            {
                UtilityNamespace.Menu SaveMenu = new UtilityNamespace.Menu("NameList has been changed, Would you like to save it to a file?"); //save file menu
                SaveMenu = SaveMenu + "Yes" + "No";
                int SaveChoice = SaveMenu.GetChoice();
                if (SaveChoice == 1)
                {
                    Tools.SaveFileDialog("Save File", Names.ToArray(), "text files|*.txt");    // saves file
                }
            }
        }