Advertisement
StoyanGrigorov

CreatePeopleConstructors

Mar 1st, 2017
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.52 KB | None | 0 0
  1. private static void CreatePeopleConstructors()
  2.         {
  3.             Console.WriteLine("Enter person parameters or leave blank");
  4.             string input = Console.ReadLine();
  5.  
  6.             List<Person> persons = new List<Person>();
  7.  
  8.             while (input != "end")
  9.             {
  10.  
  11.                 string[] inputArgs = input.Split(new char[] { ',', ' ' }, StringSplitOptions.RemoveEmptyEntries);
  12.  
  13.                 if (inputArgs.Length == 0)
  14.                 {
  15.                     persons.Add(new Person());
  16.                 }
  17.                 else if (inputArgs.Length == 1)
  18.                 {
  19.  
  20.                     int n;
  21.                     bool isNumeric = int.TryParse(inputArgs[0], out n);
  22.  
  23.                     if (isNumeric)
  24.                     {
  25.                         persons.Add(new Person(n));
  26.                     }
  27.                     else
  28.                     {
  29.                         persons.Add(new Person(inputArgs[0]));
  30.                     }
  31.  
  32.                 }
  33.                 else
  34.                 {
  35.                     string name = inputArgs[0];
  36.  
  37.                     int age = int.Parse(inputArgs[1]);
  38.  
  39.                     persons.Add(new Person { Name = name, Age = age });
  40.                 }
  41.  
  42.                 Console.WriteLine("Enter *end* to stop adding and print out the results or add more info.");
  43.                 input = Console.ReadLine();
  44.             }
  45.  
  46.             foreach (var p in persons)
  47.             {
  48.                 Console.WriteLine($"{p.Name} {p.Age}");
  49.             }
  50.         }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement