CountryCollection.cs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. using System;
  2. using System.Collections.Generic;
  3. namespace TestCode
  4. {
  5. public class CountryCollection
  6. {
  7. static List<Country> db;
  8. static CountryCollection()
  9. {
  10. db = new List<Country>();
  11. db.Add(new Country(1, "Poland", "Warsaw", 38116000.0));
  12. db.Add(new Country(2,"Portugal", "Lisbon", 10617575.0));
  13. db.Add(new Country(3,"Australia", "Canberra", 21468700.0));
  14. db.Add(new Country(4,"Austria", "Vienna", 8316487.0));
  15. db.Add(new Country(5,"Belgium", "Brussels", 10666866.0));
  16. db.Add(new Country(6,"Brazil", "Brasilia", 190132630.0));
  17. db.Add(new Country(7,"China", "Bejing", 1321000000.0));
  18. db.Add(new Country(8,"Chad", "N'Djamena", 10780600.0));
  19. db.Add(new Country(9,"Venezuela", "Caracas", 28199822.0));
  20. db.Add(new Country(10,"Vietnam", "Hanoi", 86116559.0));
  21. db.Add(new Country(11,"New Zealand", "Wellington", 4268000.0));
  22. db.Add(new Country(12,"Nigeria", "Abuja", 148000000.0));
  23. db.Add(new Country(13,"Oman", "Muscat", 2577000.0));
  24. db.Add(new Country(14,"Quatar", "Doha", 1450000.0));
  25. db.Add(new Country(15,"Denmark", "Copenhagen", 5505995.0));
  26. db.Add(new Country(16,"Dominican Republic", "Santo Domingo de Guzman", 9904000.0));
  27. db.Add(new Country(17,"France", "Paris", 64473140.0));
  28. db.Add(new Country(18,"United States of America", "Washington", 305619000.0));
  29. db.Add(new Country(19,"Latvia", "Riga", 2270700.0));
  30. }
  31. public List<Country> GetCountries(string sortExpression)
  32. {
  33. var ret = new List<Country>();
  34. ret.AddRange(db);
  35. ret.Sort(new CountryComparer(sortExpression));
  36. return ret;
  37. }
  38. public int Update(int id, string name, string capital, double population)
  39. {
  40. if (String.IsNullOrEmpty(name))
  41. return 0;
  42. int updated = 0;
  43. foreach (Country c in db)
  44. {
  45. if (c.ID != id)
  46. continue;
  47. updated++;
  48. c.Name = name;
  49. c.Capital = capital;
  50. c.Population = population;
  51. }
  52. return updated;
  53. }
  54. public int Insert(int id, string name, string capital, double population)
  55. {
  56. if (String.IsNullOrEmpty(name))
  57. return 0;
  58. db.Add(new Country(id, name, capital, population));
  59. return 1;
  60. }
  61. public int Delete(int id)
  62. {
  63. var toDelete = new List<Country> ();
  64. foreach (Country c in db)
  65. {
  66. if (c.ID != id)
  67. continue;
  68. toDelete.Add(c);
  69. }
  70. foreach (Country c in toDelete)
  71. db.Remove(c);
  72. return toDelete.Count;
  73. }
  74. }
  75. sealed class CountryComparer : IComparer<Country>
  76. {
  77. string sortProperty;
  78. bool descending;
  79. public CountryComparer(string sortExpression)
  80. {
  81. descending = sortExpression.ToLowerInvariant().EndsWith(" desc");
  82. if (descending)
  83. {
  84. sortProperty = sortExpression.Substring(0, sortExpression.Length - 5);
  85. }
  86. else
  87. {
  88. if (sortExpression.ToLowerInvariant().EndsWith(" asc"))
  89. sortProperty = sortExpression.Substring(0, sortExpression.Length - 4);
  90. else
  91. sortProperty = sortExpression;
  92. }
  93. }
  94. public int Compare(Country a, Country b)
  95. {
  96. int retVal = 0;
  97. switch (sortProperty)
  98. {
  99. case "Name":
  100. retVal = String.Compare(a.Name, b.Name, StringComparison.InvariantCultureIgnoreCase);
  101. break;
  102. case "Capital":
  103. retVal = String.Compare(a.Capital, b.Capital, StringComparison.InvariantCultureIgnoreCase);
  104. break;
  105. case "Population":
  106. retVal = (int)(a.Population - b.Population);
  107. break;
  108. }
  109. if (descending)
  110. return retVal;
  111. return retVal * -1;
  112. }
  113. }
  114. }