Skip to content

Commit

Permalink
Added comments to Portfolio.py
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeloijer-D committed Nov 25, 2022
1 parent 32f8310 commit 79cb49a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 8 deletions.
14 changes: 7 additions & 7 deletions PythonTrainingGroupB/Portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ def get_portfolio(user_name, portfolios):
:param portfolios: the portfolios dictionary
:return: a dictionary with only the portfolio dictionary items of a single user
"""
if user_name in [portfolio['username'] for portfolio in portfolios['users']]:
return [portfolio for portfolio in portfolios['users'] if portfolio['username'] == user_name][0]
if user_name in portfolios['users'].keys():
return portfolios['users'][user_name]
else:
print('Portfolio does not exist')
def display_portfolio(user_name, portfolios):
Expand All @@ -46,7 +46,7 @@ def add_money(user_name, portfolios, amount):
"""
personal_portfolio = get_portfolio(user_name, portfolios)

balance = get_balance(user_name)
balance = get_balance(user_name, portfolios)

if type(amount) == float or type(amount) == int and amount > 0:
new_balance = balance + amount
Expand Down Expand Up @@ -91,7 +91,7 @@ def withdraw_money(user_name, portfolios, amount):
"""
personal_portfolio = get_portfolio(user_name, portfolios)

balance = get_balance(user_name)
balance = get_balance(user_name, portfolios)

if amount > balance:
print("You are trying to withdraw more money than is currently in your balance!")
Expand Down Expand Up @@ -128,8 +128,8 @@ def create_empty_portfolio(user_name, portfolios):
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
"""
if user_name not in [portfolio['username'] for portfolio in portfolios['users']]:
portfolios['users'].append({'username': user_name, 'portfolio':{'balance' : 0, 'stocks' : {}}})
if user_name not in portfolios['users'].keys():
portfolios['users'][user_name] = {'portfolio': {'balance': 0, 'stocks': {}}}
with open('portfolios.json', 'w') as fp:
json.dump(portfolios, fp)
print('Empty portfolio created')
Expand All @@ -142,5 +142,5 @@ def create_empty_portfolio(user_name, portfolios):
portfolios = load_portfolios('Portfolios.json')
add_money("luuk", portfolios, 5000)
display_portfolio('luuk', portfolios)
create_empty_portfolio(portfolios, "luuk")
create_empty_portfolio("luuk", portfolios)
withdraw_money('luuk', portfolios, 50000)
2 changes: 1 addition & 1 deletion PythonTrainingGroupB/Portfolios.json
Original file line number Diff line number Diff line change
@@ -1 +1 @@
{"users": [{"username": "louk123", "portfolio": {"balance": 500, "stocks": {"AAPL": 5, "IBM": 1}}}, {"username": "luuk", "portfolio": {"balance": 24500, "stocks": {"AAPL": 1, "IBM": 2}}}, {"username": "Pieter", "portfolio": {"balance": 0, "stocks": {}}}, {"username": "Reindert", "portfolio": {"balance": 0, "stocks": {}}}]}
{"users": {"louk123": {"portfolio": {"balance": 500, "stocks": {"AAPL": {"quantity": 5, "price": 151.14}, "IBM": {"quantity": 1, "price": 1501.14}}}}, "luuk": {"portfolio": {"balance": 15500, "stocks": {"AAPL": {"quantity": 5, "price": 151.14}, "IBM": {"quantity": 1, "price": 1501.14}}}}}}

0 comments on commit 79cb49a

Please sign in to comment.