Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 24th, 2012  |  syntax: None  |  size: 2.61 KB  |  hits: 18  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Compare() mehtod, Parent and Children objects sorting not perfect
  2. class DrinkComparer : IComparer<Drink>
  3.     {
  4.         public int Compare(Drink x, Drink y)
  5.         {
  6.             // AlcoholDrink class is children of Drink class
  7.             if (x is AlcoholDrink && y is AlcoholDrink)
  8.             {
  9.                 AlcoholDrink a = (AlcoholDrink)x;
  10.                 AlcoholDrink b = (AlcoholDrink)y;
  11.                 double aAlk = a.GetValueOfAlcohol;
  12.                 double bAlk = b.GetValueOfAlcohol;
  13.                 if (aAlk > bAlk)
  14.                     return -1;
  15.                 else if (aAlk < bAlk)
  16.                     return 1;
  17.                 else
  18.                     return 0;
  19.             }
  20.             // Drink objects haven't got GetValueOfAlcohol method...
  21.             // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
  22.             else
  23.                 return 1;                
  24.         }
  25.     }
  26.     static AlcoholDrink[] Largest3AlcoholDrink(Drink[] t)
  27.     {
  28.         Array.Sort(t, new DrinkComparer());
  29.         AlcoholDrink[] sz = new AlcoholDrink[3];
  30.         sz[0] = (AlcoholDrink)t[0];
  31.         sz[1] = (AlcoholDrink)t[1];
  32.         sz[2] = (AlcoholDrink)t[2];
  33.         return sz;
  34.     }
  35.         AlcoholDrink sz = new AlcoholDrink( "Kékfrankos" , "0.75 l", 1500, 4.5);
  36.         Console.WriteLine(sz);
  37.  
  38.         Drink[] t = new Drink[8];
  39.         t[0] = new AlcoholDrink("Kék Portói", "0.75 l", 1200, 20.5);
  40.         t[1] = new Drink("Tocsik", "0.75 l", 1100); // Non Alcohol Drink
  41.         t[2] = new AlcoholDrink("Tokaji Asszú", "0.75 l ", 1600, 14.5);
  42.         t[3] = new AlcoholDrink("Egri Bikavér", "0.75 l", 1500, 23.5);
  43.         t[4] = new Drink("Egri Szamóca", "0.75 l", 1100); // Non Alchol Drink
  44.         t[5] = new AlcoholDrink("Egri Merlot", "0.75 l", 1700, 18.5);
  45.         t[6] = new AlcoholDrink("Egri Medina", "0.75 l", 900, 16.5);
  46.         t[7] = new AlcoholDrink("Törley Talisman", "0.75 l", 750, 4.5);
  47.         Console.WriteLine(DrinkKeres( t, "Egri Bikavér"));
  48.  
  49.         Largest3AlcoholDrink(t);
  50.  
  51.         Console.ReadLine();
  52.        
  53. // Drink objects haven't got GetValueOfAlcohol method...
  54.         // How can I aim, the non AlcoholDrink objects (Drink objects) go to the end     array?
  55.         else
  56.             return 1;
  57.        
  58. else if (x is AlcoholicDrink) return -1;
  59. else if (y is AlcoholicDrink) return 1;
  60. else return 0;
  61.        
  62. public AlcoholDrink[] GetAlcoholicDrinks(Drink[] drinks) {
  63.   ArrayList alcoholDrinks = new ArrayList();
  64.   foreach (Drink drink in drinks) {
  65.     if (drink is AlcoholDrink) {
  66.       alcoholDrinks.Add(drink);
  67.     }
  68.   }
  69.   return alcoholDrinks.ToArray(typeof(AlcoholDrink)) as AlcoholDrink[];
  70. }