#include <iostream>
#include "arrayUtils.h"
#include <fstream>
#include "biddercollection.h"
#include <cstdlib>
using namespace std;
/**
* Create a collection capable of holding the indicated number of items
*/
BidderCollection::BidderCollection ()
: size(0)
{
}
BidderCollection::BidderCollection (const BidderCollection& bc)
: size(bc.size)
{
list.append(bc.list);
}
BidderCollection& BidderCollection::operator= (const BidderCollection& bc)
{
if (this != &bc)
{
list.clear();
size = bc.size;
list.append(bc.list);
}
return *this;
}
BidderCollection::~BidderCollection ()
{
list.clear();
}
void BidderCollection::add (const Bidder& value)
{
list.addToEnd (value);
++size;
}
void BidderCollection::remove (BidderCollection::Position pos)
{
list.remove (pos);
}
const Bidder& BidderCollection::get(BidderCollection::Position pos) const
{
return pos->data;
}
Bidder& BidderCollection::get(BidderCollection::Position pos)
{
return pos->data;
}
BidderCollection::Position BidderCollection::getFirst() const
{
return list.first;
}
bool BidderCollection::more (BidderCollection::Position afterThis) const
{
return afterThis->next != NULL;
}
BidderCollection::Position BidderCollection::getNext
(BidderCollection::Position afterThis) const
{
return afterThis->next;
}
/**
* Read all bidders from the indicated file
*/
void BidderCollection::readBidders (std::string fileName)
{
size = 0;
ifstream in (fileName.c_str());
int nBidders;
in >> nBidders;
for (int i = 0; i < nBidders; ++i)
{
string nme;
double bal;
in >> nme >> bal;
Bidder bidder (nme, bal);;
add (bidder);
}
}
/**
* Find the index of the bidder with the given name. If no such bidder exists,
* return null.
*/
BidderCollection::Position BidderCollection::findBidder (std::string name) const
{
for (Position current = list.first; current != NULL;
current = current->next)
{
if (name == current->data.getName())
return current;
}
return NULL;
}
void BidderCollection::print (std::ostream& out) const
{
out << size << "{";
for (Position current = list.first;
current != NULL; current = current->next)
{
out << " " << current->data << "\n";
}
out << "}";
}
bool BidderCollection::operator== (const BidderCollection& bc) const
{
if (size == bc.size)
{
Position current = list.first;
Position bcurrent = bc.list.first;
while (current != NULL)
{
if (!(current->data == bcurrent->data))
return false;
current = current->next;
bcurrent = bcurrent->next;
}
return true;
}
else
return false;
}
bool BidderCollection::operator< (const BidderCollection& bc) const
{
if (size == bc.size)
{
Position current = list.first;
Position bcurrent = bc.list.first;
while (current != NULL)
{
if (current->data < bcurrent->data)
return true;
else if (bcurrent->data < current->data)
return false;
current = current->next;
bcurrent = bcurrent->next;
}
return false;
}
else
return size < bc.size;
}