/// <summary> /// Builds a new person /// </summary> private static Xml.Person GenerateXmlPerson() { Xml.Person person = new Xml.Person(); person.Id = LastXmlId; person.Name = $"Name{LastXmlId}"; person.Email = $"Email{LastXmlId}"; person.Phones = new Xml.PersonPhoneNumber[3]; Xml.PersonPhoneNumber xmlPhoneNumber = new Xml.PersonPhoneNumber { Number = $"mobile{LastXmlId}", Type = Xml.PersonPhoneNumberType.Mobile }; person.Phones[0] = xmlPhoneNumber; xmlPhoneNumber = new Xml.PersonPhoneNumber { Number = $"home{LastXmlId}", Type = Xml.PersonPhoneNumberType.Home }; person.Phones[1] = xmlPhoneNumber; xmlPhoneNumber = new Xml.PersonPhoneNumber { Number = $"work{LastXmlId}", Type = Xml.PersonPhoneNumberType.Work }; person.Phones[2] = xmlPhoneNumber; LastXmlId++; return(person); }
/// <summary> /// Builds a person based on user input /// </summary> private static object[] PromptForAddress(TextReader input, TextWriter output) { Person person = new Person(); Xml.Person xmlPerson = new Xml.Person(); Json.Person jsonPerson = new Json.Person(); output.Write("Enter person ID: "); int id = int.Parse(input.ReadLine()); person.Id = id; xmlPerson.Id = (uint)id; jsonPerson.Id = id; output.Write("Enter name: "); var name = input.ReadLine(); person.Name = name; xmlPerson.Name = name; jsonPerson.Name = name; output.Write("Enter email address (blank for none): "); string email = input.ReadLine(); if (email.Length > 0) { person.Email = email; xmlPerson.Email = email; jsonPerson.Email = email; } while (true) { output.Write("Enter a phone number (or leave blank to finish): "); string number = input.ReadLine(); if (number.Length == 0) { break; } Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber { Number = number }; Xml.PersonPhoneNumber xmlPhoneNumber = new Xml.PersonPhoneNumber { Number = number }; Json.PhoneNumber jsonPhoneNumber = new Json.PhoneNumber { Number = number }; output.Write("Is this a mobile, home, or work phone? "); String type = input.ReadLine(); switch (type) { case "mobile": phoneNumber.Type = Person.Types.PhoneType.Mobile; xmlPhoneNumber.Type = Xml.PersonPhoneNumberType.Mobile; jsonPhoneNumber.Type = Json.PhoneType.Mobile; break; case "home": phoneNumber.Type = Person.Types.PhoneType.Home; xmlPhoneNumber.Type = Xml.PersonPhoneNumberType.Home; jsonPhoneNumber.Type = Json.PhoneType.Home; break; case "work": phoneNumber.Type = Person.Types.PhoneType.Work; xmlPhoneNumber.Type = Xml.PersonPhoneNumberType.Work; jsonPhoneNumber.Type = Json.PhoneType.Work; break; default: output.Write("Unknown phone type. Using default."); break; } person.Phones.Add(phoneNumber); Xml.PersonPhoneNumber[] newValues; if (xmlPerson.Phones != null) { newValues = new Xml.PersonPhoneNumber[xmlPerson.Phones.Length + 1]; Array.Copy(xmlPerson.Phones, newValues, xmlPerson.Phones.Length); newValues[xmlPerson.Phones.Length] = xmlPhoneNumber; } else { newValues = new Xml.PersonPhoneNumber[1]; newValues[0] = xmlPhoneNumber; } xmlPerson.Phones = newValues; if (jsonPerson.Phones == null) { jsonPerson.Phones = new System.Collections.ObjectModel.ObservableCollection <Json.PhoneNumber>(); } jsonPerson.Phones.Add(jsonPhoneNumber); } return(new object[] { person, xmlPerson, jsonPerson }); }