Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions exercise_1/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
__pycache__
87 changes: 87 additions & 0 deletions exercise_1/src/bank_account.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
class BankAccount:
def __init__(self, username, password, account_id, balance=0):
self._balance = balance
self._username = username
self._password = password
self._account_id = account_id

@property
def username(self):
"""The username property (read-only)."""
return self._username

@property
def password(self):
"""The password property (read-only)."""
return self._password

@property
def account_id(self):
"""The account_id property (read-only)."""
return self._account_id

@property
def balance(self):
"""The balance for the account."""
return self._balance

@balance.setter
def balance(self, amount):
self._balance = amount

def __str__(self):
"""Return the details for the account."""
account_details = "Account details:\n"
account_details += f"Username: {self.username}\n"
account_details += f"Account number: {self.account_id}"
account_details += f"Balance: {self.balance}"
return account_details

def withdraw(self):
"""Check if the amount is correct and makes the withdraw."""
print("Withdraw.\n")
valid_amount = False
while not valid_amount:
amount = float(input("Amount to withdraw: "))
if amount <= 0 or amount > self.balance:
print("Invalid quantity. Please try again.\n")
else:
valid_amount = True
self.balance -= amount
print(f"Withdraw success. New balance: {self.balance}.")

def show_balance(self):
"""Prints the balance."""
print("Show balance.\n")
print(f"Your balance is: ${self.balance}")

def deposit(self):
"""Check valid amount and deposits."""
print("Deposit.\n")
valid_amount = False
while not valid_amount:
amount = float(input("Amount: "))
if amount <= 0:
print("Invalid amount, try again.")
else:
valid_amount = True
self.balance += amount
print(f"Deposit success. New balance: ${self.balance}.")

def transference(self, other_accounts):
"""Checks for valid account and amount. Then makes transference."""
print("Transference.\n")
valid_account = False
valid_amount = False
while not valid_account and not valid_amount:
destiny_account = input("Account to transfer: ")
amount = float(input("Amount: "))
amount_check = amount > 0 and amount <= self.balance
if destiny_account in other_accounts and amount_check:
print("Data verified successfuly. Making transference.")
valid_account = True
valid_amount = True
self.balance -= amount
print(f"New balance: ${self.balance}.")
else:
print("Invalid data. Please try again.")
79 changes: 79 additions & 0 deletions exercise_1/src/banking_system.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
from bank_account import BankAccount

my_account = BankAccount(
username="pepe", password="123", account_id="AC2323", balance=2000
)

other_accounts = ["AC3211", "AC9812", "AC2382"]


def login():
"""Ask up to three times the user for credentials. If success then log in
else the program terminates."""
login_tries_left = 3
logged_in = False

while login_tries_left > 0 and not logged_in:
print(f"\nYou have {login_tries_left} tries left. Be careful.")
username_input = input("\nUsername:\n")
password_input = input("\nPassword:\n")
if (
my_account.username == username_input
and my_account.password == password_input
):
print(f"\nWelcome {my_account.username}!\n")
logged_in = True
else:
login_tries_left -= 1

if not logged_in:
print(
"You have entered your credentials wrong three times. For security reasons"
+ " your access is blocked. gl hf contacting to support bud."
)
exit()


def display_menu():
"""Displays the main menu."""
menu_message = "Main menu:\n1. Deposit.\n"
menu_message += "2. Withdraw.\n"
menu_message += "3. View balance.\n"
menu_message += "4. Transference.\n"
menu_message += "5. Exit."
print(menu_message)


def select_task(my_account):
"""Select a task from the main menu and performs the task."""
selected_task = int(input("Enter a number for the task you want: "))

match selected_task:
case 1:
my_account.deposit()

case 2:
my_account.withdraw()

case 3:
my_account.show_balance()

case 4:
my_account.transference(other_accounts)

case 5:
print("Thank you for using our services. See you soon!")
exit()
case _:
print("Please select a valid task.")


# Bienvenida
print("Welcome to your favorite home-banking system")
print("To continue please log in:")
login()

# Main Menu
while True:
display_menu()
select_task(my_account)