-Annie-

StrategyPattern

Jul 31st, 2016
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.97 KB | None | 0 0
  1. namespace StrategyPattern
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.  
  6.     public class Program
  7.     {
  8.         public static void Main()
  9.         {
  10.             SortedSet<Person> byAge = new SortedSet<Person>(new AgeComp());
  11.             SortedSet<Person> byName = new SortedSet<Person>(new NameComp());
  12.             int numbrOfPeople = int.Parse(Console.ReadLine());
  13.             for (int i = 0; i < numbrOfPeople; i++)
  14.             {
  15.                 string[] personDetails = Console.ReadLine().Split();
  16.                 var person = new Person(personDetails[0], int.Parse(personDetails[1]));
  17.                 byAge.Add(person);
  18.                 byName.Add(person);
  19.             }
  20.  
  21.             Console.WriteLine(string.Join(Environment.NewLine, byName));
  22.             Console.WriteLine(string.Join(Environment.NewLine, byAge));
  23.         }
  24.     }
  25.  
  26.     public class RandomComp : IComparer<int>
  27.     {
  28.         public int Compare(int x, int y)
  29.         {
  30.             return 0;
  31.         }
  32.     }
  33.  
  34.     public class NameComp : IComparer<Person>
  35.     {
  36.         public int Compare(Person x, Person y)
  37.         {
  38.             int result = x.name.Length.CompareTo(y.name.Length);
  39.             if (result == 0)
  40.             {
  41.                 string fisrtChar = x.name[0].ToString();
  42.                 string scondFirstChar = y.name[0].ToString();
  43.                 result = String.Compare(fisrtChar, scondFirstChar, false);
  44.             }
  45.  
  46.             return result;
  47.         }
  48.     }
  49.  
  50.     public class AgeComp : IComparer<Person>
  51.     {
  52.         public int Compare(Person x, Person y)
  53.         {
  54.             return x.age.CompareTo(y.age);
  55.         }
  56.     }
  57.  
  58.     public class Person
  59.     {
  60.         public string name;
  61.         public int age;
  62.  
  63.         public Person(string name, int age)
  64.         {
  65.             this.name = name;
  66.             this.age = age;
  67.         }
  68.  
  69.         public override string ToString()
  70.         {
  71.             return this.name + " " + this.age;
  72.         }
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment