Python Stock price
(The Stock class) Design a class named Stock to represent a company’s stock
that contains:
■ A private string data field named symbol for the stock’s symbol.
■ A private string data field named name for the stock’s name.
■ A private float data field named previousClosingPrice that stores the stock price for the previous day.
■ A private float data field named currentPrice that stores the stock price for the current time.
■ A constructor that creates a stock with the specified symbol, name, previous price, and current price.
■ A get method for returning the stock name.
■ A get method for returning the stock symbol.
■ Get and set methods for getting/setting the stock’s previous price.
■ Get and set methods for getting/setting the stock’s current price.
■ A method named getChangePercent() that returns the percentage changed
from previousClosingPrice to currentPrice.
class Stock():
def __init__(self, symbol, name, previousClosingPrice, currentPrice):
self.__symbol = symbol
self.__name = name
self.__previousClosingPrice = previousClosingPrice
self.__currentPrice = currentPrice
def getStockName(self):
return self.__name
def getStockSymbol(self):
return self.__symbol
def getStockPreviousPrice(self):
return self.__previousClosingPrice
def getCurrentPrice(self):
return self.__currentPrice
def setStockPreviousPrice(self, previousClosingPrice):
self.__previousClosingPrice = previousClosingPrice
def setCurrentPrice(self, currentPrice):
self.__currentPrice = currentPrice
def getChangePercent(self):
if self.__previousClosingPrice < self.__currentPrice:
percent = (1 - (self.__previousClosingPrice / self.__currentPrice)) * 100
return 'Price is higher for {:.3f} %'.format(percent)
elif self.__previousClosingPrice > self.__currentPrice:
percent = (1 - (self.__currentPrice / self.__previousClosingPrice)) * 100
return 'Price is lower for - {:.3f} %'.format(percent)
else:
return 'Price is the same as previous day!'
name = Stock('INTC', 'Intel Corporation', 20.5, 20.35)
print(name.getChangePercent())
SOLUTION:
Price is lower for - 0.732 %
that contains:
■ A private string data field named symbol for the stock’s symbol.
■ A private string data field named name for the stock’s name.
■ A private float data field named previousClosingPrice that stores the stock price for the previous day.
■ A private float data field named currentPrice that stores the stock price for the current time.
■ A constructor that creates a stock with the specified symbol, name, previous price, and current price.
■ A get method for returning the stock name.
■ A get method for returning the stock symbol.
■ Get and set methods for getting/setting the stock’s previous price.
■ Get and set methods for getting/setting the stock’s current price.
■ A method named getChangePercent() that returns the percentage changed
from previousClosingPrice to currentPrice.
class Stock():
def __init__(self, symbol, name, previousClosingPrice, currentPrice):
self.__symbol = symbol
self.__name = name
self.__previousClosingPrice = previousClosingPrice
self.__currentPrice = currentPrice
def getStockName(self):
return self.__name
def getStockSymbol(self):
return self.__symbol
def getStockPreviousPrice(self):
return self.__previousClosingPrice
def getCurrentPrice(self):
return self.__currentPrice
def setStockPreviousPrice(self, previousClosingPrice):
self.__previousClosingPrice = previousClosingPrice
def setCurrentPrice(self, currentPrice):
self.__currentPrice = currentPrice
def getChangePercent(self):
if self.__previousClosingPrice < self.__currentPrice:
percent = (1 - (self.__previousClosingPrice / self.__currentPrice)) * 100
return 'Price is higher for {:.3f} %'.format(percent)
elif self.__previousClosingPrice > self.__currentPrice:
percent = (1 - (self.__currentPrice / self.__previousClosingPrice)) * 100
return 'Price is lower for - {:.3f} %'.format(percent)
else:
return 'Price is the same as previous day!'
name = Stock('INTC', 'Intel Corporation', 20.5, 20.35)
print(name.getChangePercent())
SOLUTION:
Price is lower for - 0.732 %
Коментари
Постави коментар