You are to write two source files as described below:
· The first file named stockApp.py, should contain the following two classes:
· The first is a class called Stock which defines a simple object type representing a stock being traded in a stock market.
· The second class called StockPortfolio defines objects which are containers of Stock objects.
· The second file named stockDriver.py defines a Python application, with main method, which creates one StockPortfolio object and allows the various methods of Stock to be called. This class will be an interactive application using the keyboard and the screen to interact with a human operator. It will not do calculations itself but will immediately pass user inputs as arguments to methods of StockPortfolio class.
NOTE: The final application will only execute correctly when both files have been defined completely and correctly but don’t wait until you have completely written all three before you start compiling and testing your cod.e. It is recommended that you save both source cod.e files in the same directory on your file system and compile and test each class as you develop it using small separate programs to creat and test objects of each class.
The files you will require are:
Class Stock [saved in stockApp.py]
Each stock purchased will have 4 instance variables:
· stock name (a string)
· amount purchased in lot (an integer, where 1 lot refers to 1000 units)
· price purchased (per unit, a float, in RM)
· date purchased (a string, where the format is “MM/DD/YYYY”).
The methods of class Stock should include:
– An initialiser (constructor) method (__init__) which accepts FOUR attributes for a stock – name, amount purchased, price purchased, and date purchased. The method should initialise its object’s attributes with these parametric values.
– A reader (getter) method for each of the four attributes. That is, a simple ‘getter’ method for the name, amount purchased, price purchased, and date purchased.
– A writer (setter) method for each of the attributes.
– A method named value that will return the value of this stock. A stock’s value is calculated by multiplying the purchased price with the amount purchased (in units). For example, a stock purchased with a price of RM3.50 for 2 lots (2000 units) will have a value of RM7000.00
– A method __eq__ that takes another Stock as parameter, and returns true if both stocks are equal (same), false otherwise. Two stocks are considered equal if they have the same name.
– A method __lt__ that takes another Stock as parameter, and returns true if this stock is having a value less than the parametric stock.
– A method __le__ that takes another Stock as parameter, and returns true if this stock is having a value less than or equal to the parametric stock.
– A string method (__str__) which return a single string containing the details of a stock. Such a string can be formed by concatenating the values of the four attributes name, amount purchased, price purchased, and date purchased in the format:
<name> (<amountInLots> lots @<price>/unit) bought <date>
One sample output is as shown below:
HELP (2 lots @RM2.35/unit) bought in 10/10/2010
It is recommended that once you have written the Stock class, you creat a tiny program to test it. The testing program should be used to create one or two Stock objects and call some of the Stock methods. Compile and run the test program to check your work.
Class StockPortfolio [written and saved in the same file as StockPortfolio in stockApp.py]
This class is to be defined in the same file as class Stock. It declares a class of object with maintains a list of Stock objects. It will contain methods which enable the list to show the appropriate behaviours as required by the menu.
The StockPortfolio class should have a client’s name (of type string), and a collection of Stock objects bought by the client, no additional attribute is required:
The StockPortfolio class must also contain some methods which allow the collection of stocks to be managed. The methods of class StockPortfolio should include:
9. A method named sellStock that takes a positive integer representing the location of the stock to be sold (i.e deleted from the list). If the parametric integer is invalid or out of bounds, then the method should return None.
10. A method sortedStocks that takes a parameter of type string. The string represents the criteria to be used in sorting the stock list. The method will return a new list containing all the stocks sorted accordingly to the criteria mentioned in the parametric string in ascending order. The criteria could be lots, price, or value.
11. A method saveToFile that accepts a string representing the filename to save, and all Stock objects will be saved to a text file, one per line, with each attribute separated by a comma.
12. A method loadFromFile that accepts a string representing the filename where the data is to load from.
When you have written the StockPortfolio class – test it by creating a StockPortfolio object and invoking the methods from a small test program.
The aim of this file is to provide a user-interface for a modest application which uses a StockPortfolio container class. The user-interface is written as a ‘console’ application using the normal screen and keyboard to interact with a user via a simple text-based menu.
The user-interface should creat a single StockPortfolio object and provide a menu of choices to the user.
Stock portfolio for <user’s name>
1 Buy stock
2 Display all stocks’ details
3 Display summary information about stock list
4 Display stocks with user-specified stock name
5 Sell a stock based on a given index
6 Display all stocks, sorted according to Stock values
7 Load stock information from file
8 Save stock information to file
Hi there! Click one of our representatives below and we will get back to you as soon as possible.