Guest User

HV Test 2

a guest
Mar 14th, 2012
62
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. namespace HVTest
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.  
  7.     class Program
  8.     {
  9.         static void Main(string[] args)
  10.         {
  11.             var manipulator = new WeatherManipulator();
  12.             manipulator.Run();
  13.         }
  14.     }
  15.  
  16.     class WeatherManipulator
  17.     {
  18.         private IEnumerable<Weather> collection;
  19.  
  20.         public void Run()
  21.         {
  22.             collection = this.GenerateData();
  23.             collection = collection.DateIs(DateTime.Now).CityIs("Tartu").ToList();
  24.             foreach(var item in collection)
  25.                 Console.WriteLine(item.ToString());
  26.  
  27.             Console.ReadKey();
  28.         }
  29.  
  30.         private List<Weather> GenerateData()
  31.         {
  32.             Random random = new Random();
  33.             DateTime start = new DateTime(2012, 1, 1);
  34.             DateTime end = new DateTime(2012, 4, 1).AddDays(-1);
  35.             IEnumerable<City> cities = Enum.GetValues(typeof(City)).Cast<City>();
  36.  
  37.             List<Weather> result = new List<Weather>();
  38.  
  39.             foreach(var city in cities)
  40.             {
  41.                 for (DateTime date = start; date <= end; date = date.AddDays(1)) result.Add(new Weather() { City = city, Date = date, Temperature = random.Next(-16, 3) });
  42.             }
  43.             return result;
  44.         }
  45.     }
  46.  
  47.     static class WeatherExtensions
  48.     {
  49.         public static IEnumerable<Weather> DateIs(this IEnumerable<Weather> collection, DateTime date)
  50.         {
  51.             foreach(var item in collection)
  52.             {
  53.                 if (item.Date.Date == date.Date) yield return item;
  54.             }
  55.         }
  56.  
  57.         public static IEnumerable<Weather> DateIsLINQ(this IEnumerable<Weather> collection, DateTime date)
  58.         {
  59.             return collection.Where(item => item.Date.Date == date.Date);
  60.         }
  61.  
  62.         public static IEnumerable<Weather> CityIs(this IEnumerable<Weather> collection, string city)
  63.         {
  64.             foreach(var item in collection)
  65.             {
  66.                 if (item.City == (City)Enum.Parse(typeof(City), city, true)) yield return item;
  67.             }
  68.         }
  69.  
  70.         public static IEnumerable<Weather> CityIsLINQ(this IEnumerable<Weather> collection, string city)
  71.         {
  72.             return collection.Where(item => item.City == (City)Enum.Parse(typeof(City), city, true));
  73.         }
  74.     }
  75.  
  76.  
  77.     class Weather
  78.     {
  79.         public DateTime Date { get; set; }
  80.         public City City { get; set; }
  81.         public int Temperature { get; set; }
  82.  
  83.         public override string ToString()
  84.         {
  85.             return String.Format("City: {0}, Date: {1}, Temperature: {2}", City, Date, Temperature);
  86.         }
  87.     }
  88.  
  89.     public enum City { Jõhvi, Narva, Tartu, Tallinn }
  90.  
  91. }
Advertisement
Add Comment
Please, Sign In to add comment