Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

Question 1 (a2) What is the reaction force Dx in [N]?

Answered: 1 week ago