diff --git a/.gitignore b/.gitignore new file mode 100644 index 00000000..06262728 --- /dev/null +++ b/.gitignore @@ -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/ \ No newline at end of file diff --git a/C#/Motorbike.cs b/C#/Motorbike.cs deleted file mode 100644 index 8258109f..00000000 --- a/C#/Motorbike.cs +++ /dev/null @@ -1,16 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TollFeeCalculator -{ - public class Motorbike : Vehicle - { - public string GetVehicleType() - { - return "Motorbike"; - } - } -} diff --git a/C#/Toll-calc/Tests/Tests.csproj b/C#/Toll-calc/Tests/Tests.csproj new file mode 100644 index 00000000..8810c1dd --- /dev/null +++ b/C#/Toll-calc/Tests/Tests.csproj @@ -0,0 +1,20 @@ + + + + netcoreapp3.1 + + false + + + + + + + + + + + + + + diff --git a/C#/Toll-calc/Tests/UnitTest1.cs b/C#/Toll-calc/Tests/UnitTest1.cs new file mode 100644 index 00000000..ee13ed0d --- /dev/null +++ b/C#/Toll-calc/Tests/UnitTest1.cs @@ -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(() => _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(() => _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); + } + } +} diff --git a/C#/Toll-calc/Toll-calc.sln b/C#/Toll-calc/Toll-calc.sln new file mode 100644 index 00000000..b36f72d4 --- /dev/null +++ b/C#/Toll-calc/Toll-calc.sln @@ -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 diff --git a/C#/Toll-calc/Toll-calc/Extensions/DateTimeExtensions.cs b/C#/Toll-calc/Toll-calc/Extensions/DateTimeExtensions.cs new file mode 100644 index 00000000..6f6b0ddd --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Extensions/DateTimeExtensions.cs @@ -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; + } + } +} \ No newline at end of file diff --git a/C#/Car.cs b/C#/Toll-calc/Toll-calc/Models/Car.cs similarity index 53% rename from C#/Car.cs rename to C#/Toll-calc/Toll-calc/Models/Car.cs index 6015da69..a0ece593 100644 --- a/C#/Car.cs +++ b/C#/Toll-calc/Toll-calc/Models/Car.cs @@ -1,10 +1,6 @@ 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 { @@ -12,5 +8,10 @@ public String GetVehicleType() { return "Car"; } + + public bool IsTollFree() + { + return false; + } } } \ No newline at end of file diff --git a/C#/Toll-calc/Toll-calc/Models/Diplomat.cs b/C#/Toll-calc/Toll-calc/Models/Diplomat.cs new file mode 100644 index 00000000..6df9b120 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Diplomat.cs @@ -0,0 +1,15 @@ +namespace Toll_calc.Models +{ + public class Diplomat : Vehicle + { + public string GetVehicleType() + { + return "Diplomat"; + } + + public bool IsTollFree() + { + return true; + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Models/Emergency.cs b/C#/Toll-calc/Toll-calc/Models/Emergency.cs new file mode 100644 index 00000000..e9793b48 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Emergency.cs @@ -0,0 +1,15 @@ +namespace Toll_calc.Models +{ + public class Emergency : Vehicle + { + public string GetVehicleType() + { + return "Emergency"; + } + + public bool IsTollFree() + { + return true; + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Models/Foreign.cs b/C#/Toll-calc/Toll-calc/Models/Foreign.cs new file mode 100644 index 00000000..150c7d93 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Foreign.cs @@ -0,0 +1,15 @@ +namespace Toll_calc.Models +{ + public class Foreign : Vehicle + { + public string GetVehicleType() + { + return "Foreign"; + } + + public bool IsTollFree() + { + return true; + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Models/Motorbike.cs b/C#/Toll-calc/Toll-calc/Models/Motorbike.cs new file mode 100644 index 00000000..5d731336 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Motorbike.cs @@ -0,0 +1,15 @@ +namespace Toll_calc.Models +{ + public class Motorbike : Vehicle + { + public string GetVehicleType() + { + return "Motorbike"; + } + + public bool IsTollFree() + { + return true; + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Models/Tractor.cs b/C#/Toll-calc/Toll-calc/Models/Tractor.cs new file mode 100644 index 00000000..db2c9b12 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Tractor.cs @@ -0,0 +1,15 @@ +namespace Toll_calc.Models +{ + public class Tractor : Vehicle + { + public string GetVehicleType() + { + return "Tractor"; + } + + public bool IsTollFree() + { + return true; + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Models/Vehicle.cs b/C#/Toll-calc/Toll-calc/Models/Vehicle.cs new file mode 100644 index 00000000..10ed6153 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Models/Vehicle.cs @@ -0,0 +1,8 @@ +namespace Toll_calc.Models +{ + public interface Vehicle + { + string GetVehicleType(); + bool IsTollFree(); + } +} \ No newline at end of file diff --git a/C#/Toll-calc/Toll-calc/Program.cs b/C#/Toll-calc/Toll-calc/Program.cs new file mode 100644 index 00000000..7801b11d --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Program.cs @@ -0,0 +1,29 @@ +using System; +using System.Collections.Generic; +using Toll_calc.Models; +using Toll_calc.Services; + +namespace Toll_calc +{ + class Program + { + static void Main(string[] args) + { + Console.WriteLine("Hello World!"); + var holidayService = new HolidayService(); + var calculator = new TollCalculator(holidayService); + var vehicle = new Car(); + 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, 06,15,00), + }; + + var totalFee = calculator.GetTollFeeForDay(vehicle, passTimes); + Console.WriteLine(totalFee); + + Console.ReadLine(); + } + } +} diff --git a/C#/Toll-calc/Toll-calc/Services/HolidayService.cs b/C#/Toll-calc/Toll-calc/Services/HolidayService.cs new file mode 100644 index 00000000..cf15f329 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Services/HolidayService.cs @@ -0,0 +1,19 @@ +using System; +using Nager.Date; + +namespace Toll_calc.Services +{ + + public interface IHolidayService + { + bool IsHoliday(DateTime date); + } + + public class HolidayService : IHolidayService + { + public bool IsHoliday(DateTime date) + { + return DateSystem.IsPublicHoliday(date, CountryCode.SE); + } + } +} \ No newline at end of file diff --git a/C#/Toll-calc/Toll-calc/Toll-calc.csproj b/C#/Toll-calc/Toll-calc/Toll-calc.csproj new file mode 100644 index 00000000..34ce0681 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/Toll-calc.csproj @@ -0,0 +1,17 @@ + + + + Exe + netcoreapp3.1 + Toll_calc + + + + + + + + + + + diff --git a/C#/Toll-calc/Toll-calc/TollCalculator.cs b/C#/Toll-calc/Toll-calc/TollCalculator.cs new file mode 100644 index 00000000..b659a1e5 --- /dev/null +++ b/C#/Toll-calc/Toll-calc/TollCalculator.cs @@ -0,0 +1,104 @@ +using System; +using System.Linq; +using Toll_calc.Extensions; +using Toll_calc.Models; +using Toll_calc.Services; + +public class TollCalculator +{ + private readonly IHolidayService _holidayService; + + public TollCalculator(IHolidayService holidayService) + { + _holidayService = holidayService; + } + + /** + * Calculate the total toll fee for one day + * + * @param vehicle - the vehicle + * @param passTimes - date and time of all passes on one day + * @return - the total toll fee for that day + */ + public int GetTollFeeForDay(Vehicle vehicle, DateTime[] passTimes) + { + if (null == vehicle) + throw new ArgumentException("Vehicle can not be null."); + if (null == passTimes || passTimes.Length == 0) + return 0; + if (passTimes.Any(p => p.Date != passTimes[0].Date)) + throw new ArgumentException("All pass times must be from the same date."); + + if (vehicle.IsTollFree() || IsWeekend(passTimes[0]) || _holidayService.IsHoliday(passTimes[0])) return 0; + + var totalFee = 0; + var orderedPassTimes = passTimes.OrderBy(p => p.TimeOfDay).ToList(); + var intervalStart = orderedPassTimes[0]; + + for (var i = 0; i < orderedPassTimes.Count; i++) + { + if (orderedPassTimes[i] < intervalStart) + continue; + intervalStart = orderedPassTimes[i]; + var endTime = intervalStart.AddHours(1); + + //select the highest fee in interval + totalFee += (from date in orderedPassTimes + where date >= intervalStart && date < endTime + select GetTollFee(date)) + .Max(); + + intervalStart = endTime; + } + return Math.Min(totalFee, 60); + } + + private bool IsWeekend(DateTime date) + { + return date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday; + } + + private int GetTollFee(DateTime passTime) + { + if (passTime.Hour < 6) return (int)Fees.Free; + if (passTime.FallsBetween(new TimeSpan(6, 0, 0), new TimeSpan(6, 30, 0))) return (int)Fees.Low; + if (passTime.FallsBetween(new TimeSpan(6, 30, 0), new TimeSpan(7, 0, 0))) return (int)Fees.Medium; + if (passTime.FallsBetween(new TimeSpan(7, 0, 0), new TimeSpan(8, 0, 0))) return (int)Fees.High; + if (passTime.FallsBetween(new TimeSpan(8, 0, 0), new TimeSpan(8, 30, 0))) return (int)Fees.Medium; + //I've assumed the previous condition (where xx:00 - xx:29 in this was free) was incorrect (//else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8;) + //I've changed it to Low fee for the entire span. + if (passTime.FallsBetween(new TimeSpan(8, 30, 0), new TimeSpan(15, 0, 0))) return (int)Fees.Low; + if (passTime.FallsBetween(new TimeSpan(15, 0, 0), new TimeSpan(15, 30, 0))) return (int)Fees.Medium; + if (passTime.FallsBetween(new TimeSpan(15, 30, 0), new TimeSpan(17, 0, 0))) return (int)Fees.High; + if (passTime.FallsBetween(new TimeSpan(17, 0, 0), new TimeSpan(18, 0, 0))) return (int)Fees.Medium; + if (passTime.FallsBetween(new TimeSpan(18, 0, 0), new TimeSpan(18, 30, 0))) return (int)Fees.Low; + return (int)Fees.Free; + } + + [Obsolete] + public int GetTollFee_Old(DateTime date, Vehicle vehicle) + { + int hour = date.Hour; + int minute = date.Minute; + + if (hour == 6 && minute >= 0 && minute <= 29) return 8; + else if (hour == 6 && minute >= 30 && minute <= 59) return 13; + else if (hour == 7 && minute >= 0 && minute <= 59) return 18; + else if (hour == 8 && minute >= 0 && minute <= 29) return 13; + else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; + else if (hour == 15 && minute >= 0 && minute <= 29) return 13; + else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; + else if (hour == 17 && minute >= 0 && minute <= 59) return 13; + else if (hour == 18 && minute >= 0 && minute <= 29) return 8; + else return 0; + } + + + private enum Fees + { + Free = 0, + Low = 8, + Medium = 13, + High = 18 + } +} \ No newline at end of file diff --git a/C#/TollCalculator.cs b/C#/TollCalculator.cs deleted file mode 100644 index b24c794d..00000000 --- a/C#/TollCalculator.cs +++ /dev/null @@ -1,108 +0,0 @@ -using System; -using System.Globalization; -using TollFeeCalculator; - -public class TollCalculator -{ - - /** - * Calculate the total toll fee for one day - * - * @param vehicle - the vehicle - * @param dates - date and time of all passes on one day - * @return - the total toll fee for that day - */ - - public int GetTollFee(Vehicle vehicle, DateTime[] dates) - { - DateTime intervalStart = dates[0]; - int totalFee = 0; - foreach (DateTime date in dates) - { - int nextFee = GetTollFee(date, vehicle); - int tempFee = GetTollFee(intervalStart, vehicle); - - long diffInMillies = date.Millisecond - intervalStart.Millisecond; - long minutes = diffInMillies/1000/60; - - if (minutes <= 60) - { - if (totalFee > 0) totalFee -= tempFee; - if (nextFee >= tempFee) tempFee = nextFee; - totalFee += tempFee; - } - else - { - totalFee += nextFee; - } - } - if (totalFee > 60) totalFee = 60; - return totalFee; - } - - private bool IsTollFreeVehicle(Vehicle vehicle) - { - if (vehicle == null) return false; - String vehicleType = vehicle.GetVehicleType(); - return vehicleType.Equals(TollFreeVehicles.Motorbike.ToString()) || - vehicleType.Equals(TollFreeVehicles.Tractor.ToString()) || - vehicleType.Equals(TollFreeVehicles.Emergency.ToString()) || - vehicleType.Equals(TollFreeVehicles.Diplomat.ToString()) || - vehicleType.Equals(TollFreeVehicles.Foreign.ToString()) || - vehicleType.Equals(TollFreeVehicles.Military.ToString()); - } - - public int GetTollFee(DateTime date, Vehicle vehicle) - { - if (IsTollFreeDate(date) || IsTollFreeVehicle(vehicle)) return 0; - - int hour = date.Hour; - int minute = date.Minute; - - if (hour == 6 && minute >= 0 && minute <= 29) return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) return 13; - else if (hour == 7 && minute >= 0 && minute <= 59) return 18; - else if (hour == 8 && minute >= 0 && minute <= 29) return 13; - else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; - else if (hour == 15 && minute >= 0 && minute <= 29) return 13; - else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; - else if (hour == 17 && minute >= 0 && minute <= 59) return 13; - else if (hour == 18 && minute >= 0 && minute <= 29) return 8; - else return 0; - } - - private Boolean IsTollFreeDate(DateTime date) - { - int year = date.Year; - int month = date.Month; - int day = date.Day; - - if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) return true; - - if (year == 2013) - { - if (month == 1 && day == 1 || - month == 3 && (day == 28 || day == 29) || - month == 4 && (day == 1 || day == 30) || - month == 5 && (day == 1 || day == 8 || day == 9) || - month == 6 && (day == 5 || day == 6 || day == 21) || - month == 7 || - month == 11 && day == 1 || - month == 12 && (day == 24 || day == 25 || day == 26 || day == 31)) - { - return true; - } - } - return false; - } - - private enum TollFreeVehicles - { - Motorbike = 0, - Tractor = 1, - Emergency = 2, - Diplomat = 3, - Foreign = 4, - Military = 5 - } -} \ No newline at end of file diff --git a/C#/Vehicle.cs b/C#/Vehicle.cs deleted file mode 100644 index 19fe04e4..00000000 --- a/C#/Vehicle.cs +++ /dev/null @@ -1,13 +0,0 @@ -using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; -using System.Threading.Tasks; - -namespace TollFeeCalculator -{ - public interface Vehicle - { - String GetVehicleType(); - } -} \ No newline at end of file diff --git a/Java/Car.java b/Java/Car.java deleted file mode 100644 index e0ec832c..00000000 --- a/Java/Car.java +++ /dev/null @@ -1,7 +0,0 @@ - -public class Car implements Vehicle { - @Override - public String getType() { - return "Car"; - } -} diff --git a/Java/Motorbike.java b/Java/Motorbike.java deleted file mode 100644 index 14056ff9..00000000 --- a/Java/Motorbike.java +++ /dev/null @@ -1,7 +0,0 @@ - -public class Motorbike implements Vehicle { - @Override - public String getType() { - return "Motorbike"; - } -} diff --git a/Java/TollCalculator.java b/Java/TollCalculator.java deleted file mode 100644 index d239f327..00000000 --- a/Java/TollCalculator.java +++ /dev/null @@ -1,110 +0,0 @@ - -import java.util.*; -import java.util.concurrent.*; - -public class TollCalculator { - - /** - * Calculate the total toll fee for one day - * - * @param vehicle - the vehicle - * @param dates - date and time of all passes on one day - * @return - the total toll fee for that day - */ - public int getTollFee(Vehicle vehicle, Date... dates) { - Date intervalStart = dates[0]; - int totalFee = 0; - for (Date date : dates) { - int nextFee = getTollFee(date, vehicle); - int tempFee = getTollFee(intervalStart, vehicle); - - TimeUnit timeUnit = TimeUnit.MINUTES; - long diffInMillies = date.getTime() - intervalStart.getTime(); - long minutes = timeUnit.convert(diffInMillies, TimeUnit.MILLISECONDS); - - if (minutes <= 60) { - if (totalFee > 0) totalFee -= tempFee; - if (nextFee >= tempFee) tempFee = nextFee; - totalFee += tempFee; - } else { - totalFee += nextFee; - } - } - if (totalFee > 60) totalFee = 60; - return totalFee; - } - - private boolean isTollFreeVehicle(Vehicle vehicle) { - if(vehicle == null) return false; - String vehicleType = vehicle.getType(); - return vehicleType.equals(TollFreeVehicles.MOTORBIKE.getType()) || - vehicleType.equals(TollFreeVehicles.TRACTOR.getType()) || - vehicleType.equals(TollFreeVehicles.EMERGENCY.getType()) || - vehicleType.equals(TollFreeVehicles.DIPLOMAT.getType()) || - vehicleType.equals(TollFreeVehicles.FOREIGN.getType()) || - vehicleType.equals(TollFreeVehicles.MILITARY.getType()); - } - - public int getTollFee(final Date date, Vehicle vehicle) { - if(isTollFreeDate(date) || isTollFreeVehicle(vehicle)) return 0; - Calendar calendar = GregorianCalendar.getInstance(); - calendar.setTime(date); - int hour = calendar.get(Calendar.HOUR_OF_DAY); - int minute = calendar.get(Calendar.MINUTE); - - if (hour == 6 && minute >= 0 && minute <= 29) return 8; - else if (hour == 6 && minute >= 30 && minute <= 59) return 13; - else if (hour == 7 && minute >= 0 && minute <= 59) return 18; - else if (hour == 8 && minute >= 0 && minute <= 29) return 13; - else if (hour >= 8 && hour <= 14 && minute >= 30 && minute <= 59) return 8; - else if (hour == 15 && minute >= 0 && minute <= 29) return 13; - else if (hour == 15 && minute >= 0 || hour == 16 && minute <= 59) return 18; - else if (hour == 17 && minute >= 0 && minute <= 59) return 13; - else if (hour == 18 && minute >= 0 && minute <= 29) return 8; - else return 0; - } - - private Boolean isTollFreeDate(Date date) { - Calendar calendar = GregorianCalendar.getInstance(); - calendar.setTime(date); - int year = calendar.get(Calendar.YEAR); - int month = calendar.get(Calendar.MONTH); - int day = calendar.get(Calendar.DAY_OF_MONTH); - - int dayOfWeek = calendar.get(Calendar.DAY_OF_WEEK); - if (dayOfWeek == Calendar.SATURDAY || dayOfWeek == Calendar.SUNDAY) return true; - - if (year == 2013) { - if (month == Calendar.JANUARY && day == 1 || - month == Calendar.MARCH && (day == 28 || day == 29) || - month == Calendar.APRIL && (day == 1 || day == 30) || - month == Calendar.MAY && (day == 1 || day == 8 || day == 9) || - month == Calendar.JUNE && (day == 5 || day == 6 || day == 21) || - month == Calendar.JULY || - month == Calendar.NOVEMBER && day == 1 || - month == Calendar.DECEMBER && (day == 24 || day == 25 || day == 26 || day == 31)) { - return true; - } - } - return false; - } - - private enum TollFreeVehicles { - MOTORBIKE("Motorbike"), - TRACTOR("Tractor"), - EMERGENCY("Emergency"), - DIPLOMAT("Diplomat"), - FOREIGN("Foreign"), - MILITARY("Military"); - private final String type; - - TollFreeVehicles(String type) { - this.type = type; - } - - public String getType() { - return type; - } - } -} - diff --git a/Java/Vehicle.java b/Java/Vehicle.java deleted file mode 100644 index 289ec7df..00000000 --- a/Java/Vehicle.java +++ /dev/null @@ -1,5 +0,0 @@ - -public interface Vehicle { - - public String getType(); -}