00001 #include "StdAfx.h"
00002
00003 TariffCalculator::TariffCalculator(void)
00004 {
00005 }
00006
00007 TariffCalculator::~TariffCalculator(void)
00008 {
00009 }
00010
00015 void TariffCalculator::parse(std::string filename) {
00016 std::ifstream tariffData(filename.c_str());
00017 std::string currentTariff;
00018
00019 if (!tariffData) {
00020 MessageBox(0, "Cannot open tariffs file!", "Error", MB_ICONERROR);
00021 exit (-1);
00022 }
00023
00026 while (getline(tariffData, currentTariff)) {
00027 typedef boost::tokenizer<boost::char_separator<char> > tokenizer;
00028 boost::char_separator<char> sep("|");
00029 tokenizer tokens(currentTariff, sep);
00030
00031 Tariff* tariff = new Tariff();
00032 tokenizer::iterator tok_iter = tokens.begin();
00033 tariff->setCountry(*tok_iter++);
00034 tariff->setDayTariff(boost::lexical_cast<float>(*tok_iter++));
00035 tariff->setEveningTariff(boost::lexical_cast<float>(*tok_iter++));
00036 tariff->setWeekendTariff(boost::lexical_cast<float>(*tok_iter));
00037
00038 tariffs[tariff->getCountry()] = tariff;
00039 }
00040 }
00041
00045 float TariffCalculator::getCost(CallInfo* callInfo) {
00046 float cost = 5.5f;
00047 std::string* location = callInfo->callerCountry;
00048 long durationSeconds = (callInfo->duration);
00049 long durationMinutes = (callInfo->duration/60L);
00050 bool isDomestic = false;
00051 Tariff* tariff;
00052
00053
00054
00055 if (durationSeconds < 60) {
00056 return cost;
00057 }
00058
00059
00060
00061 if (location->empty() || (location->compare("") == 0))
00062 isDomestic = true;
00063
00064
00065 TariffMap::const_iterator it = tariffs.find(*location);
00066 if (it == tariffs.end())
00067 isDomestic = true;
00068 else
00069 tariff = it->second;
00070
00071 if (isDomestic) {
00072 if(callInfo->tariffType == TariffType::EVENING
00073 || callInfo->tariffType == TariffType::WEEKEND) {
00074
00079 if (durationMinutes > 60) {
00080 cost = 5.5f;
00081 durationMinutes -= 60;
00082 cost += (durationMinutes * 3.0f);
00083 }
00084 }
00085 else {
00086 cost = (durationMinutes * 3.0f);
00087 }
00088 }
00089 else {
00090 if (callInfo->tariffType == TariffType::DAY)
00091 cost = (tariff->getDayTariff() * durationMinutes);
00092 else if (callInfo->tariffType == TariffType::EVENING)
00093 cost = (tariff->getEveningTariff() * durationMinutes);
00094 else if (callInfo->tariffType == TariffType::WEEKEND)
00095 cost = (tariff->getWeekendTariff() * durationMinutes);
00096 }
00097
00098 return cost;
00099 }
00100
00101
00102 TariffMap& TariffCalculator::getTariffs(void) {
00103 return tariffs;
00104 }