Skip to content

Commit

Permalink
Updated portfolio functions
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeloijer-D committed Nov 24, 2022
1 parent 2766e29 commit 5192395
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 26 deletions.
61 changes: 36 additions & 25 deletions PythonTrainingGroupB/Portfolio.py
Original file line number Diff line number Diff line change
@@ -1,57 +1,68 @@
import json

with open('Portfolios.json', 'r') as j:
portfolios = json.loads(j.read())
def load_portfolios(path):
with open('Portfolios.json', 'r') as j:
portfolios = json.loads(j.read())
return portfolios

def get_portfolio(user_name):
if user_name in [portfolio['username'] for portfolio in portfolios['users']]:
return [portfolio for portfolio in portfolios['users'] if portfolio['username'] == user_name][0]['portfolio']
return [portfolio for portfolio in portfolios['users'] if portfolio['username'] == user_name][0]
else:
print('Portfolio does not exist')

def display_portfolio(user_name):
personal_portfolio = get_portfolio(user_name)

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

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

def add_money(user_name, amount):
def add_money(user_name, portfolios, amount):
personal_portfolio = get_portfolio(user_name)
balance = personal_portfolio['balance']

balance = get_balance(user_name)

if type(amount) == float or type(amount) == int and amount > 0:
balance += amount
print(f'Your balance is now {balance}')
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, amount):
personal_portfolio = [portfolio for portfolio in portfolios['users'] if portfolio['username'] == user_name][0]

user_index = [portfolio['username'] for portfolio in portfolios['users']].index(user_name)

new_balance = add_money(user_name, amount)
def change_balance(user_name, portfolios, new_balance):
print(portfolios)
personal_portfolio = get_portfolio(user_name)
personal_portfolio['portfolio']['balance'] = new_balance

portfolios[user_index] = personal_portfolio

print(portfolios)
with open('portfolios.json', 'w') as fp:
json.dump(portfolios, fp)

def get_balance(user_name):
return get_portfolio(user_name)['portfolio']['balance']

def withdraw_money(user_name, portfolios, amount):
personal_portfolio = get_portfolio(user_name)

change_balance("luuk", portfolios, 5000)
balance = get_balance(user_name)

def withdraw_money(self, amount):
if amount > self.balance:
if amount > balance:
print("You are trying to withdraw more money than is currently in your balance!")
print("Please provide an amount which is less or equal to your balance")

elif type(amount) == float or type(amount) == int and amount > 0:
print(f"{amount} is deducted from your balance which is {self.balance}")
self.balance -= amount
print(f'Your balance is now {self.balance}')
new_balance = balance - amount
change_balance(user_name, portfolios, new_balance)
print(f"Your balance is now {personal_portfolio['portfolio']['balance']}")
else:
print("The amount given is not a positive number!")

def create_empty_portfolio(portfolios):
pass


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

0 comments on commit 5192395

Please sign in to comment.