Stock Program

Hello, the program I want to design will take information from a user regarding a stock ticker such as date, price, volume and it will store those in a stock object.

I want to store multiple dates and their corresponding prices and volumes on those days for the same stock object. I.E YHOO on 5/1 30 dollars, 5/2 29 dollars so on and so forth.

I thought the best way to do this would be to have a class called stock who's private members are stored in a doubly linked list to allow for fast inputs modifications and deletions.

Would something like this work or is it impossible?
Should I have my own linked list class and have my stock class inherit from it?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
class stock{
public: 
//member functions

private:
class stock{

public:
//member functions

private: 

struct node{//create struct
	string name;

	string date;

	int price;

	int volume;

	node* next;

	node* prev;
}


};


I am not sure if that is how I would properly implement it I just want to know if it is even possible to use a list to store data members for an object?

I have searched multiple webpages and have found no true answer to my question.
Last edited on
Whe don't use std::list? Or a vector? Or a set/unordered set if order is not important?

Example:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
class stock
{
  public:
//member functions
  private: 
    struct node{//create struct
	string name;
	string date;
	int price;
	int volume;
    }

    std::list<node> stocks;
};
A linked list provides quick insertion and deletion, but is slow to search and would not be my choice for implementing this. i.e. you must iterate over the entire list until you find the entry you're looking for. For the number of entries you're probably going to have, this won't be noticable.

If you're going to use a linked list, use the std::list container rather than writing your own unless you're trying to learn about how linked lists work.

I would be inclined to implement this using the std::map container for both stocks and quotes.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
class Quote
{  string date;
    float price; 
    int volume;
....
};

class Stock 
{  string symbol;
    string fullname;
    map<string, Quote>  quotes;    // Indexed by date
public:
    Stock (string sym, string name);
    void AddQuote (string date, float price, int vol)
    { Quote q (date, price, vol);
        quotes.insert (pair<string,Quote>(date, q));
    }
...
};

  map<string,stock>  stocks;  // Indexed by stock name
  Stock yahoo ("YHOO", "Yahoo");
  yahoo.AddQuote ("05/31", 30.00, 1000000);
  yahoo.AddQuote ("05/02", 29.00, 900000);
  stocks.insert (pair<string,Stock> ("YHOO", yahoo);

Last edited on
Wow, thank you so much!! I am going to work on adding a modify, delete, and print function for this set up. I'm going to hopefully set up a while loop to search and and complete those functions repeatedly.
You won't need a while loop to find either a stock or a quote. map has a find function which will return you the entry you're looking for (or a not found indication).

If fact, std::map is an associative container and can be used as
1
2
3
 
  cout << stocks["YHOO"].fullname << endl;  // assumes fullname is public
  cout << stocks["YHOO"].quotes["05/02"].price << endl;  


Be careful with this approach though, using a key which doesn't exist will add that key to the map.
Topic archived. No new replies allowed.