Skip to content

Commit

Permalink
Portfolio changed to functions instead of classes
Browse files Browse the repository at this point in the history
  • Loading branch information
ldeloijer-D committed Nov 24, 2022
1 parent bdeff76 commit 90cd19c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 43 deletions.
76 changes: 35 additions & 41 deletions PythonTrainingGroupB/Portfolio.py
Original file line number Diff line number Diff line change
@@ -1,41 +1,35 @@
class portfolio_information:

def __init__(self, user_id):
self.user_id = user_id
self.balance = 0
self.portfolio = {}
self.portfolio_count = len(self.portfolio)

def display_portfolio(self):
print(f"Portfolio displayed for user {self.user_id}:")
print(f'Your Portfolio contains {self.portfolio_count} stocks and your balance is €{self.balance}!')

def add_money(self, amount):
if type(amount) == float or type(amount) == int and amount > 0:
self.balance += amount
print(f'Your balance is now {self.balance}')
else:
print("The amount given is not a positive number!")

def withdraw_money(self, amount):
if amount > self.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}')
else:
print("The amount given is not a positive number!")


if __name__ == '__main__':
Port1 = portfolio_information('Louk123')
Port1.display_portfolio()
Port1.add_money(-150)
Port1.add_money(10000)
Port1.add_money(15000)

Port1.withdraw_money(24499)
Port1.withdraw_money(600)
import json

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

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

get_portfolio('luuk')

def display_portfolio(self):
print(f"Portfolio displayed for user {self.user_id}:")
print(f'Your Portfolio contains {self.portfolio_count} stocks and your balance is €{self.balance}!')

def add_money(self, amount):
if type(amount) == float or type(amount) == int and amount > 0:
self.balance += amount
print(f'Your balance is now {self.balance}')
else:
print("The amount given is not a positive number!")

def withdraw_money(self, amount):
if amount > self.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}')
else:
print("The amount given is not a positive number!")
2 changes: 2 additions & 0 deletions PythonTrainingGroupB/Portfolios.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"username": "louk123", "portfolio":{"balance": 500, "portfolio": {"AAPL": 5, "IBM": 1}}},
{"username": "luuk", "portfolio":{"balance": 1500, "portfolio": {"AAPL": 1, "IBM": 2}}}]
4 changes: 2 additions & 2 deletions PythonTrainingGroupB/User.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from Portfolio import portfolio_information
class user_name:

def __init__(self, user_id, password):
Expand All @@ -8,8 +9,7 @@ def welcome_message(self):
print(f"Welcome {self.user_id}!\n\nThe user information of user_id {self.user_id}: No additional information")

def create_portfolio(self):
pass

portfolio = portfolio_information(self.user_id)

if __name__ == '__main__':
User1 = user_name('louk123', 'ABC123')
Expand Down

0 comments on commit 90cd19c

Please sign in to comment.