Python Advance Menu Driven Programming Help | Demo Practice Set

Overview

For this Task, you are going to develop an application that keeps track of a fund manager’s daily operations. To keep the process as simple as possible, we will assume that there is only a single portfolio/ investment made and that is in the stock market.


The problem focuses on a container class of user-defined objects and the main features being assessed include your ability to handle several classes working together, the dynamic adding of new objects to a list and searching the list for particular objects.


The specifications given below are to be followed, even though they might differ from any known investment system.


Task

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 code. It is recommended that you save both source code files in the same directory on your file system and compile and test each class as you develop it using small separate programs to create and test objects of each class.


The files

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 create 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:


  • An initialiser (constructor) (__init__) with one argument of type string, which is used to initialise the client’s name. The constructor should also initialise an empty list for storing stocks purchased.

  • Getter (reader) methods for accessing the attributes, name and stock list, but only setter (writer) method for name.

  • A method named addStock which accepts as an argument an object of class Stock. This method will store a reference to this Stock object into the list.

  • A method named noOfStocks which returns the number of Stock objects currently stored in the list.

  • A method named allStocks that does not take any parameters, but returns a string containing all the details of all Stock objects, one per line.

  • A method named totalValue that does not take any parameters, but returns the total value of all the Stock objects in the portfolio.

  • A method named mostExpensiveStock that finds and returns the details of the stock with the highest value. This method does not take any parameters.

  • NOTE: You may assume that there will always be only one stock with the highest value. There will not be a tie between two stocks on values.

  • A method named stockSummary that takes a parametric string, representing a stock name. This method will find and return the total value, number of lots, and average value of the purchased stocks with the parametric name.

  • 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.

  • 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.

  • 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.

  • 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.

stockDriver.py

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 create a single StockPortfolio object and provide a menu of choices to the user.


The Menu

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


0 Quit

Your choice?


Sample Run

Please note - I’ve truncated display of menu to save space! The menu should display on the screen in full each time it is displayed. User’s input is in blue, and bold.


Enter client’s name: Jackson Howard


Stock portfolio for Jackson Howard

----------------------------------

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


0 Quit


Sample Run

Please note - I’ve truncated display of menu to save space! The menu should display on the screen in full each time it is displayed. User’s input is in blue, and bold.


Enter client’s name: Jackson Howard


Stock portfolio for Jackson Howard

----------------------------------

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


0 Quit


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

8 Save stock information to file


0 Quit

Your choice? 1

Buying a new stock

Stock Name: Haas-Bioroid

Amount of Lots: 5

Price: 2.75

Date: 17/1/2019

... bought successfully.


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

2 Display all stocks’ details

:

8 Save stock information to file


0 Quit


Your choice? 2

Details of all stocks:

Weyland Consortium (2 lots @RM4.50/unit) bought in 3/2/2019

Haas-Bioroid (5 lots @RM2.75/unit) bought in 17/1/2019

Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

2 Display all stocks’ details

3 Display summary information about stock list

:

8 Save stock information to file


0 Quit


Your choice? 3

Summary Information

Number of stocks: 2

Total value: RM22750.00

Most expensive stock: Haas-Bioroid (5 lots @RM2.75/unit) bought in 17/1/2019 with a total value of RM13750.00


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

4 Display stocks with user-specified stock name

:

8 Save stock information to file


0 Quit


Your choice? 4

Stock Name: Weyland Consortium

The stocks are:

Weyland Consortium (2 lots @RM4.50/unit) bought in 3/2/2019 with a total value of RM9000

Average value of Weyland Consortium stocks: RM9000


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

4 Display stocks with user-specified stock name

:

8 Save stock information to file


0 Quit

Your choice? 4

Stock Name: Jinteki Biotech

No stock with that name could be found


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

5 Sell a stock based on a given index

:

8 Save stock information to file


0 Quit


Your choice? 5

Stock to sell (1..2)? 0

Out of range...


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

8 Save stock information to file


0 Quit


Your choice? 1

Buying a new stock

Stock Name: Globalsec

Amount of Lots: 6

Price: 3.24

Date: 8/1/2019

... bought successfully.


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

8 Save stock information to file


0 Quit

Your choice? 1

Buying a new stock

Stock Name: Jinteki Biotech

Amount of Lots: 3

Price: 6.22

Date: 10/3/2019

... bought successfully.


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

8 Save stock information to file


0 Quit

Your choice? 1

Buying a new stock

Stock Name: Weyland Consortium

Amount of Lots: 7

Price: 1.01

Date: 10/3/2019

... bought successfully.


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

2 Display all stocks’ details

:

8 Save stock information to file


0 Quit

Your choice? 2

Details of all stocks:

Weyland Consortium (2 lots @RM4.50/unit) bought in 3/2/2019

Haas-Bioroid (5 lots @RM2.75/unit) bought in 17/1/2019

Globalsec (6 lots @RM3.24/unit) bought in 8/1/2019

Jinteki Biotech (3 lots @RM6.22/unit) bought in 10/3/2019

Weyland Consortium (7 lots @RM1.01/unit) bought in 10/3/2019


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

2 Display all stocks’ details

3 Display summary information about stock list

:

8 Save stock information to file


0 Quit

Your choice? 3

Summary Information

Number of stocks: 5

Total value: RM67920.00

Most expensive stock: Globalsec (6 lots @RM3.24/unit) bought in 8/1/2019 with a total value of RM19440.00


Stock portfolio for Jackson Howard

----------------------------------

1 Buy stock

:

4 Display stocks with user-specified stock name