Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/C#/TollCalculatorTests/TollCalculatorTests/TollCalculatorTests/obj/Debug/netcoreapp3.1
/C#/TollCalculator/TollCalculator/obj
/C#/TollCalculator/.vs/TollCalculator/v16
/C#/TollCalculator/App/obj
/C#/TollCalculator/App/bin/Debug/netcoreapp3.1
/C#/TollCalculator/.vs/TollCalculator/DesignTimeBuild
/C#/TollCalculator/TollCalculatorTests/bin/Debug/netcoreapp3.1
/C#/TollCalculator/TollCalculatorTests/obj
16 changes: 0 additions & 16 deletions C#/Car.cs

This file was deleted.

16 changes: 0 additions & 16 deletions C#/Motorbike.cs

This file was deleted.

108 changes: 0 additions & 108 deletions C#/TollCalculator.cs

This file was deleted.

12 changes: 12 additions & 0 deletions C#/TollCalculator/App/Car.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using TollCalculatorApp;

namespace TollFeeCalculator
{
public class Car : IVehicle
{
public VehicleType GetVehicleType()
{
return VehicleType.Car;
}
}
}
34 changes: 34 additions & 0 deletions C#/TollCalculator/App/Extensions/DateTimeExtension.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace TollCalculatorApp
{
public static class DateTimeExtension
{

/// <summary>
/// Determines if date is between two Timespans representing time of day
/// </summary>
public static bool IsBetweenTimes(this DateTime date, TimeSpan start, TimeSpan end)
{
if(start <= end)
return date.TimeOfDay >= start && date.TimeOfDay < end;
else
throw new ArgumentException("End must be later than start");
}

/// <summary>
/// This method returns wether a datetime is a weekend day or not
/// </summary>
public static bool IsWeekend(this DateTime date)
{
if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday)
return true;

return false;
}
}
}

9 changes: 9 additions & 0 deletions C#/TollCalculator/App/IVehicle.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
using TollCalculatorApp;

namespace TollFeeCalculator
{
public interface IVehicle
{
VehicleType GetVehicleType();
}
}
13 changes: 13 additions & 0 deletions C#/TollCalculator/App/Motorbike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@

using TollCalculatorApp;

namespace TollFeeCalculator
{
public class Motorbike : IVehicle
{
public VehicleType GetVehicleType()
{
return VehicleType.Motorbike;
}
}
}
13 changes: 13 additions & 0 deletions C#/TollCalculator/App/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using System;

namespace TollFeeCalculator
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Hello World!");

}
}
}
11 changes: 11 additions & 0 deletions C#/TollCalculator/App/Services/Interfaces/IHolidayService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;
using System.Text;

namespace TollCalculatorApp.Services.Interfaces
{
public interface IHolidayService
{
bool IsHoliday(DateTime date);
}
}
139 changes: 139 additions & 0 deletions C#/TollCalculator/App/Services/SwedishHolidayService.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
using System;
using System.Collections.Generic;
using System.Linq;
using TollCalculatorApp.Services.Interfaces;

namespace TollCalculatorApp.Services
{

public class SwedishHolidayService : IHolidayService
{

/// <summary>
/// Determines if date is the same day as any of the swedish bank-holidays
/// </summary>
public bool IsHoliday(DateTime date)
{
var holidays = GetSwedishHolidays(date.Year);

if (holidays.Any(holiday => holiday.Month == date.Month && holiday.Day == date.Day))
return true;

return false;
}

/// <summary>
/// This method returns the swedish bank holidays
/// </summary>
/// <param name="year">The year you want days to be generated for</param>
/// <returns>List of DateTimes where swedish holidays occur</returns>
///
/// *I assumed that the "holidays" in the description in this repository meant bank-holidays
/// Used the following list of holidays https://www.riksbank.se/en-gb/press-and-published/calendar/bank-holidays-2021/
private List<DateTime> GetSwedishHolidays(int year)
{
//Holidays that are in terms of date of month - they occur at the same date of a month each year.
var Holidays = new List<DateTime>() {
new DateTime(year, 1, 1), //New years day
new DateTime(year, 1, 6), //Epiphany
new DateTime(year, 5,1), //First of May
new DateTime(year, 6,6), //Swedish national day
new DateTime(year, 12,24), //Christmas eve
new DateTime(year, 12,25), //Chirstmas day
new DateTime(year, 12,26), //Boxing day
new DateTime(year, 12,31) //New years eve
};

//Holidays that are not the same date of month each year
var calculatedHolidays = new List<DateTime>();
calculatedHolidays.Add(GetEasterDay(year).AddDays(-2)); //Good Friday
calculatedHolidays.Add(GetEasterDay(year).AddDays(-1)); //Easter Eve
calculatedHolidays.Add(GetEasterDay(year)); // Easter Sunday
calculatedHolidays.Add(GetEasterDay(year).AddDays(1)); // Easter Monday
calculatedHolidays.Add(GetMidsummerDay(year).AddDays(-1)); //Midsummers eve
calculatedHolidays.Add(GetMidsummerDay(year));
calculatedHolidays.Add(GetAllSaintsDay(year));
calculatedHolidays.Add(GetAscensionDay(year));
calculatedHolidays.Add(GetPentecostDay(year));
calculatedHolidays.Add(GetPentecostDay(year).AddDays(-1)); //Whitsun

var swedishHolidays = new List<DateTime>();
swedishHolidays.AddRange(Holidays);
swedishHolidays.AddRange(calculatedHolidays);

return swedishHolidays;
}



//This method uses Gauss algorithm to calculate easter day
//The algorithm used here was copied from following stack-exchange article: https://codereview.stackexchange.com/q/193847
/// <summary>
/// This method calculates the day that Easter day occur on
/// </summary>
/// <param name="year">The year you want to get the date of Easter day for</param>
/// <returns>DateTime that Easter day occur on</returns>
private DateTime GetEasterDay(int year)
{
int a = year % 19;
int b = year / 100;
int c = (b - (b / 4) - ((8 * b + 13) / 25) + (19 * a) + 15) % 30;
int d = c - (c / 28) * (1 - (c / 28) * (29 / (c + 1)) * ((21 - a) / 11));
int e = d - ((year + (year / 4) + d + 2 - b + (b / 4)) % 7);
int month = 3 + ((e + 40) / 44);
int day = e + 28 - (31 * (month / 4));
return new DateTime(year, month, day);
}

/// <summary>
/// This method calculates the day that All saints day occur on
/// </summary>
/// <param name="year">The year you want to get the date of all saints day for</param>
/// <returns>DateTime that all saints day occur on</returns>
private DateTime GetAllSaintsDay(int year)
{
var date = new DateTime(year, 10, 31);
while (date.DayOfWeek != DayOfWeek.Saturday)
{
date = date.AddDays(1);
}
return date;
}

/// <summary>
/// This method calculates the day that All saints day occur on
/// </summary>
/// <param name="year">The year you want to get the date of All saints day for</param>
/// <returns>DateTime that ascension day occur on</returns>
private DateTime GetAscensionDay(int year)
{
return GetEasterDay(year).AddDays(39);
}

/// <summary>
/// This method calculates the day that Pentecost occur on
/// </summary>
/// <param name="year">The year you want to get the date of Pentecost for</param>
/// <returns>DateTime that Pentecost occur on</returns>
private DateTime GetPentecostDay(int year)
{
return GetEasterDay(year).AddDays(49);
}

/// <summary>
/// This method calculates the day that Midsummer day occur on
/// </summary>
/// <param name="year">The year you want to get the date of Midsummer day for</param>
/// <returns>DateTime that Midsummer day occur on</returns>
private DateTime GetMidsummerDay(int year)
{
var date = new DateTime(year, 6, 20);
while (date.DayOfWeek != DayOfWeek.Saturday)
{
date = date.AddDays(1);
}

return date;
}
}
}
Loading