Esempio n. 1
0
        /// <summary>
        /// Assigns child to the current person
        /// </summary>
        /// <param name="count">Specifies the amount of children created</param>
        public void AssignChild(int count)
        {
            if (PartnerId == -1)
            {
                Print.PrMsg("No partner present, partner is necessary to assign a child");
                Console.WriteLine("Person was added, to assign a partner and a child, use the 'Add' command");
                Print.PrDb();
            }
            else
            {
                for (int i = 0; i < count; i++)
                {
                    Person child = Generate.GenChildPerson(("Input the details for the child of " + Name), Surname, Birthyear, Name);

                    child.ParentId[0] = Id;
                    child.ParentId[1] = PartnerId;

                    childId.Add(child.Id);

                    PersonDB.Add(child);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Function used for picking a person when using the command 'Add'
        /// </summary>
        /// <returns>The person picked</returns>
        public static Person PickPerson(string request)
        {
            if (PersonDB.personList.Count == 0)
            {
                Print.PrMsg("No people have been added, please use 'Create' command to add some!");
            }
            else
            {
                Print.PrDb();
                Print.PrMsg(request);
                Print.PrMsg("Please pick a person by inputting the associated number of the person");
                bool   validNumber  = false;
                Person returnPerson = new Person("", "", 0, false);

                while (!validNumber)
                {
                    string input = Console.ReadLine();

                    if (Validator.HasKnownChars(input, "0-9"))
                    {
                        int personNum = Convert.ToInt32(input);

                        returnPerson = PersonDB.personList[personNum - 1];

                        validNumber = true;
                    }
                    else
                    {
                        Print.PrMsg("Input invalid, use numbers only");
                    }
                }

                return(returnPerson);
            }

            return(null);
        }
Esempio n. 3
0
        /// <summary>
        /// Validates commands that the person has inputted
        /// </summary>
        /// <param name="inputFull">The full input, one or more commands</param>
        /// <returns></returns>
        public static List <string>[] SyntaxValidator(string inputFull)
        {
            //Removes whitespace
            inputFull = Regex.Replace(inputFull, @"\s+", "");
            string[] inputLines = inputFull.Split('&');

            List <string>[] outList = new List <string> [inputLines.Length];
            for (int i = 0; i < outList.Length; i++)
            {
                outList[i] = new List <string>();
            }

            bool validList = true;

            for (var k = 0; k < inputLines.Length; k++)
            {
                var input = inputLines[k];

                if (input == "printdb")
                {
                    Print.PrDb();
                }
                else if (input == "save")
                {
                    ReadWrite.SaveSession();
                }
                else if (input == "load")
                {
                    if (Input.YesOrNo("Are you sure? All progress will be lost! (yes/no)"))
                    {
                        ReadWrite.LoadSession();
                    }
                }
                else if (input.IndexOf(';') == input.Length - 1 && (!string.IsNullOrEmpty(input) || Regex.Replace(input, "[^0-9A-z.()]", "") == input))
                {
                    string[] inputArr = input.Split('.');

                    if (Regex.IsMatch(inputArr[0], "Add|Create"))
                    {
                        outList[k].Add(inputArr[0]);
                        bool hasPerson = false;

                        for (int i = 1; i < inputArr.Length; i++)
                        {
                            string validPrefix =
                                "Children[(][0-9]+[)]|Siblings[(][0-9]+[)]|Person[(][0-9]+[)]|Person[(][A-z]+[,][A-z]+[,][0-9]+[,](Male|Female|male|female)[)]";

                            if (Regex.IsMatch(inputArr[i], "Parents|Partner") && !inputArr[i].Contains('(') &&
                                !inputArr[i].Contains(')'))
                            {
                                outList[k].Add(inputArr[i]);
                            }
                            else if (Regex.IsMatch(inputArr[i], validPrefix))
                            {
                                if ((i == inputArr.Length - 1 && Validator.HasKnownChars(inputArr[i], "0-9A-z();,") ||
                                     (i < inputArr.Length && Validator.HasKnownChars(inputArr[i], "0-9A-z(),"))))
                                {
                                    string tmp = inputArr[i].Substring(inputArr[i].IndexOf('(') + 1,
                                                                       inputArr[i].IndexOf(')') - 1 - inputArr[i].IndexOf('('));

                                    if (inputArr[i].Contains("Person"))
                                    {
                                        if (hasPerson)
                                        {
                                            validList = false;
                                            Print.PrMsg(
                                                "Separate 2 lines with '&'. You can not use more than one Person() command in a line");
                                        }
                                        else
                                        {
                                            hasPerson = true;
                                        }
                                    }

                                    if (Validator.HasKnownChars(tmp, "0-9"))
                                    {
                                        string prefix = inputArr[i].Substring(0, inputArr[i].IndexOf('('));

                                        if (prefix == "Person" && inputArr[0] == "Add")
                                        {
                                            Print.PrMsg("Syntax error: Cannot 'Add' 'Person' to another 'Person'");

                                            validList = false;
                                        }
                                        else
                                        {
                                            outList[k].Add(prefix + "*" + tmp);
                                        }
                                    }
                                    else if (Validator.HasKnownChars(tmp, "[A-z]+[,][A-z]+[,][0-9]+[,](Male|Female|male|female)") &&
                                             inputArr[0] == "Create")
                                    {
                                        outList[k].AddRange(PersonSyntaxValidator(tmp, "PersonStruct"));
                                    }
                                    else
                                    {
                                        Print.PrMsg("Syntax error: Cannot 'Add' 'Person' to another 'Person'");

                                        validList = false;
                                    }
                                }
                                else
                                {
                                    Print.PrMsg("Unknown characters detected, write !help for help");

                                    validList = false;
                                }
                            }
                            else
                            {
                                Print.PrMsg(
                                    "Please use a valid operation prefix: Parents, Partner, Children(0-9), Siblings(0-9), Person(0-9), Person(name, surname, birthyear, 'Male' / 'Female')",
                                    "You used: " + inputArr[i]);

                                validList = false;
                            }
                        }
                    }
                    else
                    {
                        Print.PrMsg("Please start line with 'Add' or 'Create' (Case sensitive)");

                        validList = false;
                    }
                }
                else if (!input.Contains(';'))
                {
                    Print.PrMsg("Please end the line with a ';'");

                    validList = false;
                }
                else
                {
                    Print.PrMsg("Unknown characters detected, write !help for help");

                    validList = false;
                }
            }

            if (validList)
            {
                return(outList);
            }
            else
            {
                return(outList = null);
            }
        }