#include #include #include #include #include "CPstring.h" #include "vector.h" #include "rando.h" #include "strutils.h" #include "stock.h" #include "stlist.h" struct MenuItem { char choice; // character typed by user string description; // description of operation MenuItem(char c, const string & d) : choice(c), description(d) { } }; MenuItem menu_g[] = { MenuItem('a', "add"), MenuItem('d', "delete"), MenuItem('m', "most active stock"), MenuItem('s', "symbol search"), MenuItem('l', "print stocks starting with given letter"), MenuItem('p', "print all stocks"), MenuItem('t', "trade stocks"), MenuItem('q', "quit"), }; const int numOps = sizeof(menu_g)/sizeof(MenuItem); const char QUIT_CHAR = 'q'; // function prototypes for database manipulating functions // and for menu system void Menu(); void DoAdd(StockList & list); void DoDelete(StockList & list); void DoSymbol(StockList & list); void DoLetterPrint(StockList & list); void DoReadStocks(StockList & stockList, istream & input); void DoTrades(StockList & stockList); int main (int argc, char * argv[]) { StockList stocklist; // Data base int k; string filename = "stocks.dat"; // File that contains starting data string line; // Line of data file char choice; // User's choice // test for filename entered on command line if (argc > 1) // command line argument is filename { filename = argv[1]; } ifstream instream(filename); if (instream.fail()) { cerr << "could not open " << filename << "for reading" << endl; return 1; } DoReadStocks(stocklist, instream); // read in data do { Menu(); cout << endl << "enter choice: "; getline(cin,line); // don't mix >> and getline choice = tolower(line[0]); switch (choice) // what action to take? { case 'a': DoAdd(stocklist); // add stock to database break; case 'd': DoDelete(stocklist); // delete stock from database break; case 'm': // find most active stock cout << "Most Active stock Not implemented" << endl; break; case 'p': stocklist.Print(); break; case 's': DoSymbol(stocklist); break; case 'l': DoLetterPrint(stocklist); break; case 't': DoTrades(stocklist); break; case 'q': cout << "thanks for using our stock database" << endl; break; default: cerr << "Unrecognized option: " << choice << endl; } } while (choice != QUIT_CHAR); return 0; } void Menu() // postcondition: all choices and corresponding operations printed { int k; for(k=0; k < numOps; k++) { cout << "[" << menu_g[k].choice << "]\t"; cout << menu_g[k].description << endl; } } void DoReadStocks(StockList & stockList, istream & input) // postcondition: stocks are read from input stream line by line { string sym; // Symbol char exchange; // Exchange double last; // last price unsigned int volume; // volume double volatility; // volatility of stock string name; // Name of stock while (input >> sym) { // read stock from file input >> exchange; input >> last; input >> volume; input >> volatility; getline(input, name); // Rest of line is company name StripWhite(name); // Strip leading/trailing white space stockList.Add(sym, exchange, last, volume, volatility, name); } } void DoAdd(StockList & list) // postcondition: new stock added to list (from user entered data) { string sym; char exchange; // Exchange double last; // last price unsigned int volume; // volume double volatility; // volatility of stock string name; // Name of stock string temp; // temp string cout << "Enter Symbol: "; getline (cin, sym); cout << "Enter Exchange: "; getline (cin, temp); exchange = toupper(temp[0]); cout << "Enter Last Trading Price: "; getline (cin, temp); last = atof(temp); cout << "Enter Traded volume: "; getline (cin, temp); volume = atoi(temp); cout << "Enter Volatitility: "; getline (cin, temp); volatility = atof(temp); cout << "Enter Company Name: "; getline (cin, name); list.Add(sym, exchange, last, volume, volatility, name); } void DoDelete(StockList & list) // postcondition: Stock deleted from list (from user entered data) { // Query User for symbol, delete stock } void DoLetterPrint(StockList & list) // postcondition: prompt for letter, print all stocks with that letter { string s; cout << "Enter Character: "; getline(cin,s); list.Print(toupper(s[0])); } void DoSymbol(StockList & list) // postcondition: Information for user input stock is printed { string sym; cout << "Enter Symbol: "; getline (cin, sym); list.Print(sym); } void DoTrades(StockList & stocklist) // postcondition: updates database from simulated trading file { string filename = "trades.dat"; //Name of simulated trading file ifstream instream(filename); if (instream.fail()) { cerr << "could not open " << filename << "for reading" << endl; return; } string line; // line of file string sym; // symbol of stock double price; // price of trade unsigned int volume; // volume of trade (number of shares traded) while (getline(instream, line)) { istrstream stringstream(line); //read in data stringstream >> sym; stringstream >> price; stringstream >> volume; if (stocklist.SetCurrent(sym)) //If stock is in data base { stocklist.Current().AddTrade(price, volume); } else { cerr << sym << " was not found in database" << endl; } } }