diff --git a/C#/Car.cs b/C#/Car.cs index 6015da69..b1df38ac 100644 --- a/C#/Car.cs +++ b/C#/Car.cs @@ -8,9 +8,5 @@ namespace TollFeeCalculator { public class Car : Vehicle { - public String GetVehicleType() - { - return "Car"; - } } } \ No newline at end of file diff --git a/C#/Motorbike.cs b/C#/Motorbike.cs index 8258109f..42f89610 100644 --- a/C#/Motorbike.cs +++ b/C#/Motorbike.cs @@ -7,10 +7,10 @@ namespace TollFeeCalculator { public class Motorbike : Vehicle - { - public string GetVehicleType() - { - return "Motorbike"; + { + public override bool IsTollFree() + { + return true; } } } diff --git a/C#/TollCalculator.cs b/C#/TollCalculator.cs index b24c794d..08300da2 100644 --- a/C#/TollCalculator.cs +++ b/C#/TollCalculator.cs @@ -1,9 +1,13 @@ using System; using System.Globalization; -using TollFeeCalculator; +using TollFeeCalculator; +using System.Collections.Generic; public class TollCalculator -{ +{ + + const int MAX_DAY_FEE = 60; + const int FEE_INTERVAL_MINUTES = 60; /** * Calculate the total toll fee for one day @@ -12,62 +16,66 @@ public class TollCalculator * @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 totalDayFee = 0; + int intervalFee = 0; + List datesSorted = new List(dates); + datesSorted.Sort(); + foreach (DateTime date in datesSorted) { 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; + int diffInMinutes = Convert.ToInt32((date - intervalStart).TotalMinutes); + + if (diffInMinutes <= FEE_INTERVAL_MINUTES) + { + if (nextFee >= intervalFee) + { + intervalFee = nextFee; + } } else - { - totalFee += nextFee; + { + totalDayFee += intervalFee; + if (totalDayFee > MAX_DAY_FEE) + { + return MAX_DAY_FEE; + } + intervalStart = date; + intervalFee = nextFee; } + } + + totalDayFee += intervalFee; + if (totalDayFee > MAX_DAY_FEE) + { + return MAX_DAY_FEE; } - 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()); + return totalDayFee; } public int GetTollFee(DateTime date, Vehicle vehicle) - { - if (IsTollFreeDate(date) || IsTollFreeVehicle(vehicle)) return 0; + { + if (IsTollFreeDate(date) || vehicle.IsTollFree()) + { + 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; + if (hour == 6 && minute <= 29) return 8; + else if (hour == 6) return 13; + else if (hour == 7) return 18; + else if (hour == 8 && minute <= 29) return 13; + else if (hour >= 8 && hour <= 14) return 8; + else if (hour == 15 && minute <= 29) return 13; + else if (hour == 15 || hour == 16) return 18; + else if (hour == 17) return 13; + else if (hour == 18 && minute <= 29) return 8; else return 0; } @@ -80,8 +88,8 @@ private Boolean IsTollFreeDate(DateTime date) if (date.DayOfWeek == DayOfWeek.Saturday || date.DayOfWeek == DayOfWeek.Sunday) return true; if (year == 2013) - { - if (month == 1 && day == 1 || + { + if (month == 1 && (day == 1 || day == 5 || day == 6) || month == 3 && (day == 28 || day == 29) || month == 4 && (day == 1 || day == 30) || month == 5 && (day == 1 || day == 8 || day == 9) || @@ -95,14 +103,4 @@ private Boolean IsTollFreeDate(DateTime date) } 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#/UnitTests/TollCalculatorTest.cs b/C#/UnitTests/TollCalculatorTest.cs new file mode 100644 index 00000000..22ffe9ba --- /dev/null +++ b/C#/UnitTests/TollCalculatorTest.cs @@ -0,0 +1,82 @@ +using System; +using System.Text; +using System.Collections.Generic; +using System.Linq; +using Microsoft.VisualStudio.TestTools.UnitTesting; +using TollFeeCalculator; + +namespace UnitTests +{ + [TestClass] + public class TollCalculatorTest + { + TollCalculator tc; + + [TestInitialize] + public void Before() + { + tc = new TollCalculator(); + } + + [TestMethod] + public void TestMethodMaxIntervalFee() + { + Assert.AreEqual(18, tc.GetTollFee(new Car(), new DateTime[] { + new DateTime(2013, 2, 1, 6, 30, 0), + new DateTime(2013, 2, 1, 6, 45, 0), + new DateTime(2013, 2, 1, 7, 0, 0), + new DateTime(2013, 2, 1, 7, 15, 0), + new DateTime(2013, 2, 1, 7, 30, 0) })); + } + + [TestMethod] + public void TestMethodMaxDayFee() + { + Assert.AreEqual(60, tc.GetTollFee(new Car(), new DateTime[] { + new DateTime(2013, 2, 1, 6, 0, 0), + new DateTime(2013, 2, 1, 7, 0, 0), + new DateTime(2013, 2, 1, 8, 0, 0), + new DateTime(2013, 2, 1, 9, 0, 0), + new DateTime(2013, 2, 1, 11, 0, 0), + new DateTime(2013, 2, 1, 13, 0, 0), + new DateTime(2013, 2, 1, 15, 0, 0), + new DateTime(2013, 2, 1, 16, 0, 0), + new DateTime(2013, 2, 1, 17, 0, 0), + new DateTime(2013, 2, 1, 18, 0, 0) })); + } + + [TestMethod] + public void TestMethodTimeOfDay() + { + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 5, 0, 0) })); + Assert.AreEqual(8, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 6, 0, 0) })); + Assert.AreEqual(13, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 6, 30, 0) })); + Assert.AreEqual(18, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 7, 30, 0) })); + Assert.AreEqual(13, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 8, 0, 0) })); + Assert.AreEqual(8, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 8, 30, 0) })); + Assert.AreEqual(8, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 9, 30, 0) })); + Assert.AreEqual(8, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 12, 30, 0) })); + Assert.AreEqual(13, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 15, 0, 0) })); + Assert.AreEqual(18, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 15, 30, 0) })); + Assert.AreEqual(18, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 16, 30, 0) })); + Assert.AreEqual(13, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 17, 30, 0) })); + Assert.AreEqual(8, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 18, 0, 0) })); + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 2, 1, 18, 30, 0) })); + } + + [TestMethod] + public void TestMethodTollFreeDays() + { + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 1, 1, 12, 0, 0) })); + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 5, 1, 12, 0, 0) })); + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 7, 1, 12, 0, 0) })); + Assert.AreEqual(0, tc.GetTollFee(new Car(), new DateTime[] { new DateTime(2013, 12, 24, 12, 0, 0) })); + } + + [TestMethod] + public void TestMethodTollFreeVehicle() + { + Assert.AreEqual(0, tc.GetTollFee(new Motorbike(), new DateTime[] { new DateTime(2013, 2, 1, 12, 0, 0) })); + } + } +} diff --git a/C#/Vehicle.cs b/C#/Vehicle.cs index 19fe04e4..6d6b3320 100644 --- a/C#/Vehicle.cs +++ b/C#/Vehicle.cs @@ -6,8 +6,11 @@ namespace TollFeeCalculator { - public interface Vehicle - { - String GetVehicleType(); + public class Vehicle + { + public virtual Boolean IsTollFree() + { + return false; + } } } \ No newline at end of file