예제 #1
0
        /// <summary>
        /// method asking the user for the correct information to create a new person in the database.
        /// </summary>
        /// <param name="addParentWhenCreatingNewPerson">If user has chosen to add a new parent when adding a new person, this will be true.</param>
        /// <returns>Returns true if the loop in the calling method should end or false if it should continue to loop.</returns>
        private bool AskForInputToCreatePerson(bool addParentWhenCreatingNewPerson = false)
        {
            /*******************************************************************
            *          ASKFORINPUTTOCREATEPERSON() - PRIVATE
            *******************************************************************/
            List <string> mandatory = new List <string> {
                "förnamn", "efternamn"
            };
            List <string> optional = new List <string> {
                "födelsedatum (enter för att skippa)", "dödsdatum (enter för att skippa)"
            };
            List <string> parents = new List <string> {
                "mamma", "pappa"
            };
            List <string> userInputs       = new List <string>();
            List <string> userInputsParent = new List <string>();
            int           numberOfInputsRequiredToCreatePerson = 6;
            Crud          crud         = new Crud(standardDatabaseName);
            bool          continueLoop = true;

            do
            {
                if (!addParentWhenCreatingNewPerson)
                {
                    Console.Clear();
                    Print.PrintText("Add");
                    Console.WriteLine(Environment.NewLine);
                    Console.WriteLine("\t\tLägg till en person");
                    Console.WriteLine("\t\t" + new string('═', 20));
                    Console.WriteLine("\n\t\tTryck 0 + enter närsomhelst för att avbryta");
                    Console.WriteLine();
                }
                foreach (var input in mandatory)
                {
                    string userInput = AskUserForMandatoryInput(input);
                    if (userInput == "0")
                    {
                        continueLoop = false;
                        addParentWhenCreatingNewPerson = false;
                        break;
                    }
                    else
                    {
                        if (addParentWhenCreatingNewPerson)
                        {
                            userInputsParent.Add(userInput);
                        }
                        else
                        {
                            userInputs.Add(userInput);
                        }
                    }
                }

                if (continueLoop)
                {
                    foreach (var input in optional)
                    {
                        string userInput = AskUserForOptionalInput(input);
                        if (userInput == "0")
                        {
                            continueLoop = false;
                            addParentWhenCreatingNewPerson = false;
                            break;
                        }
                        else
                        {
                            if (addParentWhenCreatingNewPerson)
                            {
                                userInputsParent.Add(userInput);
                            }
                            else
                            {
                                userInputs.Add(userInput);
                            }
                        }
                    }
                }
                if (!addParentWhenCreatingNewPerson && continueLoop)
                {
                    foreach (var input in parents)
                    {
                        string userInput = AskUserForParentInput(input);
                        if (userInput == "0")
                        {
                            continueLoop = false;
                            break;
                        }
                        if (userInput == "")
                        {
                            userInput = "0";
                            userInputs.Add(userInput);
                        }
                        else
                        {
                            userInputs.Add(userInput);
                        }
                    }
                }
                if (userInputsParent.Count >= 2 && addParentWhenCreatingNewPerson)
                {
                    Relative personAdded = new Relative(
                        //Firstname
                        userInputsParent[0],
                        //Lastname
                        userInputsParent[1],
                        //Date of birth
                        Convert.ToDateTime(userInputsParent[2]),
                        //Date of death
                        Convert.ToDateTime(userInputsParent[3]), 0, 0);
                    crud.AddPerson(personAdded);
                    userInputsParent.Clear();
                    return(true);
                }
                else if (userInputs.Count >= numberOfInputsRequiredToCreatePerson)
                {
                    Relative personAdded = new Relative(
                        //Firstname
                        userInputs[0],
                        //Lastname
                        userInputs[1],
                        //Date of birth
                        Convert.ToDateTime(userInputs[2]),
                        //Date of death
                        Convert.ToDateTime(userInputs[3]),
                        //MotherId
                        Convert.ToInt32(userInputs[4]),
                        //FatherId
                        Convert.ToInt32(userInputs[5]));
                    userInputs.Clear();

                    if (crud.AddPerson(personAdded) == 1)
                    {
                        Print.Green($"\t\t{personAdded.FirstName} {personAdded.LastName} lades till");
                        Thread.Sleep(1500);
                    }
                    else
                    {
                        Print.Red("\t\tNåt gick snett och personen sparades inte i databasen");
                        Thread.Sleep(2500);
                    }
                }
            } while (continueLoop && !addParentWhenCreatingNewPerson);
            //Code reaches here only if user has pressed 0
            return(false);
        }