Skip to content

Commit

Permalink
Added create empty portfolio function
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeloijer-D committed Nov 25, 2022
1 parent 5192395 commit 85e506d
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
12 changes: 10 additions & 2 deletions PythonTrainingGroupB/Portfolio.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,20 @@ def withdraw_money(user_name, portfolios, amount):
else:
print("The amount given is not a positive number!")

def create_empty_portfolio(portfolios):
pass
def create_empty_portfolio(portfolios, user_name):
if user_name not in [portfolio['username'] for portfolio in portfolios['users']]:
portfolios['users'].append({'username': user_name, 'portfolio':{'balance' : 0, 'stocks' : {}}})
with open('portfolios.json', 'w') as fp:
json.dump(portfolios, fp)
print('Empty portfolio created')
display_portfolio(user_name)
else:
print('This username already exists, please create a portfolio with a different username!')


if __name__ == '__main__':
portfolios = load_portfolios('Portfolios.json')
add_money("luuk", portfolios, 5000)
display_portfolio('luuk')
create_empty_portfolio(portfolios, "luuk")
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, "portfolio": {"AAPL": 5, "IBM": 1}}}, {"username": "luuk", "portfolio": {"balance": 29500, "portfolio": {"AAPL": 1, "IBM": 2}}}]}
{"users": [{"username": "louk123", "portfolio": {"balance": 500, "stocks": {"AAPL": 5, "IBM": 1}}}, {"username": "luuk", "portfolio": {"balance": 14500, "stocks": {"AAPL": 1, "IBM": 2}}}]}

0 comments on commit 85e506d

Please sign in to comment.