DEV Community

Cover image for Python 27: Banking System (part 1)
Gregor Schafroth
Gregor Schafroth

Posted on

Python 27: Banking System (part 1)

For todays Python exercises I wanted to do something with classes. I know the basics about them from CS50 Python, but havenโ€™t used them since that, so itโ€™s fair to say I am a total beginner. To practice this ChatGPT gave me a Banking System exercise (see next paragraph). Below that as always I share with you my code

Exercise: Simple Banking System
Create a basic banking system.
Allow users to create a new account, deposit money, withdraw money, and check their balance.
Use classes to represent users and accounts.

My Code

Since Iโ€™m not very familiar with classes I was only able to do a first part of this exercise today. I decided to just make it work with one account. Iโ€™ll still need to implement the bank and more features in the coming days ๐Ÿ™‚

import logging

logging.basicConfig(level=logging.DEBUG)

class Account:
    def __init__(self, account_id, holder_name, initial_balance=0):
        self.account_id = account_id
        self.holder_name = holder_name
        self.balance = initial_balance

    def deposit(self, amount):
        if amount > 0:
            self.balance += amount
            logging.info(f"Deposited {amount}. New balance: {self.balance}")
        else:
            logging.warning("Invalid deposit amount.")

    def withdraw(self, amount):
        if 0 < amount <= self.balance:
            self.balance -= amount
            logging.info(f"Withdrew {amount}. New balance: {self.balance}")
        else:
            logging.warning("Invalid withdrawal amount or insufficient funds.")

    def get_balance(self):
        return self.balance

class Bank:
    ...

def main():
    account = Account(123, "John Doe", 50)  # Creating an account with initial balance 50
    try:
        while True:
            user_input = input('What would you like to do? Options: n = new account (currently not available), d = deposit money, w = withdraw money, c = check balance, e = exit\n')
            if user_input == 'n':
                logging.info('This feature is not yet avaiable. Please wait for an update in the coming days')
            elif user_input == 'd':
                amount = float(input('Enter deposit amount: '))
                account.deposit(amount)
            elif user_input == 'w':
                amount = float(input('Enter withdrawal amount: '))
                account.withdraw(amount)
            elif user_input == 'c':
                print(f"Current balance: {account.get_balance()}")
            elif user_input == 'e':
                break
            else:
                logging.info("Invalid option selected.")
    except KeyboardInterrupt:
        logging.error('KeyboardInterrupt')
    except ValueError:
        logging.error("Invalid input. Please enter a number.")

    logging.debug('main() end')

if __name__ == '__main__':
    main()
Enter fullscreen mode Exit fullscreen mode

Top comments (0)