Esempio n. 1
0
        //Adds or Updates a person depending on the values set in the menu, to the database
        private void AddorUpdatePerson(Person P, int Statementvalue, int typeOfInserttoDB)
        {
            string name       = "";
            string phone      = "";
            string birthday   = "";
            int    counter    = 0;
            int    idToUpdate = 0;
            // Checks if there are any matches in DB
            bool matchinDB = true;
            //NOTE: Rename this bool to reflect what it does... what does it do? ;S
            bool inputIsNoInt = true;

            //statementvalue=2, updating a person
            if (Statementvalue == 2)
            {
                while (true)
                {
                    PrintAllPersonsWithId(P);
                    // ask for person's id to be updated
                    System.Console.WriteLine("Enter ID of person to update or Q to return to the menu: ");
                    string input = Console.ReadLine();
                    //Checks if input was Q.
                    if (CheckIfInputIsQuit(input.ToUpper()))
                    {
                        return;
                    }
                    //Checks if input is convertable to int
                    else if (input.inputIsInt())
                    {
                        //Converts input and searches for an ID that matches
                        if (DB.FindUserInDB(Convert.ToInt32(input), P))
                        {
                            idToUpdate     = Convert.ToInt32(input);
                            matchinDB      = true;
                            Statementvalue = 1;
                            break;
                        }

                        else
                        {
                            matchinDB = false;
                        }
                    }
                    else
                    {
                        System.Console.WriteLine("Enter a number!");
                    }
                    //If no matches were found in the DB
                    if (!matchinDB)
                    {
                        Console.WriteLine("Id does not exist! Press any key...");
                        Console.ReadLine();
                    }
                }
            }
            //statementvalue=1, ordinary insert, displays the choice to exit.
            else if (Statementvalue == 1)
            {
                System.Console.WriteLine("Press Q and Enter to return to the menu");
            }


            while (true)
            {
                System.Console.Write("Enter Name: ");
                name = Console.ReadLine();
                if (CheckIfInputIsQuit(name.ToUpper()))
                {
                    return;
                }
                //Checks if input is Q
                else if (name.Length <= 50 && name.Length > 1)
                {
                    break;
                }
                else
                {
                    Console.WriteLine("Enter between 2 and 50 characters!");
                }
            }

            // https://stackoverflow.com/questions/8764827/c-sharp-regex-validation-rule-using-regex-match
            // https://regex101.com/r/kF1uH5/2
            var regex = @"[0-9]{2,4}-[0-9]{2,3}\s[0-9]{2,3}\s[0-9]{2,3}$";

            inputIsNoInt = true;
            while (inputIsNoInt)
            {
                System.Console.Write("Enter Telephone number (999-999 99 99): ");
                phone = Console.ReadLine();
                var match = Regex.Match(phone, regex, RegexOptions.IgnoreCase);

                if (match.Success)
                {
                    inputIsNoInt = false;
                }
                else
                {
                    Console.WriteLine("Valid format is: 99-999 999 99, 99-999 99 99, 999-99 99 99");
                }
            }
            // https://www.regular-expressions.info/dates.html
            regex = @"^(19|20)\d\d[- /.](0[1-9]|1[012])[- /.](0[1-9]|[12][0-9]|3[01])$";

            inputIsNoInt = true;
            while (inputIsNoInt)
            {
                System.Console.Write("Enter Birthday (YYYY-MM-DD): ");
                birthday = Console.ReadLine();

                var match = Regex.Match(birthday, regex, RegexOptions.IgnoreCase);
                // if format matches a date AND date is in the past
                if (match.Success && DateTime.Now.CompareTo(DateTime.Parse(birthday)) > 0)
                {
                    inputIsNoInt = false;
                }

                else
                {
                    Console.WriteLine("Enter a valid date, format YYYY-MM-DD)!");
                }
            }

            // check for int
            inputIsNoInt = true;
            while (inputIsNoInt)
            {
                System.Console.Write("Enter the time interval (max 365): ");

                string input = Console.ReadLine();

                if (input.inputIsInt() && counter <= 365)
                {
                    counter      = Convert.ToInt32(input);
                    inputIsNoInt = false;
                }
                else
                {
                    Console.WriteLine("Enter a number of max 365 days!");
                    inputIsNoInt = true;
                }
            }

            //reads the typeofinsert-variable and executes the code depending on the value
            switch (typeOfInserttoDB)
            {
            case 1:
            {
                // add person to DB
                P.AddPerson(name, phone.ToString(), birthday.ToString(), counter);
                System.Console.WriteLine($"{name} has been added to the list. Press any key...");
                Console.ReadLine();
            }
            break;

            case 2:
            {
                // update person
                P.UpdatePerson(idToUpdate, name, phone, birthday, counter);
                System.Console.WriteLine($"{name} has been updated. Press any key...");
                Console.ReadLine();
            }
            break;
            }
        }
Esempio n. 2
0
        // deletes a person from DB
        private void DeletePerson(Person P)

        {
            int  inputAsInt = 0;
            bool validInt   = false;

            while (true)
            {
                Console.Clear();
                PrintAllPersonsWithId(P);

                while (!validInt)
                {
                    PrintAllPersonsWithId(P);
                    // ask for person's id to be deleted
                    System.Console.Write("Enter ID of person to delete or Q to quit: ");
                    // try to get a number
                    string input = Console.ReadLine();
                    //Checks if input is Q

                    if (CheckIfInputIsQuit(input.ToUpper()))
                    {
                        return;
                    }
                    // Send the value to errorhandling to try/catch it
                    else if (input.inputIsInt())
                    {
                        inputAsInt = Convert.ToInt32(input);
                        validInt   = true;
                    }
                    else
                    {
                        System.Console.WriteLine("Enter a number!");
                        Console.ReadLine();
                    }
                }

                // if user exists in DB
                if (DB.FindUserInDB(inputAsInt, P))
                {
                    System.Console.WriteLine($"Are you sure you want to delete ID: {inputAsInt}");
                    System.Console.WriteLine($"[Y]es/[N]o");

                    string input = Console.ReadLine().ToUpper();

                    // check user confirmation
                    while (true)
                    {
                        switch (input)
                        {
                        case "Y":
                        {
                            System.Console.WriteLine("Person has been deleted. Press any key...");
                            P.DeletePerson(inputAsInt);
                            Console.ReadLine();
                            P.GetPersons();
                            PrintAllPersonsWithoutId(P);
                            return;
                        }

                        case "N":
                        {
                            System.Console.WriteLine("No one has been deleted. Press any key...");
                            PrintAllPersonsWithoutId(P);
                            return;
                        }

                        default:
                        {
                            System.Console.WriteLine("Wrong input.");
                            Console.ReadLine();
                            break;
                        }
                        }
                        break;
                    }
                }

                else
                {
                    Console.WriteLine("Id does not exist! Press any key...");
                    validInt = false;
                    Console.ReadLine();
                }
            }
        }