Brief
The brief for this exercise was to create a python script that would emulate the basic operation of an online bank account. The exercise is a good way to learn to create classes for efficient implementation of code.
# Create the class for the bank account
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited £{amount} New balance: £{self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrawn: £{amount} New balance: £{self.balance}")
else:
print(f"Insufficient funds you loser")
def check_balance(self):
return self.balance
# Define the function for interaction with the online banking system
def cashier():
cashier_prompt = input(f"Cashier: Good morning. Would you like to make an account?: ")
if cashier_prompt.lower() == "yes":
account_name = input("Cashier: Under what name? max 5 letters: ")
account = BankAccount()
initial_deposit = int(input("Cashier: How much will you be depositing?: £"))
account.deposit(initial_deposit)
print(f"Cashier: Your account {account_name} has been created with the balance of: £{initial_deposit}")
else:
print("ok")
customer = input("Is the anything else I can help you with? ")
if customer == "yes":
while customer != "quit":
customer = input("Request: deposit, withdraw, balance or quit: ")
if customer == "deposit":
deposit = int(input("How much? "))
account.deposit(deposit)
elif customer == "withdraw":
withdraw = int(input("How much? "))
account.withdraw(withdraw)
elif customer == "balance":
balance = account.check_balance()
print(f"£{balance}")
else:
print("Good day to you.")
cashier()
Cashier: Good morning. Would you like to make an account?: yes
Cashier: Under what name? max 5 letters: timmy
Cashier: How much will you be depositing?: £1000
Deposited £1000 New balance: £1000
Cashier: Your account timmy has been created with the balance of: £1000
Is the anything else I can help you with? yes
Request: deposit, withdraw, balance or quit: deposit
How much? 246000
Deposited £246000 New balance: £247000
Request: deposit, withdraw, balance or quit: balance
£247000
Request: deposit, withdraw, balance or quit: quit
Good day to you.
The Bank Account Class
class BankAccount:
def __init__(self, balance=0):
self.balance = balance
def deposit(self, amount):
self.balance += amount
print(f"Deposited £{amount} New balance: £{self.balance}")
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
print(f"Withdrawn: £{amount} New balance: £{self.balance}")
else:
print(f"Insufficient funds you loser")
def check_balance(self):
return self.balance
The BankAccount class is the foundation of this banking simulation program. It represents a bank account with methods for basic financial operations. The class initialises an account with a balance (defaulting to 0), and provides methods to deposit money, withdraw funds (with a check for sufficient balance), and check the current balance. These methods also print informative messages about each transaction, making the program more interactive and user-friendly.
The Cashier Function
def cashier():
cashier_prompt = input(f"Cashier: Good morning. Would you like to make an account?: ")
if cashier_prompt.lower() == "yes":
account_name = input("Cashier: Under what name? max 5 letters: ")
account = BankAccount()
initial_deposit = int(input("Cashier: How much will you be depositing?: £"))
account.deposit(initial_deposit)
print(f"Cashier: Your account {account_name} has been created with the balance of: £{initial_deposit}")
else:
print("ok")
customer = input("Is the anything else I can help you with? ")
if customer == "yes":
while customer != "quit":
customer = input("Request: deposit, withdraw, balance or quit: ")
if customer == "deposit":
deposit = int(input("How much? "))
account.deposit(deposit)
elif customer == "withdraw":
withdraw = int(input("How much? "))
account.withdraw(withdraw)
elif customer == "balance":
balance = account.check_balance()
print(f"£{balance}")
else:
print("Good day to you.")
The cashier function simulates a bank teller interaction. It prompts the user to create an account, collecting necessary information like account name and initial deposit. If an account is created, it then enters a loop allowing the user to perform various banking operations (deposit, withdraw, check balance) until they choose to quit. This function effectively ties together the BankAccount class functionality with a user-friendly interface, creating an interactive banking simulation.
Finally we call the cashier function:
cashier()
Conclusion
We created a class that is then utilised by the cashier function to provide the basic function of a banking system.