biddercollection.cpp


#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)
{
}


// Big 3

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();
}




// Collection operations


void BidderCollection::add (const Bidder& value)
//  Adds this bidder
//Pre: getSize() < getMaxSize()
{
  list.addToEnd (value);
  ++size;
}


void BidderCollection::remove (BidderCollection::Position pos)
// Remove the bidder at the indicated position
{
  list.remove (pos);
}


  // Access to individual elements

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;
}


// Print the collection
void BidderCollection::print (std::ostream& out) const
{
  out << size << "{";
  for (Position current = list.first; 
       current != NULL; current = current->next)
    {
      out << "  " << current->data << "\n";
    }
  out << "}";
}



  // Comparison operators
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;
	}

      // All of the elements are equal
      return false;
    }
  else
    return size < bc.size;
}