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
37 changes: 37 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
*.swp
*.*~
project.lock.json
.DS_Store
*.pyc
nupkg/

# Visual Studio Code
.vscode

# Rider
.idea

# User-specific files
*.suo
*.user
*.userosscache
*.sln.docstates

# Build results
[Dd]ebug/
[Dd]ebugPublic/
[Rr]elease/
[Rr]eleases/
x64/
x86/
build/
bld/
[Bb]in/
[Oo]bj/
[Oo]ut/
msbuild.log
msbuild.err
msbuild.wrn

# Visual Studio 2015
.vs/
16 changes: 0 additions & 16 deletions C#/Motorbike.cs

This file was deleted.

20 changes: 20 additions & 0 deletions C#/Toll-calc/Tests/Tests.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>netcoreapp3.1</TargetFramework>

<IsPackable>false</IsPackable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.5.0" />
<PackageReference Include="xunit" Version="2.4.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.0" />
<PackageReference Include="coverlet.collector" Version="1.2.0" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\Toll-calc\Toll-calc.csproj" />
</ItemGroup>

</Project>
196 changes: 196 additions & 0 deletions C#/Toll-calc/Tests/UnitTest1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,196 @@
using System;
using Toll_calc.Models;
using Toll_calc.Services;
using Xunit;

namespace Tests
{
public class UnitTest1
{

private readonly TollCalculator _calculator;
public UnitTest1()
{
var holidayService = new HolidayService();
_calculator = new TollCalculator(holidayService);
}

[Fact]
public void Car_Three_Passes_Spread_Out_Weekday()
{
var passTimes = new DateTime[]
{
new DateTime(2021, 9, 1,08,30,0),
new DateTime(2021, 9, 1, 06,15,00),
new DateTime(2021, 9, 1, 16,15,00),
};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(34, totalSum);
}

[Fact]
public void Toll_Free_Vehicle()
{
var passTimes = new DateTime[]
{
new DateTime(2021, 9, 1,08,30,0),
new DateTime(2021, 9, 1, 06,15,00),
new DateTime(2021, 9, 1, 16,15,00),
};
var totalSum = _calculator.GetTollFeeForDay(new Diplomat(), passTimes);
Assert.Equal(0, totalSum);
}

[Fact]
public void Saturday()
{
var passTimes = new DateTime[]
{
new DateTime(2021, 9, 4,08,30,0),
new DateTime(2021, 9, 4, 06,15,00),
new DateTime(2021, 9, 4, 16,15,00),
};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(0, totalSum);
}

[Fact]
public void Weekends_Should_Be_Free()
{
var saturday = new DateTime[]
{
new DateTime(2021, 9, 4,08,30,0),
new DateTime(2021, 9, 4, 06,15,00),
new DateTime(2021, 9, 4, 16,15,00),
};
var saturdaySum = _calculator.GetTollFeeForDay(new Car(), saturday);
Assert.Equal(0, saturdaySum);

var sunday = new DateTime[]
{
new DateTime(2021, 9, 5,08,30,0),
new DateTime(2021, 9, 5, 06,15,00),
new DateTime(2021, 9, 5, 16,15,00),
};
var sundaySum = _calculator.GetTollFeeForDay(new Car(), sunday);
Assert.Equal(0, sundaySum);
}

[Fact]
public void Holidays_Should_Be_Free()
{
//friday
var passTimes = new DateTime[]
{
new DateTime(2021, 12, 24,08,30,0),
new DateTime(2021, 12, 24, 06,15,00),
new DateTime(2021, 12, 24, 16,15,00),
};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(0, totalSum);
}

[Fact]
public void Multiple_Passes_Within_An_Hour_Should_Only_Count_Highest()
{
//friday
var passTimes = new DateTime[]
{
new DateTime(2021, 09, 1,06,55,0), // 13 - overwritten by line 3
new DateTime(2021, 09, 1, 06,15,00), // 8 - overwritten by line 3
new DateTime(2021, 09, 1, 7,13,00), // 18 - counted
new DateTime(2021, 09, 1, 7,16,00), // 18 - counted

};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(36, totalSum);
}

[Fact]
public void Multiple_Passes_Within_An_Hour_Should_Only_Count_Highest2()
{
//friday
var passTimes = new DateTime[]
{
new DateTime(2021, 09, 1,06,55,0), // 13 - overwritten by line 3
new DateTime(2021, 09, 1, 06,15,00), // 8 - overwritten by line 3
new DateTime(2021, 09, 1, 7,13,00), // 18 - counted
new DateTime(2021, 09, 1, 7,16,00), // 18 - counted
new DateTime(2021, 09, 1, 8,12,00), // 13 - skipped since previous row is start of a new hour interval and has a higher fee

};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(36, totalSum);
}


[Fact]
public void Maximum_Fee_Cannot_Exceed_60()
{
//friday
var passTimes = new DateTime[]
{
//interval 1
new DateTime(2021, 09, 1, 06,55,0), // 13 - overwritten by line 3
new DateTime(2021, 09, 1, 06,15,00), // 8 - overwritten by line 3
new DateTime(2021, 09, 1, 07,13,00), // 18 - counted

//interval 2
new DateTime(2021, 09, 1, 07,16,00), // 18 - counted
new DateTime(2021, 09, 1, 08,12,00), // 13 - skipped since previous row is start of a new hour interval and has a higher fee

//interval 3
new DateTime(2021, 09, 1, 11,12,00), // 8 - counted

//interval 4
new DateTime(2021, 09, 1, 15,12,00), // 13 - overwritten
new DateTime(2021, 09, 1, 16,00,00), // 18 - counted

//sum = 62
};
var totalSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(60, totalSum);
}


[Fact]
public void NullVehicle_ThrowsArgumentException()
{
var passTimes = new DateTime[]
{
new DateTime(2021, 09, 1, 06, 55, 0)
};

var ex = Assert.Throws<ArgumentException>(() => _calculator.GetTollFeeForDay(null, passTimes));
Assert.Equal("Vehicle can not be null.", ex.Message);

}

[Fact]
public void Pass_Times_With_Different_Dates_ThrowsArgumentException()
{
var passTimes = new DateTime[]
{
new DateTime(2021, 09, 1, 06, 55, 0),
new DateTime(2021, 08, 1, 06, 55, 0)
};

var ex = Assert.Throws<ArgumentException>(() => _calculator.GetTollFeeForDay(new Car(), passTimes));
Assert.Equal("All pass times must be from the same date.", ex.Message);
}

[Fact]
public void Null_Or_Empty_Pass_Times_returns_0()
{
var passTimes = new DateTime[]
{
};

var zeroLengthSum = _calculator.GetTollFeeForDay(new Car(), passTimes);
Assert.Equal(0, zeroLengthSum);

var nullTimesSum = _calculator.GetTollFeeForDay(new Car(), null);
Assert.Equal(0, nullTimesSum);
}
}
}
31 changes: 31 additions & 0 deletions C#/Toll-calc/Toll-calc.sln
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@

Microsoft Visual Studio Solution File, Format Version 12.00
# Visual Studio Version 16
VisualStudioVersion = 16.0.30717.126
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Toll-calc", "Toll-calc\Toll-calc.csproj", "{087AFB44-3A49-4A5F-B905-9BEABFA8C08E}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Tests", "Tests\Tests.csproj", "{0FC12320-1CDF-403A-A857-F3948402B420}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{087AFB44-3A49-4A5F-B905-9BEABFA8C08E}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{087AFB44-3A49-4A5F-B905-9BEABFA8C08E}.Debug|Any CPU.Build.0 = Debug|Any CPU
{087AFB44-3A49-4A5F-B905-9BEABFA8C08E}.Release|Any CPU.ActiveCfg = Release|Any CPU
{087AFB44-3A49-4A5F-B905-9BEABFA8C08E}.Release|Any CPU.Build.0 = Release|Any CPU
{0FC12320-1CDF-403A-A857-F3948402B420}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{0FC12320-1CDF-403A-A857-F3948402B420}.Debug|Any CPU.Build.0 = Debug|Any CPU
{0FC12320-1CDF-403A-A857-F3948402B420}.Release|Any CPU.ActiveCfg = Release|Any CPU
{0FC12320-1CDF-403A-A857-F3948402B420}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {83341090-C237-4058-809D-F46C43901EAC}
EndGlobalSection
EndGlobal
17 changes: 17 additions & 0 deletions C#/Toll-calc/Toll-calc/Extensions/DateTimeExtensions.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Reflection.Metadata.Ecma335;

namespace Toll_calc.Extensions
{
public static class DateTimeExtensions
{
public static bool FallsBetween(this DateTime date, TimeSpan start, TimeSpan end)
{
var time = new TimeSpan(date.Hour, date.Minute, date.Second);
return start <= time && time < end;
}
}
}
11 changes: 6 additions & 5 deletions C#/Car.cs → C#/Toll-calc/Toll-calc/Models/Car.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,17 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace TollFeeCalculator
namespace Toll_calc.Models
{
public class Car : Vehicle
{
public String GetVehicleType()
{
return "Car";
}

public bool IsTollFree()
{
return false;
}
}
}
15 changes: 15 additions & 0 deletions C#/Toll-calc/Toll-calc/Models/Diplomat.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Toll_calc.Models
{
public class Diplomat : Vehicle
{
public string GetVehicleType()
{
return "Diplomat";
}

public bool IsTollFree()
{
return true;
}
}
}
15 changes: 15 additions & 0 deletions C#/Toll-calc/Toll-calc/Models/Emergency.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Toll_calc.Models
{
public class Emergency : Vehicle
{
public string GetVehicleType()
{
return "Emergency";
}

public bool IsTollFree()
{
return true;
}
}
}
15 changes: 15 additions & 0 deletions C#/Toll-calc/Toll-calc/Models/Foreign.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace Toll_calc.Models
{
public class Foreign : Vehicle
{
public string GetVehicleType()
{
return "Foreign";
}

public bool IsTollFree()
{
return true;
}
}
}
Loading