Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TollFeeCalculator NewSolution #83

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
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
11 changes: 11 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
################################################################################
# This .gitignore file was automatically created by Microsoft(R) Visual Studio.
################################################################################

/.vs/TollFeeCalculator/v16
/TollFeeCalculator/bin/Debug
/TollFeeCalculator/obj/Debug
/TollFeeCalculator/Properties
/packages
/UnitTestToll/bin/Debug
/UnitTestToll/obj/Debug
31 changes: 31 additions & 0 deletions TollFeeCalculator.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.31424.327
MinimumVisualStudioVersion = 10.0.40219.1
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "TollFeeCalculator", "TollFeeCalculator\TollFeeCalculator.csproj", "{67E17081-A80F-496E-BD4E-3F9ADBC04A5F}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "UnitTestToll", "UnitTestToll\UnitTestToll.csproj", "{223B14E2-C898-47A6-BDCB-D71F06194EBE}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Release|Any CPU = Release|Any CPU
EndGlobalSection
GlobalSection(ProjectConfigurationPlatforms) = postSolution
{67E17081-A80F-496E-BD4E-3F9ADBC04A5F}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{67E17081-A80F-496E-BD4E-3F9ADBC04A5F}.Debug|Any CPU.Build.0 = Debug|Any CPU
{67E17081-A80F-496E-BD4E-3F9ADBC04A5F}.Release|Any CPU.ActiveCfg = Release|Any CPU
{67E17081-A80F-496E-BD4E-3F9ADBC04A5F}.Release|Any CPU.Build.0 = Release|Any CPU
{223B14E2-C898-47A6-BDCB-D71F06194EBE}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{223B14E2-C898-47A6-BDCB-D71F06194EBE}.Debug|Any CPU.Build.0 = Debug|Any CPU
{223B14E2-C898-47A6-BDCB-D71F06194EBE}.Release|Any CPU.ActiveCfg = Release|Any CPU
{223B14E2-C898-47A6-BDCB-D71F06194EBE}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
GlobalSection(SolutionProperties) = preSolution
HideSolutionNode = FALSE
EndGlobalSection
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {3D1F5E48-4781-434B-BA20-DDF7E4B4EAFA}
EndGlobalSection
EndGlobal
6 changes: 6 additions & 0 deletions TollFeeCalculator/App.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="utf-8" ?>
<configuration>
<startup>
<supportedRuntime version="v4.0" sku=".NETFramework,Version=v4.5" />
</startup>
</configuration>
10 changes: 10 additions & 0 deletions TollFeeCalculator/Motorbike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
namespace TollFeeCalculator
{
public class Motorbike : Vehicle
{
public string GetVehicleType()
{
return "Motorbike";
}
}
}
27 changes: 27 additions & 0 deletions TollFeeCalculator/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
using System;

namespace TollFeeCalculator
{
class Program
{
static void Main(string[] args)
{
// build holidays
Toll.Builder builder = new Toll.Build();
builder.BuildHollidays();

//Initialize vehicle type
Toll.Motorbike motorbike1 = new Toll.Motorbike();
Toll.Tesla tesla = new Toll.Tesla();

Toll.TollCalculator calculatorTesla = new Toll.TollCalculator(motorbike1,12,00,13,00,DateTime.Now, builder.GetResult());
Console.WriteLine($"Toll Fee: { calculatorTesla.GetTollFee()} - Vehicle is FeeFree: {calculatorTesla.IsFeeFree(DateTime.Now)}");


Toll.TollCalculator calculatorMotorBike = new Toll.TollCalculator(tesla, 12, 00, 13, 00, DateTime.Now, builder.GetResult());
Console.WriteLine($"Toll Fee: { calculatorMotorBike.GetTollFee()} - Vehicle is eeFree: {calculatorMotorBike.IsFeeFree(DateTime.Now)}");

Console.ReadLine();
}
}
}
25 changes: 25 additions & 0 deletions TollFeeCalculator/Toll/Build.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
using System;
using System.Collections.Generic;

namespace TollFeeCalculator.Toll
{
public class Build : Builder
{
private List<DateTime> HolidayDates = new List<DateTime>();


public override void BuildHollidays()
{
HolidayDates.Add(new DateTime(2021, 11, 01));
HolidayDates.Add(new DateTime(2021, 12, 24));
HolidayDates.Add(new DateTime(2021, 12, 25));
HolidayDates.Add(new DateTime(2021, 12, 26));
HolidayDates.Add(new DateTime(2021, 12, 27));
}

public override List<DateTime> GetResult()
{
return HolidayDates;
}
}
}
11 changes: 11 additions & 0 deletions TollFeeCalculator/Toll/Builder.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
using System;
using System.Collections.Generic;

namespace TollFeeCalculator.Toll
{
public abstract class Builder
{
public abstract void BuildHollidays();
public abstract List<DateTime> GetResult();
}
}
16 changes: 16 additions & 0 deletions TollFeeCalculator/Toll/Enums.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
namespace TollFeeCalculator.Toll
{
public class Enums
{
public enum TollFreeVehicle
{
None,//Type for vehicles which will not be feee free
Motorbike,
Tractor,
Emergency,
Diplomat,
Foreign,
Military,
}
}
}
8 changes: 8 additions & 0 deletions TollFeeCalculator/Toll/IVehicleType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TollFeeCalculator.Toll
{
public interface IVehicleType
{
bool IsFeeFree(object t);
Enums.TollFreeVehicle GetVehicleType(object t);
}
}
8 changes: 8 additions & 0 deletions TollFeeCalculator/Toll/Motorbike.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TollFeeCalculator.Toll
{
public class Motorbike:VehicleType
{
public bool FeeFree=>IsFeeFree(this);
public Enums.TollFreeVehicle VehicleType => GetVehicleType(this);
}
}
2 changes: 2 additions & 0 deletions TollFeeCalculator/Toll/Readme.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Movie in the gif called "Hackers" :)
https://www.imdb.com/title/tt0113243/
8 changes: 8 additions & 0 deletions TollFeeCalculator/Toll/Tesla.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
namespace TollFeeCalculator.Toll
{
public class Tesla : VehicleType
{
public bool FeeFree => IsFeeFree(this);
public Enums.TollFreeVehicle VehicleType => GetVehicleType(this);
}
}
88 changes: 88 additions & 0 deletions TollFeeCalculator/Toll/TollCalculator.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
using System;
using System.Collections.Generic;
using System.Linq;

namespace TollFeeCalculator.Toll
{
public class TollCalculator
{
private List<DateTime> HolidayDates = new List<DateTime>();
public int CurrentFee { get; set; }
private DateTime entranceDate;
private int[] FeeRanges = { 8, 13, 18 };
private int MaxFee = 60;
private readonly IVehicleType vehicleType;
private int startRushHourH;
private int startRushHourM;
private int endRushHourH;
private int endRushHourM;
public DateTime LastAddedFeeDateTime { get; set; }


public TollCalculator(IVehicleType VehicleType,
int startRushHourH, int startRushHourM, int endRushHourH, int endRushHourM,
DateTime entranceDate,
List<DateTime> HolidayDates
)
{

vehicleType = VehicleType;
this.startRushHourH = startRushHourH;
this.startRushHourM = startRushHourM;
this.endRushHourH = endRushHourH;
this.endRushHourM = endRushHourM;
this.entranceDate = entranceDate;
this.HolidayDates = HolidayDates;
}


private bool isWeekend(DateTime date)
{
return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday;
}

public bool IsFeeFree(DateTime date)
{
if (isWeekend(date)) return true;

bool feeFreeCategory = this.vehicleType.IsFeeFree(this.vehicleType);
if (feeFreeCategory) return feeFreeCategory;

return HolidayDates.Any(x => x.Date == date.Date);
}


public int GetTollFee()
{
//Check if entrancedate is between range of rush hours
if (
this.entranceDate.Hour == this.startRushHourH && this.entranceDate.Minute <= this.startRushHourM
&&
this.entranceDate.Hour == this.endRushHourH && this.entranceDate.Minute >= this.endRushHourM
)
{
this.LastAddedFeeDateTime = entranceDate;
this.CurrentFee = this.MaxFee;
return this.CurrentFee;
}


//If vehicle is charged with fee more then once inside same hour
if (this.LastAddedFeeDateTime.Hour == entranceDate.Hour)
{
Array.Sort(this.FeeRanges);
Array.Reverse(this.FeeRanges);
this.CurrentFee += this.FeeRanges[0];
return this.CurrentFee;
}

//generate random fee based on fee list , or based on specific entrance then apply specific fee rate from the list
int randomIndex = new Random().Next(0, this.FeeRanges.Length - 1);
this.CurrentFee += this.FeeRanges[randomIndex];
this.LastAddedFeeDateTime = entranceDate;

return this.CurrentFee;
}

}
}
31 changes: 31 additions & 0 deletions TollFeeCalculator/Toll/VehicleType.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Linq;

namespace TollFeeCalculator.Toll
{

public class VehicleType : IVehicleType
{
public Enums.TollFreeVehicle GetVehicleType(object t)
{
//Filter enum list and return type of vehicle
var feeTypes = Enum.GetValues(typeof(Enums.TollFreeVehicle))
.Cast<Enums.TollFreeVehicle>()
.Select(d => d)
.ToList();
var vehicleType = feeTypes.Where(x => x.ToString() == t.GetType().Name).ToList();
return vehicleType.Count == 0 ? Enums.TollFreeVehicle.None : vehicleType.ToList()[0];
}

public bool IsFeeFree(object t)
{
//filter all enum of FeeFree categories excpect None category
var feeTypes = Enum.GetValues(typeof(Enums.TollFreeVehicle))
.Cast<Enums.TollFreeVehicle>()
.Select(d => d)
.ToList();

return feeTypes.Any(x => x.ToString() == t.GetType().Name && x != Enums.TollFreeVehicle.None);
}
}
}
Loading