-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Portfolio changed to functions instead of classes
- Loading branch information
1 parent
bdeff76
commit 90cd19c
Showing
3 changed files
with
39 additions
and
43 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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!") |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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}}}] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters