/// <summary>
 /// Builds a new person
 /// </summary>
 private static Json.Person GenerateJsonPerson()
 {
     Json.Person person = new Json.Person();
     person.Id     = LastJsonId;
     person.Name   = $"Name{LastJsonId}";
     person.Email  = $"Email{LastJsonId}";
     person.Phones = new System.Collections.ObjectModel.ObservableCollection <Json.PhoneNumber>();
     person.Phones.Add(new Json.PhoneNumber {
         Number = $"mobile{LastJsonId}", Type = Json.PhoneType.Mobile
     });
     person.Phones.Add(new Json.PhoneNumber {
         Number = $"home{LastJsonId}", Type = Json.PhoneType.Home
     });
     person.Phones.Add(new Json.PhoneNumber {
         Number = $"work{LastJsonId}", Type = Json.PhoneType.Work
     });
     LastJsonId++;
     return(person);
 }
예제 #2
0
        /// <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 });
        }