Posts Tagged ‘tasks’

A fast and beautiful solution.  🙂

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkDays
{
class WorkDays
{
private static DateTime[] holidays = { new DateTime(2012, 12, 24), new DateTime(2012, 12, 25), new DateTime(2012, 12, 26),
new DateTime(2012, 12, 30), new DateTime(2012, 12, 31), new DateTime(2013, 01, 01) };

static void Main(string[] args)
{
// 5. Write a method that calculates the number of workdays between today and given date, passed as parameter.
// Consider that workdays are all days from Monday to Friday except a fixed list of public holidays
// specified preliminary as array.

Console.WriteLine(“Method that calculates the number of workdays between today and given date, passed as parameter.  \n\n”);

Console.WriteLine(“What is the past date:”);
DateTime StartDate = DateTime.Parse(Console.ReadLine());

int nWorkingDays = CalculateWorkDays(StartDate);
if (nWorkingDays > 0)
Console.WriteLine(“Number of work days is {0}”, nWorkingDays);
else Console.WriteLine(“Invlid dates!”);

}

private static int CalculateWorkDays(DateTime StartDate)
{
DateTime TodayDate = DateTime.Now;
int nWorkingDays = 0;

if (TodayDate.Date > StartDate.Date)
{
DateTime day = StartDate.Date;
while (day <= TodayDate.Date)
{
if (day.DayOfWeek != DayOfWeek.Saturday && day.DayOfWeek != DayOfWeek.Sunday
&& !holidays.Contains(day))
{
nWorkingDays++;
}

day = day.AddDays(1);
}
}

return nWorkingDays;
}

}
}

Advertisements