Advertisement
vlad0

ClassesAndObjects 5

Jan 16th, 2013
469
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.49 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace _05.WorkingDays
  7. {
  8.     class WorkingDays
  9.     {
  10.         static void Main(string[] args)
  11.         {
  12.             var holidays = new List<DateTime>();
  13.  
  14.             holidays.Add(new DateTime(2013, 01, 30));
  15.             holidays.Add(new DateTime(2013, 01, 31));
  16.             holidays.Add(new DateTime(2013, 02, 05));
  17.             holidays.Add(new DateTime(2013, 02, 07));
  18.  
  19.             DateTime now = DateTime.Today;
  20.             Console.Write("Enter final date<YYYY-MM-DD>: ");
  21.             DateTime final = DateTime.Parse(Console.ReadLine());
  22.             CheckWorkingDates(now, final, holidays);
  23.  
  24.         }
  25.  
  26.        
  27.  
  28.         private static void CheckWorkingDates(DateTime now, DateTime final, List<DateTime> holidays)
  29.         {
  30.             //got the length between without weekends or holidays
  31.             int lengthDays = (final - now).Days;
  32.             int length = lengthDays;
  33.             DateTime currentDate = new DateTime();
  34.             //check which dates of the period are weekends or holidays
  35.             //and the crease the length
  36.             //we pass through the whole period
  37.             for (int i = 0; i <= length; i++)
  38.             {
  39.                 //increase date
  40.                 currentDate = now.AddDays(i);
  41.                 //we compare the currentDate with every single holiday
  42.                 for (int days = 0; days < holidays.Count; days++)
  43.                 {
  44.                     int comparison = currentDate.CompareTo(holidays[days]); //if match return 0
  45.                     if (comparison == 0)
  46.                     {
  47.                         //decrease the length ot period
  48.                         lengthDays-- ;
  49.                     }
  50.                 }
  51.                 //check is the current day in weekend
  52.                 if (currentDate.DayOfWeek == DayOfWeek.Saturday || currentDate.DayOfWeek == DayOfWeek.Sunday)
  53.                 {
  54.                     //decrease the length ot period
  55.                     lengthDays--;
  56.                 }
  57.             }
  58.  
  59.             PrintResult(now,final,lengthDays);
  60.            
  61.         }
  62.  
  63.         private static void PrintResult(DateTime now, DateTime final, int lengthDays)
  64.         {
  65.             Console.WriteLine("Today is: {0:D}", now);
  66.             Console.WriteLine("Final Date is: {0:D}", final);
  67.             Console.WriteLine();
  68.             Console.WriteLine("Working days: {0}\n", lengthDays);
  69.         }
  70.  
  71.        
  72.  
  73.     }
  74. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement