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 a29be49 commit 284ba90
Showing 1 changed file with 62 additions and 4 deletions.
66 changes: 62 additions & 4 deletions PythonTrainingGroupB/Portfolio.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,49 @@
import json

def load_portfolios(path):
"""
load_portfolios loads in the users portfolios from a json file
:param path: the path to the json file
:return: a dictionary with the portfolio's
"""
with open('Portfolios.json', 'r') as j:
portfolios = json.loads(j.read())
return portfolios

def get_portfolio(user_name, portfolios):
"""
get_portfolio retrieves the portfolio of a single user from the portfolios dictionary
:param user_name: the user_name of a user
: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]
else:
print('Portfolio does not exist')
def display_portfolio(user_name, portfolios):
"""
display_portfolio retrieves shows the balance, the number of stocks, and the stocks a user has
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
"""
personal_portfolio = get_portfolio(user_name, portfolios)

print(f"Portfolio displayed for user {user_name}:")

print(f"Your Portfolio contains {len(personal_portfolio['portfolio'])} stocks and your balance is €{personal_portfolio['portfolio']['balance']}!")

def add_money(user_name, portfolios, amount):
"""
with add_money a user can deposit money into the balance of the portfolio of the user
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
:param amount: the amount
"""
personal_portfolio = get_portfolio(user_name, portfolios)

balance = get_balance(user_name)
Expand All @@ -26,11 +52,17 @@ def add_money(user_name, portfolios, amount):
new_balance = balance + amount
change_balance(user_name, portfolios, new_balance)
print(f"{amount} is added to your initial balance of {balance} so your new balance is now {personal_portfolio['portfolio']['balance']}")
return balance
else:
print("The amount given is not a positive number!")

def change_balance(user_name, portfolios, new_balance):
"""
change_balance is a helper-function of add_money and withdraw_money and performs the transaction in the portfolio.json file
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
:param new_balance: the balance after money is added or withdrawn
"""
print(portfolios)
personal_portfolio = get_portfolio(user_name, portfolios)
personal_portfolio['portfolio']['balance'] = new_balance
Expand All @@ -39,10 +71,24 @@ def change_balance(user_name, portfolios, new_balance):
with open('portfolios.json', 'w') as fp:
json.dump(portfolios, fp)

def get_balance(user_name):
def get_balance(user_name, portfolios):
"""
get_balance retrieves the balance from a user by searching in portfolios
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
:return balance: the balance of a user
"""
return get_portfolio(user_name, portfolios)['portfolio']['balance']

def withdraw_money(user_name, portfolios, amount):
"""
with withdraw_money a user can withdraw money from the balance of the portfolio of the user
:param user_name: the user_name of a user
:param portfolios: the portfolios dictionary
:param amount: the amount
"""
personal_portfolio = get_portfolio(user_name, portfolios)

balance = get_balance(user_name)
Expand All @@ -58,7 +104,14 @@ def withdraw_money(user_name, portfolios, amount):
else:
print("The amount given is not a positive number!")

def check_if_portfolio_exists(portfolios, user_name):
def check_if_portfolio_exists(user_name, portfolios):
"""
check_if_portfolio_exists checks if a user already has a portfolio and if not it calls
create_empty_portfolio to create a new portfolio
: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']]:
choice = input('It seems you do not have a portfolio, want to create one? [yes, no]')
if choice == "yes":
Expand All @@ -69,7 +122,12 @@ def check_if_portfolio_exists(portfolios, user_name):
display_portfolio(user_name, portfolios)


def create_empty_portfolio(portfolios, user_name):
def create_empty_portfolio(user_name, portfolios):
"""
create_empty_portfio creates an empty portfolio for a user when the user does not have a portfolio
: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' : {}}})
with open('portfolios.json', 'w') as fp:
Expand Down

0 comments on commit 284ba90

Please sign in to comment.