00001 /*************************************************************************** 00002 stockmarket.cpp - description 00003 ------------------- 00004 begin : Fri Jan 14 2003 00005 copyright : (C) 2002 by Michael Otto 00006 email : Michael.Otto@saskathex.de 00007 ***************************************************************************/ 00008 00009 /*************************************************************************** 00010 * * 00011 * This program is free software; you can redistribute it and/or modify * 00012 * it under the terms of the GNU General Public License as published by * 00013 * the Free Software Foundation; either version 2 of the License, or * 00014 * (at your option) any later version. * 00015 * * 00016 ***************************************************************************/ 00017 00018 #include "stockmarket.h" 00019 #include "stockcontainer.h" 00020 #include "stock.h" 00021 #include "stockmarketdefines.h" 00022 #include <functional> 00023 #include <algorithm> 00024 00025 00026 00030 StockMarket::StockMarket(string marketName) { 00031 StockMarket::marketName = marketName; 00032 stocks.clear(); 00033 } 00034 00038 StockMarket::StockMarket() { 00039 marketName = ""; 00040 stocks.clear(); 00041 } 00042 00046 StockMarket::~StockMarket() { 00047 // deleting every element 00048 for (int i = 0; i < stocks.size(); i++) { 00049 StockContainer * sc = stocks[i]; 00050 delete sc; 00051 } 00052 stocks.clear(); 00053 } 00054 00055 00056 00060 int StockMarket::getNumberOfStockContainer() const { 00061 return stocks.size(); 00062 } 00063 00068 StockContainer *StockMarket::getStockContainer(int number) const { 00069 return stocks[number]; 00070 } 00071 00075 string StockMarket::getMarketName() const { 00076 return marketName; 00077 } 00078 00082 void StockMarket::setMarketName(string marketName) { 00083 StockMarket::marketName = marketName; 00084 notify(); 00085 } 00086 00091 bool StockMarket::removeStock(Stock *stock) { 00092 #ifdef DEBUG 00093 cout << "removing stock with name " << stock->getName() << endl; 00094 #endif 00095 for (int i = 0; i < stocks.size(); i++) { 00096 StockContainer * sc = stocks[i]; 00097 Stock * s = sc->getStock(); 00098 if (*s == *stock) { 00099 #ifdef DEBUG 00100 cout << " Stock was found!" << endl; 00101 #endif 00102 deque <StockContainer*>::iterator iter; 00103 iter = find(stocks.begin(), stocks.end(), sc); 00104 stocks.erase(iter); 00105 notify(); 00106 delete sc; 00107 return true; 00108 } 00109 } 00110 return false; 00111 } 00112 00113 00118 void StockMarket::addStockContainer(StockContainer * stockContainer) { 00119 stocks.push_back(stockContainer); 00120 sortStockMarket(); 00121 notify(); 00122 } 00123 00124 00128 void StockMarket::addStock(const Stock & stock) { 00129 #ifdef DEBUG 00130 cout << "adding a stock to " << marketName << endl; 00131 #endif 00132 StockContainer *stockContainer = new StockContainer(stock); 00133 this->addStockContainer(stockContainer); 00134 } 00135 00136 void StockMarket::sortStockMarket() { 00137 #ifdef DEBUG 00138 cout << "SORT START " << endl; 00139 #endif 00140 sort(stocks.begin(), stocks.end(), std::greater<StockContainer*>()); 00141 #ifdef DEBUG 00142 cout << "SORT END " << endl; 00143 #endif 00144 } 00145 00146 00147 00148 00154 int operator==(const StockMarket &x, const StockMarket &y) { 00155 #ifdef DEBUG 00156 cout << "StockMarket::Operator == is called" << endl; 00157 #endif 00158 if (x.getMarketName() != y.getMarketName()) return 0; 00159 if (x.getNumberOfStockContainer() != y.getNumberOfStockContainer()) return 0; 00160 return 1; 00161 }