[Tutor] Code critique please (OOP strategy)
Sean 'Shaleh' Perry
shalehperry@attbi.com
Mon, 31 Dec 2001 00:11:02 -0800 (PST)
On 31-Dec-2001 Timothy Wilson wrote:
> Hi everyone,
>
> I've done an object-oriented version of the stock portfolio tracking
> program I assigned to my students
> (http://www.isd197.org/sibley/cs/icp/assignments/portfolio_html). I plan
> to use this for comparison when we introduce OOP after the Winter Break.
>
> My version is available at http://www.qwerk.org/tim/
>
> I'd like to get some feedback on the code. I've created two classes
> here, Portfolio and Stock. I'm specifically interested in the way the
> main() function interacts with the class methods. I've got a strange mix
> of program logic stuck in a series of elif statements and in the class
> methods themselves. Something doesn't seem right about it.
>
In my opinion, constructing a class should never cause the program to stop at a
prompt. This prevents later use. If I wanted to make a list of portfolio's
and then give them files to open, I see no way to do this. In C++ parlance you
have no default constructor.
I am also curious as to why the prices are * 100 every time. There is no
comment in the code for this.
> Is there a way to create a Menu class that would help?
>
you could make a menu class or function, but all that would do is move the
code out of main.
What you could do is have each menu choice be a function (or class method) and
have the code do a dictionary lookup -- think symbol table.
options['A'] = addNewStock
options['D'] = deleteStock
...
while not done:
input = raw_input(prompt)
try:
options[input.upper()]()
except KeyError:
print "Invalid choice!"