Skip to content

Instantly share code, notes, and snippets.

@hasayvaz
Created December 30, 2012 14:13
Show Gist options
  • Save hasayvaz/4412991 to your computer and use it in GitHub Desktop.
Save hasayvaz/4412991 to your computer and use it in GitHub Desktop.
using System;
using System.Collections.Generic;
using System.Text;
using LibraryConfigUtilities;
namespace LibraryBusiness
{
/* Description,
* settingList member holds configuration parameters stored in the App.config file,
* please explore the properties and methods in the Country class to get a better understanding.
*
* Please implement this class accordingly to accomplish requirements.
* Feel free to add any parameters, methods, class members, etc. if necessary
*/
public class PenaltyFeeCalculator {
string CountryCode;
DateTime DateStart, DateEnd;
private List<Country> settingList = new LibrarySetting().LibrarySettingList;
public PenaltyFeeCalculator(string country_code, DateTime date_start, DateTime date_end)
{
CountryCode = country_code;
DateStart = date_start;
DateEnd = date_end;
}
public String Calculate()
{
return System.Configuration.ConfigurationManager.AppSettings[LibrarySetting.Currency];
//double penaltyMultiplier = ; it will be penalty multiplier from App.config but i didn't this command.
/*double penaltyMultiplier = 5.25;
string fee = "";
int days = GetBusinessDays(DateStart, DateEnd);
if (days <= 0)
fee = "0.00";
else
fee = Convert.ToString(penaltyMultiplier * GetBusinessDays(DateStart, DateEnd));
return fee;*/
}
private int GetBusinessDays(DateTime firstDay, DateTime lastDay)
{
firstDay = firstDay.Date;
lastDay = lastDay.Date;
if (firstDay > lastDay)
throw new ArgumentException("Incorrect last day " + lastDay);
TimeSpan span = lastDay - firstDay;
int businessDays = span.Days + 1;
int fullWeekCount = businessDays / 7;
// Find out if there are weekends during the time exceedng the full weeks
if (businessDays > fullWeekCount * 7)
{
// We are here to find out if there is a 1-day or 2-days weekend
// In the time interval remaining after subtracting the complete weeks
int firstDayOfWeek = (int)firstDay.DayOfWeek;
int lastDayOfWeek = (int)lastDay.DayOfWeek;
if (lastDayOfWeek < firstDayOfWeek)
lastDayOfWeek += 7;
if (firstDayOfWeek <= 6)
{
if (lastDayOfWeek >= 7)// Both Saturday and Sunday are in the remaining time interval
businessDays -= 2;
else if (lastDayOfWeek >= 6)// Only Saturday is in the remaining time interval
businessDays -= 1;
}
else if (firstDayOfWeek <= 7 && lastDayOfWeek >= 7)// Only Sunday is in the remaining time interval
businessDays -= 1;
}
// subtract the weekends during the full weeks in the interval
businessDays -= fullWeekCount + fullWeekCount;
return businessDays;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment