/**************************************************************************************************/
/*                                                                                                */
/*  Name:  DarC KonQuesT                                                                          */
/*  Program:  #2                                                                                  */
/*  Date:  2005.2.8                                                                               */
/*  Purpose:                                                                                      */
/*  stockListType class implementation file, which allows for creating list arrays (with listType)*/
/*  of type stockType (from stockType.h/cpp).  Includes operator overloading and assorted         */  
/*  functions.                                                                                    */
/*                                                                                                */
/**************************************************************************************************/
#include <iostream>
#include <string>
#include <cassert>
#include "listType.h"
#include "stockType.h"
#include "stockListType.h"

using namespace std;


//extraction operator overloading
istream& operator>>(istream& input, stockListType& stock_list) {
	for(int i = 0; i < stock_list.maxSize; i++)
	{
		input >> stock_list.stock_in;
		stock_list.list[i] = stock_list.stock_in;
	}

}

//insertion operator overloading 
ostream& operator <<(ostream& output, stockListType& stock_list) {
  for(int i = 0; i < stock_list.maxSize; i++)
  {
    cout << stock_list.list[i];
    stock_list.total_assets += stock_list.list[i].add_assets();
   }
  
	
}


/*  Name:  print_by_symbol                                                                                            */
/*  Purpose:  Output stock_list ordered alphabetically by stock Symbol (string symbol)                                */
/*  Parameters:  stockListType& stock_list                                                                            */
/*  Preconditions:  stockListType& stock_list has to be created/initialized/filled with data                          */
/*  Postconditions:  stockListType& stock_list is sorted alphabetically by stock symbol (string)                      */

void stockListType::print_by_symbol(stockListType& stock_list) {
  total_assets = 0;
	cout << endl << "*********    First Investor's Heaven    *********"
        << endl << "*********         Financial Report      *********" << endl
        << "Stock" << setw(22) << "Today" << setw(25) << "Previous" << setw(10) << "Percent"
        << endl << "Symbol" << setw(7) << "Open" << setw(11) << "Close" << setw(8) << "High"
        << setw(8) << "Low" << setw(9) << "Close" << setw(10) << "Gain" << setw(12) << "Volume"
        << endl << "------" << setw(8) << "-----" << setw(10) << "-----"
        << setw(9) << "-----" << setw(9) << "-----" << setw(9) << "-------" << setw(10) << "------"
        << setw(10) << "------" << endl;
  
	for(int i = 0; i < max_size; i++)
	{
		cout << stock_list.list[i];
		total_assets += stock_list.list[i].add_assets();
	}
	
	cout << "Closing Assets: $" << stock_list.total_assets << endl;
	cout << "-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-" << endl;


}


/*  Name:  print_by_gain                                                                                              */
/*  Purpose: Output stock_list ordered from least to greatest in order of percent gained/lost                         */
/*  Parameters:  stockListType& stock_list                                                                            */
/*  Preconditions:  stockListType& stock_list has to be created/initialized/filled with data                          */
/*  Postconditions: stockListType& stock_list is output sorted by percent gain/lost but IS NOT changed                */

void stockListType::print_by_gain(stockListType& stock_list) {
  total_assets = 0;
  
	cout << endl << "*********    First Investor's Heaven    *********"
        << endl << "*********         Financial Report      *********" << endl
        << "Stock" << setw(22) << "Today" << setw(25) << "Previous" << setw(10) << "Percent"
        << endl << "Symbol" << setw(7) << "Open" << setw(11) << "Close" << setw(8) << "High"
        << setw(8) << "Low" << setw(9) << "Close" << setw(10) << "Gain" << setw(12) << "Volume"
        << endl << "------" << setw(8) << "-----" << setw(10) << "-----"
        << setw(9) << "-----" << setw(9) << "-----" << setw(9) << "-------" << setw(10) << "------"
        << setw(10) << "------" << endl;
  
	for(int i = 0; i < max_size; i++)
	{
		cout << stock_list.list[sortIndicesGainLoss[i]];
		total_assets += stock_list.list[i].add_assets();
	}
	
	cout << "Closing Assets: $" << stock_list.total_assets << endl;
 
}


/*  Name: stockListType - constructor                                                                                 */
/*  Purpose: initialize variables and array                                                                           */
/*  Parameters:  list_size - the size of the list being created                                                       */
/*  Preconditions:  list_size must be initialized from input file                                                     */
/*  Postconditions:  size variables are set and sortIndicesGainLoss array is created                                  */


stockListType::stockListType(int list_size): listType<stockType>(list_size) {
	sortIndicesGainLoss = new int[list_size]; 
	maxSize = list_size;
  length = list_size;
  max_size = list_size;
}


/*  Name:  ~stockListType - destructor                                                                                */
/*  Purpose:  Deallocate memory used to create array (sortIndicesGainLoss)                                            */
/*  Parameters:  None                                                                                                 */
/*  Preconditions: sortIndicesGainLoss should have been created                                                       */
/*  Postconditions:  sortIndicesGainLoss memory is deallocated                                                        */

stockListType::~stockListType() {
	delete [] sortIndicesGainLoss;
}



/*  Name:  stock_sort()                                                                                               */
/*  Purpose:  Sort stocks (stockListType) by percent gained or loss, ascending order                                  */
/*  Parameters:  None                                                                                                 */
/*  Preconditions:  list of stocks should be created and initialized as well as sortIndicesGainLoss array             */
/*  Postconditions:  sortIndicesGainLoss holds the indices of stockList sorted by gain/loss                           */


void stockListType::stock_sort() {
    int i, j;
    int min;
    int temp1, temp2;

    for(i = 0; i < length; i++)
      sortIndicesGainLoss[i] = i;
   
    for(i = 0; i < length; i++)
    {
      min = i;
      for(j = i + 1; j < length; ++j)
        if(list[sortIndicesGainLoss[min]].percent_gain() > list[sortIndicesGainLoss[j]].percent_gain())
        
           min = j;
        temp1 = sortIndicesGainLoss[i];
        
        sortIndicesGainLoss[i] = sortIndicesGainLoss[min];
        sortIndicesGainLoss[min] = temp1;
        
        
      }
}



