コード例 #1
0
        /// <summary>
        /// Builds a person based on user input
        /// </summary>
        private static Person PromptForAddress(TextReader input, TextWriter output)
        {
            Person person = new Person();

            output.Write("Enter person ID: ");
            person.Id = int.Parse(input.ReadLine());

            output.Write("Enter name: ");
            person.Name = input.ReadLine();

            output.Write("Enter email address (blank for none): ");
            string email = input.ReadLine();

            if (email.Length > 0)
            {
                person.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
                };

                output.Write("Is this a mobile, home, or work phone? ");
                String type = input.ReadLine();
                switch (type)
                {
                case "mobile":
                    phoneNumber.Type = Person.Types.PhoneType.Mobile;
                    break;

                case "home":
                    phoneNumber.Type = Person.Types.PhoneType.Home;
                    break;

                case "work":
                    phoneNumber.Type = Person.Types.PhoneType.Work;
                    break;

                default:
                    output.Write("Unknown phone type. Using default.");
                    break;
                }

                person.Phones.Add(phoneNumber);
            }
            return(person);
        }
コード例 #2
0
        /// <summary>
        /// Builds a new person
        /// </summary>
        private static Person GeneratePerson()
        {
            Person person = new Person();

            person.Id = LastProtoId;

            person.Name  = $"Name{LastProtoId}";
            person.Email = $"Email{LastProtoId}";
            Person.Types.PhoneNumber phoneNumber = new Person.Types.PhoneNumber {
                Number = $"mobile{LastProtoId}", Type = Person.Types.PhoneType.Mobile
            };
            person.Phones.Add(phoneNumber);
            phoneNumber = new Person.Types.PhoneNumber {
                Number = $"home{LastProtoId}", Type = Person.Types.PhoneType.Home
            };
            person.Phones.Add(phoneNumber);
            phoneNumber = new Person.Types.PhoneNumber {
                Number = $"work{LastProtoId}", Type = Person.Types.PhoneType.Work
            };
            person.Phones.Add(phoneNumber);
            LastProtoId++;
            return(person);
        }
コード例 #3
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 });
        }