Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DeveloperTest
  5. {
  6. class Program
  7. {
  8. static void Main(string[] args)
  9. {
  10. var users = new List<User>();
  11.  
  12. users.Add(new User("John Smith", "42", DateTime.Today));
  13.  
  14. string name = "Jane Smith";
  15. int age = 37;
  16. int yearJoined = 17;
  17. int monthJoined = 01;
  18. int dayJoined = 15;
  19. users.Add(new User()
  20. {
  21. Name = name,
  22. Age = age,
  23. DateJoined = new DateTime(day: dayJoined, month: monthJoined, year: yearJoined)
  24. });
  25.  
  26. users[0].PrintUserInfo();
  27.  
  28. RemoveUsersUnderAge(users, 40);
  29.  
  30. Console.ReadKey();
  31. }
  32.  
  33. private void RemoveUsersUnderAge(List<User> users, int age)
  34. {
  35. foreach (var user in users)
  36. {
  37. if (user.Age < age)
  38. {
  39. users.Remove(user);
  40. }
  41. }
  42. }
  43. }
  44.  
  45. class User
  46. {
  47. private string _name;
  48. private int _age;
  49. private DateTime _dateJoined;
  50.  
  51. public User()
  52. {
  53.  
  54. }
  55.  
  56. public User(string name, string age, DateTime dateJoined)
  57. {
  58. if (name == null) Name = "Unknown";
  59. if (age == null) Age = 0;
  60. if (dateJoined == null) DateJoined = DateTime.Today;
  61.  
  62. Name = name;
  63. Age = Convert.ToInt32(age);
  64. }
  65.  
  66. public string Name
  67. {
  68. get { return _name; }
  69. set { _name = value; }
  70. }
  71.  
  72. public int Age
  73. {
  74. get { return _age; }
  75. set { _age = value; }
  76. }
  77.  
  78. public DateTime DateJoined
  79. {
  80. get { return _dateJoined; }
  81. set { _dateJoined = value; }
  82. }
  83.  
  84. private void PrintUserInfo()
  85. {
  86. Console.WriteLine($"Name: {this.Name}.");
  87. Console.WriteLine($"Age: {this.Age} years old.");
  88. Console.WriteLine($"Joined: {this.DateJoined}.");
  89. }
  90. }
  91. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement