|
/* Begin Student Code */
#pragma warning (disable: 4786)
#include "BiddingWarAnalysis.h"
#include <numeric>
#include <functional>
#include <set>
using namespace std;
bool operator<(const CategoryStats& cs1, const CategoryStats& cs2) {
if (cs1.percentDiff > cs2.percentDiff)
return true;
else if (cs1.percentDiff < cs2.percentDiff)
return false;
else {
if (cs1.numberWars > cs2.numberWars)
return true;
else
return false;
}
}
int BiddingWarAnalysis::NUMBER_OF_BIDDERS_IN_A_WAR = 2;
int BiddingWarAnalysis::MINIMUM_BIDS_IN_WAR = 5;
BiddingWarAnalysis::BiddingWarAnalysis(Listing& listing, Categories& categories) {
convertRepresentation(listing, categories);
calculatePercentDiff();
}
void BiddingWarAnalysis::convertRepresentation (Listing& listing, Categories& categories) {
Categories::iterator it;
for (it = categories.begin(); it!= categories.end(); it++) {
stats[*it] = CategoryStats();
vector<int>::iterator items;
for (items = (*it)->itemsBegin(); items!= (*it)->itemsEnd(); items++) {
Advertisement* ad = listing[*items];
if (ad->getBids().size() > 0) {
if (isBiddingWar(ad)) {
stats[*it].numberWars++;
stats[*it].priceWars += ad->getBids().top().getAmount();
} else {
stats[*it].numberNonWars++;
stats[*it].priceNonWars += ad->getBids().top().getAmount();
}
}
}
}
}
void BiddingWarAnalysis::calculatePercentDiff(void) {
map<Category*, CategoryStats>::iterator it;
for (it = stats.begin(); it!= stats.end(); it++) {
if (it->second.numberNonWars > 0 &&
it->second.numberWars > 0) {
double warsAverage, nonWarsAverage;
warsAverage = it->second.priceWars / it->second.numberWars;
nonWarsAverage = it->second.priceNonWars / it->second.numberNonWars;
it->second.percentDiff = ((warsAverage - nonWarsAverage) / warsAverage) * 100;
}
it->second.cat = it->first;
sorted.push_back(it->second);
}
}
bool BiddingWarAnalysis::isBiddingWar(Advertisement* ad) {
priority_queue<Bid> bids = ad->getBids();
if (bids.size() >= MINIMUM_BIDS_IN_WAR) {
set<string> bidders;
for (int i = 0; i < MINIMUM_BIDS_IN_WAR; i++) {
bidders.insert(bids.top().getEmail());
bids.pop();
}
if (bidders.size() == NUMBER_OF_BIDDERS_IN_A_WAR) {
return true;
}
else {
return false;
}
} else {
return false;
}
}
void BiddingWarAnalysis::showBiddingWarStatistics(void) {
sort(sorted.begin(), sorted.end());
vector<CategoryStats>::iterator it;
for (it = sorted.begin(); it!= sorted.end(); it++) {
cout << it->cat->getName() << ": "
<< it->numberNonWars << "\t"
<< it->numberWars << "\t"
<< it->percentDiff << endl;
}
}
/* End Student Code */
Дата добавления: 2015-11-14; просмотров: 71 | Нарушение авторских прав
<== предыдущая страница | | | следующая страница ==> |
Identifying Bid Snipers | | | BiddingWarAnalysis.h |