WARNING: After submission, two types of tests will be performed. The ‘General’ tab provides feedback on instance variable and method definitions. The ‘Evaluation after exam’ tab shows the tests used for evaluation after submission.
The Python project Stocks1 is used to keep track of your stock portfolio. Each stock has a Market code, a Ticker (i.e., stock name abbreviation), a price, and a quantity. The stocks in your portfolio are read in from a text file. Figure 1 shows the lines of this file:
NASDAQ;NFLX;447.77;15 NASDAQ;AMZN;2640.98;3 NASDAQ;AAPL;351.59;10 NASDAQ;TSLA;991.79;5 NASDAQ;MSFT;194.24;6 NYSE;DIS;117.65;3 NYSE;BAC;25.00;100 NYSE;JPM;99.45;15 NYSE;WMT;119.03;23 NYSE;T;30.25;45
The project contains two classes. The FileReader class reads a text file with a portfolio in the format described above when instantiated (by default, the file is “portfolio.txt”). The class interface provides the following methods:
has_next
: Returns True or False depending on whether there is more stock data to be read;next
: Reads the data (market, ticker, price, and quantity) of a stock. This data is returned as an AandeelData object;remove
: In principle, this command can be used to remove the data of a stock from the text file, but this command is not allowed;reset
: This command allows you to start reading the file again (i.e., the iterator goes back to the data of the first stock in the file).As shown in the figure below, FileReader uses the AandeelData class. Each object of this class represents the data of one stock. The class has the following methods:
get_markt
: Returns the market name of the stock as a string. The result is case-sensitive.get_ticker
: Returns the Ticker of the stock as a string. The result is case-sensitive.get_prijs
: Returns the price of the stock as a float.get_aantal
: Returns the quantity of the stock as an int.create_key
: A static method that returns a unique key based on the market and ticker in the form of [markt]-[ticker]. The result is case-sensitive. For example, for the stock NFLX on the NASDAQ market, the key NASDAQ-NFLX will be created.We will add simple management tasks for a stock portfolio to the AandelenPortefeuille class using 10 methods.
Create a class AandelenPortefeuille.
Add a parameterless constructor to the AandelenPortefeuille class. This constructor will first initialize a new FileReader object reader
and a dictionary portefeuille_map
as instance variables. Then, it will use the reader object to add the AandeelData entries one by one to the portefeuille_map dictionary. The key will be created using the static create_key method of the AandeelData class.
Add a public method aandeel_aanwezig
with parameters a string markt
and a string ticker
. If the stock is present in the instance variable portefeuille_map
, the method should return True
, otherwise, it should return False
. You can determine if a stock is present by examining the key of the portefeuille_map
instance variable.
Add a public method toevoegen_aandeel
with as parameter an AandeelData object. This object should only be added if the stock is not already present in the portfolio. If the stock could be added, the method should return True
, otherwise, it should return False
.
Add a public method aanpassen_aandeel
with parameters a string market
, a string ticker
, a float prijs
, and int aantal
. The method should adjust the price and quantity of a stock if it is present in the portefeuilleMap collection. If the stock is not present in the collection, nothing should be modified.
Add a public method waarde_aandeel
with parameters string markt
and a string ticker
. The method should return the value of a stock as a float based on the price and quantity. If the stock is not present in the collection, the method should return 0.
Add a public method totale_waarde
. The method should return the total value of the portfolio as a float.
Add a public method get_aandelen_markt
with a market code as a string parameter. This method should return a list of AandeelData objects listed on the market that matches the given parameter. If the market does not exist, an empty list should be returned.
Add a public method get_kleinste_aandeel
. This method should return the key (market-stock) of the stock with the lowest value (price * quantity).
Add a public method rapport
. This method should print a report in the console in the following format:
Portfolio ---------------- NYSE T 30.25 45 NASDAQ AMZN 2640.98 3 NYSE DIS 117.65 3 NYSE WMT 119.03 23 NASDAQ NFLX 447.77 15 NASDAQ MSFT 194.24 6 NYSE JPM 99.45 15 NASDAQ TSLA 991.79 5 NYSE BAC 25.0 100 NASDAQ AAPL 351.59 10 TOTAL VALUE: 32723.42