/// <summary> /// Assigns partner for the current person /// </summary> /// <param name="partner">The partner that has been picked (if it has)</param> /// <param name="id">Used to check if the person already has a partner</param> public void AssignPartner(int id, Person partner = null) { if (!PersonDB.CheckPartner(id)) { if (partner == null) { Person tmpPartner = Generate.GenPerson("Type in details for the partner"); PartnerId = tmpPartner.Id; tmpPartner.PartnerId = Id; PersonDB.Add(tmpPartner); } else { for (int i = 0; i < PersonDB.personList.Count; i++) { if (PersonDB.personList[i].Id == partner.Id) { PersonDB.personList[i].PartnerId = Id; PartnerId = PersonDB.personList[i].Id; } } } } else { Print.PrMsg("Person already has partner"); } }
/// <summary> /// Generates a child for a couple /// </summary> /// <param name="request">Specifies the situation the child is created for</param> /// <param name="parentSurname">Surname of the parent that the child will inherit</param> /// <param name="parentBirthyear">Birthyear of the parent, used to check that the child isn't older than the parent</param> /// <param name="parentName">The name of the parent, used for interface purposes only</param> /// <returns>The child generated</returns> public static Person GenChildPerson(string request, string parentSurname, int parentBirthyear, string parentName) { Print.PrMsg("Creating child of " + parentName); bool validChild = false; Person returnChild = null; while (!validChild) { Person tmpChild = GenPerson(request); if ((parentBirthyear - tmpChild.Birthyear) < 18) { returnChild = tmpChild; validChild = true; if (tmpChild.Surname != parentSurname) { tmpChild.Surname = parentSurname; Print.PrMsg("Surname has been adjusted to match parents"); } } else { Print.PrMsg("Error: Birthyear difference relative to parents needs to be atleast 18, and surname needs to match"); } } return(returnChild); }
public static bool YesOrNo(string request) { Print.PrMsg(request); bool inputValid = false; bool answer = false; while (!inputValid) { string input = Console.ReadLine(); if (Validator.HasKnownChars(input, "Yes") || Validator.HasKnownChars(input, "yes")) { answer = true; inputValid = true; } else if (Validator.HasKnownChars(input, "No") || Validator.HasKnownChars(input, "no")) { answer = false; inputValid = true; } else { Print.PrMsg("Please use 'Yes' or 'No' (not case sensitive)"); } } return(answer); }
/// <summary> /// Generates a person /// </summary> /// <param name="request">Specifies which situation the person is generated for</param> /// <returns>A person newly generated</returns> public static Person GenPerson(string request) { bool inputGood = false; Person returnPerson = new Person("", "", 0, false); while (!inputGood) { Print.PrMsg(request, "Please follow the syntax: name,surname,birthyear,sex"); string input = Console.ReadLine(); List <string> personList = Validator.PersonSyntaxValidator(input, "Person"); if (personList != null) { string name = personList[1]; string surname = personList[2]; int birthyear = Convert.ToInt32(personList[3]); bool sex; sex = personList[4] != "Male"; returnPerson = new Person(name, surname, birthyear, sex); inputGood = true; } else { Print.PrMsg("Syntax error"); } } return(returnPerson); }
/// <summary> /// Generates parents for a person /// </summary> /// <param name="request">Specifies the situation the parents are created for</param> /// <param name="personSurname">Surname of the child, is to be inherited by the parents</param> /// <param name="personName">Name of the child, used for interface purposes only</param> /// <param name="personBirthyear">Birthyear of the parent, used to check that the child isn't older than the parent</param> /// <param name="id">Id of the child, used to set the parentId:s for the child</param> /// <param name="siblingIdList">List of siblings, used to assign other children to the parents</param> /// <returns></returns> public static Person[] GenParents(string request, string personSurname, string personName, int personBirthyear, int id, List <int> siblingIdList) { Person[] returnParent = new Person[2]; returnParent[0] = null; returnParent[1] = null; Print.PrMsg("Creating parents of " + personName); bool validParents = false; while (!validParents) { Person[] tmpParent = new Person[2]; for (int i = 0; i < 2;) { tmpParent[i] = GenPerson(request); if (personBirthyear - tmpParent[i].Birthyear > 17) { returnParent[i] = tmpParent[i]; validParents = true; for (int j = 0; j < PersonDB.personList.Count; j++) { for (int k = 0; k < siblingIdList.Count; k++) { if (siblingIdList[k] == PersonDB.personList[j].Id) { PersonDB.personList[j].ParentId[i] = tmpParent[i].Id; } } if (id == PersonDB.personList[j].Id) { PersonDB.personList[j].ParentId[i] = tmpParent[i].Id; } } if (tmpParent[i].Surname != personSurname) { tmpParent[i].Surname = personSurname; Print.PrMsg("Surname has been adjusted to match child"); } i++; } else { Print.PrMsg("Age difference between child and parent needs to be atleast 18"); } } tmpParent[0].PartnerId = tmpParent[1].Id; tmpParent[1].PartnerId = tmpParent[0].Id; } return(returnParent); }
/// <summary> /// Generates a sibling for a person /// </summary> /// <param name="request">Specifies the situation the person is created for</param> /// <param name="siblingSurname">Surname of the parent that the person will inherit</param> /// <param name="parentId">Id for the parents of the siblings, used to check that the child isn't older than the parent</param> /// <param name="siblingName">The name of the sibling, used for interface purposes only</param> /// <returns>The sibling generated</returns> public static Person GenSiblingPerson(string request, string siblingSurname, int[] parentId, string siblingName) { Print.PrMsg("Creating sibling of " + siblingName); bool validChild = false; Person returnSibling = null; while (!validChild) { Person tmpSibling = GenPerson(request); if (parentId[0] == -1) { returnSibling = tmpSibling; validChild = true; } else { for (int i = 0; i < PersonDB.personList.Count; i++) { if (parentId[0] == PersonDB.personList[i].Id && tmpSibling.Birthyear - PersonDB.personList[i].Birthyear > 17) { tmpSibling.ParentId = parentId; returnSibling = tmpSibling; validChild = true; } else { Print.PrMsg("Age difference between sibling and parent needs to be atleast 18"); } } } if (tmpSibling.Surname != siblingSurname) { tmpSibling.Surname = siblingSurname; Print.PrMsg("Surname has been adjusted to match sibling"); } } return(returnSibling); }
/// <summary> /// Assigns parents to a person /// </summary> /// <returns></returns> public Person[] AssignParents() { Person[] parents = null; if (ParentId[0] == -1) { parents = Generate.GenParents("Please input the credentials for this persons parents", Surname, Name, Birthyear, Id, SiblingIdList); for (int i = 0; i < parents.Length; i++) { PersonDB.Add(parents[i]); } } else { Print.PrMsg("Person already has parents"); } return(parents); }
/// <summary> /// Main command line interface, used for requesting an input and helping the users /// </summary> public static void RequestAction() { while (true) { Print.PrMsg("Write a command or a chain of commands, separating them with '.' input '!help' for help", "To execute several commands in a row, separate them with &. Whitespace does not matter."); Print.PrMsg("Other commands: printdb, load, save"); string input = Console.ReadLine(); if (input == "!help") { Console.Clear(); Print.PrMsg("Actions are separated with '.', for example: Create.Person(2); or Create.Person(1).Siblings(2); "); Print.PrMsg("To write several commands at once, the can be separated with '&', for example: Create.Person(2); & Create.Person(3); "); Print.PrMsg("Start your line with either Add or Create depending on your preferred action"); Print.PrMsg("Please use a valid operation prefix: Parents, Partner, Children(1-9), Siblings(1-9), Person(1-9), Person(name, surname, birthyear, 'Male' / 'Female')"); } else { List <string>[] commandList = Validator.SyntaxValidator(input); if (commandList == null) { Print.PrMsg("Input not valid"); } else { foreach (var list in commandList) { Compiler.CompileRequest(list); ReadWrite.SaveLine(list); } } } } }
public static void LoadSession() { Console.Clear(); string[] fullList = File.ReadAllLines(dir); int listCount = 0; mainList = new List <List <string> >(); List <string> tmpList = new List <string>(); for (int i = 0; i < fullList.Length; i++) { if (fullList[i] == "&") { mainList.Add(tmpList); tmpList = new List <string>(); i++; } else { tmpList.Add(fullList[i]); } } Print.PrMsg("Commands executed:"); foreach (var t in mainList) { foreach (var s in t) { Console.WriteLine(s); } Print.PrMsg("New command"); Compiler.CompileRequest(t); } }
/// <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); } } }
public static void SaveSession() { if (mainList.Count == 0) { Print.PrMsg("Session empty"); } else { List <string> tmpList = new List <string>(); for (int i = 0; i < mainList.Count; i++) { for (int j = 0; j < mainList[i].Count; j++) { tmpList.Add(mainList[i][j]); } tmpList.Add("&"); } File.WriteAllLines(dir, tmpList); } }
/// <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); }
/// <summary> /// Runs functions for the specified commands in the list 'commandList' /// </summary> /// <param name="commandList">The current list of commands returned from the validator</param> public static void CompileRequest(List <string> commandList) { List <Person> tmpPersonList = new List <Person>(); bool hasPerson = false; bool picked = false; for (int i = 0; i < commandList.Count; i++) { if (commandList[0] == "Add" && !picked) { tmpPersonList.Add(Input.PickPerson("Pick a person to execute your commands on")); if (tmpPersonList[0] == null) { break; } hasPerson = true; picked = true; } else { if (commandList[i] == "PersonStruct") { bool sex; if (commandList[i + 4] == "Male") { sex = false; } else { sex = true; } Person tmpPerson = new Person(commandList[i + 1], commandList[i + 2], Convert.ToInt32(commandList[i + 3]), sex); tmpPersonList.Add(tmpPerson); PersonDB.Add(tmpPerson); hasPerson = true; } else if (commandList[i].Contains("Person")) { int tmpCount = Convert.ToInt32(commandList[i].Substring(commandList[i].IndexOf('*') + 1)); for (int j = 0; j < tmpCount; j++) { Person tmpPerson = Generate.GenPerson("Type in the details for person #" + j + 1); tmpPersonList.Add(tmpPerson); PersonDB.Add(tmpPerson); } hasPerson = true; } } for (int j = 0; j < tmpPersonList.Count; j++) { if (commandList[i].Contains("Siblings")) { //Print.PrMsg("Assigning siblings for person " + j + 1 + ", Name: " + tmpPersonList[j].Name); int tmpCount = Convert.ToInt32(commandList[i].Substring(commandList[i].IndexOf('*') + 1)); tmpPersonList[j].AssignSibling(tmpCount); } if (commandList[i].Contains("Partner")) { //Print.PrMsg("Assigning partner for person " + j + 1 + ", Name: " + tmpPersonList[j].Name); if (Input.YesOrNo("Do you want to pick a partner? (Yes/No)")) { Person pickedPartner = Input.PickPerson("Pick a partner for" + tmpPersonList[j].Name); tmpPersonList[j].AssignPartner(tmpPersonList[j].Id, pickedPartner); } else { tmpPersonList[j].AssignPartner(tmpPersonList[j].Id); } } if (commandList[i].Contains("Children")) { //Print.PrMsg("Assigning children for person " + j + 1 + ", Name: " + tmpPersonList[j].Name); int tmpCount = Convert.ToInt32(commandList[i].Substring(commandList[i].IndexOf('*') + 1)); tmpPersonList[j].AssignChild(tmpCount); } if (commandList[i].Contains("Parents") && hasPerson) { //Print.PrMsg("Assigning parents for person " + j+1 + ", Name: "+ tmpPersonList[j].Name); tmpPersonList[j].AssignParents(); } } if (tmpPersonList.Count == 0) { Print.PrMsg("You did not add any people prior to assigning other family members, did you mean to use 'Add'?"); } } }
/// <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); } }