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

Bugfixes, improvements, adding unit tests #82

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
4 changes: 0 additions & 4 deletions C#/Car.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,5 @@ namespace TollFeeCalculator
{
public class Car : Vehicle
{
public String GetVehicleType()
{
return "Car";
}
}
}
8 changes: 4 additions & 4 deletions C#/Motorbike.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
namespace TollFeeCalculator
{
public class Motorbike : Vehicle
{
public string GetVehicleType()
{
return "Motorbike";
{
public override bool IsTollFree()
{
return true;
}
}
}
102 changes: 50 additions & 52 deletions C#/TollCalculator.cs
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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<DateTime> datesSorted = new List<DateTime>(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;
}

Expand All @@ -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) ||
Expand All @@ -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
}
}
82 changes: 82 additions & 0 deletions C#/UnitTests/TollCalculatorTest.cs
Original file line number Diff line number Diff line change
@@ -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) }));
}
}
}
9 changes: 6 additions & 3 deletions C#/Vehicle.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,11 @@

namespace TollFeeCalculator
{
public interface Vehicle
{
String GetVehicleType();
public class Vehicle
{
public virtual Boolean IsTollFree()
{
return false;
}
}
}