I am trying to create a simple program in Ruby to track the price movements of stocks, but I’m not entirely sure of how it should be designed.
Basically, I’m thinking of a class Stock, with all the attributes such as name, desc, etc. However, I’m not sure of how the price attribute would work. Because for each stock, I also want to track the history of prices and plot them on a graph. So, my question is, should I create another class, Prices and associate it with Stock? or is there a better way?
I’m a newbie at OOD and would love some explanation, helpful links or other advice. Thank you in advance.
Some of this will depend on your choice of DB: OO, document, or relational. If you’re using the typical relational DB, then you would have a table for prices that represents the one-to-many relationship between prices and stock.
It seems like you should have a separate class for price, because I’m assuming you’ll want to track price with time. I know this isn’t Ruby, but your Stock class could look something like this:
And then Price:
Note: You’ll need to ignore the fact that the IDs are public, and therefore able to be changed by outside classes. However, this code is presented this way for simplicity.
Hope that helps.