svetlozar_kirkov

Holidays (Exercise)

Oct 8th, 2014
242
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 3.27 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. namespace DateThing
  5. {
  6.     class Dates
  7.     {
  8.         static void Main()
  9.         {
  10.             var holidays = new List<DateTime>()
  11.                                {
  12.                                    new DateTime(2014,01,01),
  13.                                    new DateTime(2014,03,03),
  14.                                    new DateTime(2014,04,18),
  15.                                    new DateTime(2014,04,19),
  16.                                    new DateTime(2014,04,20),
  17.                                    new DateTime(2014,05,01),
  18.                                    new DateTime(2014,05,06),
  19.                                    new DateTime(2014,05,02),
  20.                                    new DateTime(2014,05,05),
  21.                                    new DateTime(2014,05,24),
  22.                                    new DateTime(2014,09,06),
  23.                                    new DateTime(2014,09,22),
  24.                                    new DateTime(2014,11,01),
  25.                                    new DateTime(2014,12,24),
  26.                                    new DateTime(2014,12,25),
  27.                                    new DateTime(2014,12,26),
  28.                                    new DateTime(2014,12,31),
  29.                                };
  30.  
  31.             var workingSaturdays = new List<DateTime>()
  32.             {
  33.                 new DateTime(2014, 05, 10),
  34.                 new DateTime(2014, 05, 31),
  35.                 new DateTime(2014, 12, 13),
  36.  
  37.             };
  38.             Console.Write("From: ");
  39.             string firstDate = Console.ReadLine();
  40.             DateTime start = Convert.ToDateTime(firstDate);
  41.             Console.Write("To: ");
  42.             string finalDate = Console.ReadLine();
  43.             DateTime toDate = Convert.ToDateTime(finalDate);
  44.             int workingdayscount = 0;
  45.             int nonworkingdayscount = 0;
  46.             List<DateTime> nonworkingdayslist = new List<DateTime>();
  47.  
  48.             for (DateTime i = start; i <= toDate ; i=i.AddDays(1) )
  49.             {
  50.                 if (i.DayOfWeek == DayOfWeek.Saturday && workingSaturdays.Contains(i))
  51.                 {
  52.                     workingdayscount++;
  53.                 }
  54.                 else if (i.DayOfWeek != DayOfWeek.Sunday && i.DayOfWeek != DayOfWeek.Saturday &&
  55.                     holidays.Contains(i))
  56.                 {
  57.                     nonworkingdayscount++;
  58.                     nonworkingdayslist.Add(i);
  59.                 }
  60.                 else if (i.DayOfWeek == DayOfWeek.Sunday || i.DayOfWeek == DayOfWeek.Saturday)
  61.                 {
  62.                     nonworkingdayscount++;
  63.                     nonworkingdayslist.Add(i);
  64.                 }
  65.                 else
  66.                 {
  67.                     workingdayscount++;
  68.                 }
  69.  
  70.             }
  71.  
  72.             Console.WriteLine("From {0} to {1}:",start.ToString("d"),toDate.ToString("d"));
  73.             Console.WriteLine("Working days = {0}",workingdayscount);
  74.             Console.WriteLine("Non-working days = {0}",nonworkingdayscount);
  75.  
  76.             Console.WriteLine("\nHolidays:");
  77.             foreach (var holiday in nonworkingdayslist)
  78.             {
  79.                Console.WriteLine(holiday.ToString("d"));
  80.             }
  81.  
  82.             Console.ReadKey();
  83.         }
  84.  
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment