24 lines
499 B
Python
24 lines
499 B
Python
import csv
|
|
|
|
|
|
class Stock:
|
|
def __init__(self, name, shares, price):
|
|
self.name = name
|
|
self.shares = shares
|
|
self.price = price
|
|
|
|
def cost(self):
|
|
return self.shares * self.price
|
|
|
|
def sell(self, num):
|
|
self.shares -= num
|
|
if self.shares < 0:
|
|
self.shares = 0
|
|
|
|
|
|
def read_portfolio(filename):
|
|
with open(filename) as f:
|
|
rows = csv.reader(f)
|
|
_ = next(rows)
|
|
return [Stock(row[0], row[1], row[2]) for row in rows]
|