Question: Write in Python this project, you need to implement three classes: Stock , in the file stock.py , which encapsulates a stock. This class has

Write in Python this project, you need to implement three classes:

Stock, in the file stock.py, which encapsulates a stock. This class has two public instance variables:

symbol: the stock symbol

shares: the number of shares.

Portfolio, in the file portfolio.py, which encapsulates a collection of stocks. This class has two public instance variables:

name: the name of the collection

collection: a list of instances of Stock

Prices, in the file prices.py, which encapsulates prices of stocks. This class has two public instance variable:

name: the name of the prices

price_dict: a dictionary with stock symbols as keys and the corresponding prices (float) as values.

By inspecting the main program below and the corresponding output, you should be able to figure out which methods the three classes need.

from stock import Stock

from prices import Prices

from portfolio import Portfolio

# Create a few stocks

stock_apple = Stock("AAPL", 1000)

stock_google = Stock("GOOGL", 500)

stock_amazon = Stock("AMZN", 300)

stock_dell = Stock("DELL", 700) # This stock will not have a price

print(stock_apple)

print()

# Create a portfolio and add stocks to it

portfolio = Portfolio("My stocks")

print(portfolio)

portfolio.add(stock_apple)

portfolio.add(stock_google)

portfolio.add(stock_amazon)

portfolio.add(stock_dell)

print(portfolio)

print()

# Create market prices

prices = Prices("Closing prices")

print(prices)

prices.set_price("AAPL", 145.85)

prices.set_price("GOOGL", 96.27)

prices.set_price("AMZN", 92.96)

print(prices)

print()

# The value of a portfolio is the sum of the market value of the individual stocks.

# The market value of a stock is the market price * number of shares.

# If no price exists for at stock, the market value of the stock is 0.

value = portfolio.value(prices)

print(f"Value of portfolio is: {value:.2f}")

# Change the market price and recompute the value of the portfolio

prices.set_price("GOOGL", 99.75)

print(prices.get_price("GOOGL"))

value = portfolio.value(prices)

print(f"Value of portfolio is: {value:.2f}")

The output from the main program:

AAPL: 1000

My stocks:

My stocks:

AAPL: 1000

GOOGL: 500

AMZN: 300

DELL: 700

Closing prices:

Closing prices:

AAPL: 145.85

GOOGL: 96.27

AMZN: 92.96

Value of portfolio is: 221873.00

99.75

Value of portfolio is: 223613.00

from stock import Stock

from prices import Prices

from portfolio import Portfolio

# Create a few stocks

stock_apple = Stock("AAPL", 1000)

stock_google = Stock("GOOGL", 500)

stock_amazon = Stock("AMZN", 300)

stock_dell = Stock("DELL", 700) # This stock will not have a price

print(stock_apple)

print()

# Create a portfolio and add stocks to it

portfolio = Portfolio("My stocks")

print(portfolio)

portfolio.add(stock_apple)

portfolio.add(stock_google)

portfolio.add(stock_amazon)

portfolio.add(stock_dell)

print(portfolio)

print()

# Create market prices

prices = Prices("Closing prices")

print(prices)

prices.set_price("AAPL", 145.85)

prices.set_price("GOOGL", 96.27)

prices.set_price("AMZN", 92.96)

print(prices)

print()

# The value of a portfolio is the sum of the market value of the individual stocks.

# The market value of a stock is the market price * number of shares.

# If no price exists for at stock, the market value of the stock is 0.

value = portfolio.value(prices)

print(f"Value of portfolio is: {value:.2f}")

# Change the market price and recompute the value of the portfolio

prices.set_price("GOOGL", 99.75)

print(prices.get_price("GOOGL"))

value = portfolio.value(prices)

print(f"Value of portfolio is: {value:.2f}")

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!