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

Update TollCalculator.cs #97

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
104 changes: 65 additions & 39 deletions C#/TollCalculator.cs
Original file line number Diff line number Diff line change
@@ -1,60 +1,83 @@
using System;
using System;
using System.Globalization;
using TollFeeCalculator;

public class TollCalculator
{

//Comments explaining why I did certain changes is not something I usually do. So it might look a bit more cluttered than it would normally.
/**
* 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)
//I'm assuming the main function calls this at the end of each day when all toll moments(all dates) have been registered.
public int GetTollFee(Vehicle vehicle, DateTime[] timeStamps)
{
DateTime intervalStart = dates[0];
DateTime intervalStart = timeStamps[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;

//Moved to this state, makes more sense to not make calculations on a toll free vehicle or date at all.
if (IsTollFreeDate(intervalStart) || IsTollFreeVehicle(vehicle)) return totalFee;

if (minutes <= 60)
{
if (totalFee > 0) totalFee -= tempFee;
if (nextFee >= tempFee) tempFee = nextFee;
totalFee += tempFee;
int hour = intervalStart.Hour;
int tempFee = GetTollFee(intervalStart);

//TimeStamps is more logical than dates since. Will always compare intervalStart to intervalStart the first iteration.
foreach (DateTime time in timeStamps)
{
int nextFee = GetTollFee(time);

//long diffInMillies = date.Millisecond - intervalStart.Millisecond;
//long minutes = diffInMillies/1000/60;

//if multiple date in the same hour.
if (hour == time.Hour)
{
if (nextFee >= tempFee)
{
//remove the lower fee from total. Only relevant if fee change for the hour.
if (totalFee > 0) totalFee -= tempFee;
tempFee = nextFee;
}
totalFee += tempFee; //add the higher fee.
}
else
{
totalFee += nextFee;
totalFee += nextFee; //new fee to add.
hour = time.Hour; //update hour
tempFee = GetTollFee(time); //update new temp fee for the new hour.
}

if (totalFee > 60) //if exceeding 60SEK, no need to continue checking toll fees.
{
totalFee = 60;
return totalFee;
}
}
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()) ||
if (vehicle == null) return false; //if no vehicle is specified, for some reason. ?

string vehicleType = vehicle.GetVehicleType();
return Enum.IsDefined(typeof(TollFreeVehicles), vehicleType);


/*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());
vehicleType.Equals(TollFreeVehicles.Military.ToString());*/
}

public int GetTollFee(DateTime date, Vehicle vehicle)
public int GetTollFee(DateTime date) //Struggled with this one
{
if (IsTollFreeDate(date) || IsTollFreeVehicle(vehicle)) return 0;
//if (IsTollFreeDate(date) || IsTollFreeVehicle(vehicle)) return 0; moved to GetTollFee.

int hour = date.Hour;
int minute = date.Minute;
Expand All @@ -71,28 +94,30 @@ public int GetTollFee(DateTime date, Vehicle vehicle)
else return 0;
}

//I would change this to support an automated format that reads from a calendar file which can be changed instead of the dates being hard coded into the software.
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(year == 2013) only works the 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))
{
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 true;
}
else
return false;

return false;
}

Expand All @@ -105,4 +130,5 @@ private enum TollFreeVehicles
Foreign = 4,
Military = 5
}
}

}