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