amphibia89

Zodiac

Apr 6th, 2016
167
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.84 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4.  
  5. namespace Zodiac
  6. {
  7.     public struct ZodiacSign
  8.     {
  9.         public string Name { get; set; }
  10.         public DateTime StartDate { get; set; }
  11.         public DateTime EndDate { get; set; }
  12.  
  13.         public ZodiacSign(string name, DateTime startDate, DateTime endDate)
  14.             : this()
  15.         {
  16.             Name = name;
  17.             StartDate = startDate;
  18.             EndDate = endDate;
  19.         }
  20.     }
  21.  
  22.     class Program
  23.     {
  24.         static readonly ZodiacSign[] ZodiacSigns = new[]
  25.         {
  26.             new ZodiacSign(
  27.                 "Sagittarius",
  28.                 new DateTime(2016, 11, 22),
  29.                 new DateTime(2016, 12, 21)),
  30.             new ZodiacSign(
  31.                 "Capricorn",
  32.                 new DateTime(2016, 12, 22),
  33.                 new DateTime(2016, 1, 19))
  34.         };
  35.  
  36.         const string DataFile = "../../dates.txt";
  37.  
  38.         static void Main(string[] args)
  39.         {
  40.             var dates = ReadDates(DataFile);
  41.  
  42.             foreach (var date in dates)
  43.             {
  44.                 PrintZodiacSign(date);
  45.             }
  46.         }
  47.  
  48.         private static void PrintZodiacSign(DateTime date)
  49.         {
  50.             var comparisonDate = new DateTime(2016, date.Month, date.Day);
  51.  
  52.             foreach (var zodiacSign in ZodiacSigns)
  53.             {
  54.                 if (zodiacSign.Name != "Capricorn")
  55.                 {
  56.                     if (comparisonDate >= zodiacSign.StartDate
  57.                         && comparisonDate <= zodiacSign.EndDate)
  58.                     {
  59.                         Console.WriteLine("{0} => {1}",
  60.                             date.ToShortDateString(),
  61.                             zodiacSign.Name);
  62.                     }
  63.                 }
  64.                 else
  65.                 {
  66.                     if (comparisonDate >= zodiacSign.StartDate
  67.                         || comparisonDate <= zodiacSign.EndDate)
  68.                     {
  69.                         Console.WriteLine("{0} => {1}",
  70.                             date.ToShortDateString(),
  71.                             zodiacSign.Name);
  72.                     }
  73.                 }
  74.             }
  75.         }
  76.  
  77.         private static IList<DateTime> ReadDates(string dataFile)
  78.         {
  79.             var dateList = new List<DateTime>();
  80.  
  81.             using (var reader = new StreamReader(dataFile))
  82.             {
  83.                 string line = reader.ReadLine();
  84.                 while (line != null)
  85.                 {
  86.                     DateTime date;
  87.                    
  88.                     if (DateTime.TryParse(line, out date))
  89.                     {
  90.                         dateList.Add(date);
  91.                     }
  92.  
  93.                     line = reader.ReadLine();
  94.                 }
  95.             }
  96.  
  97.             return dateList;
  98.         }
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment