Programming Assignment: Homework 4 - Online Banking System

###########################################################
### EXECUTE THIS CELL BEFORE YOU TO TEST YOUR SOLUTIONS ###
###########################################################

import nose.tools as tools

Import and Create Bank

def import_and_create_bank(filename):

    bank = {}
    with open(filename, 'r') as file:
        for line in file:
            lst = line.strip().split(':')
            if len(lst) <= 1:
                continue
            username = lst[0].strip()
            value = lst[1].strip()
            try:
                value = float(value)
                bank[username] = bank.get(username,0) + value
            except:
                continue
    return bank

sign up

def signup(user_accounts, log_in, username, password):

    def valid(password):
        if len(password) < 8:
            return False
        if password.isalpha() or password.isdigit():
            return False
        if password.lower() == username.lower():
            return False
        if not any(char.islower() for char in password):
            return False
        if not any(char.isupper() for char in password):
            return False
        if not any(char.isdigit() for char in password):
            return False
        return True
    if username in user_accounts:
        return False

    if not valid(password):
        return False

    user_accounts[username] = password
    log_in[username] = False

    return True

Import and Create Accounts

def import_and_create_accounts(filename):

    user_accounts = {}
    log_in = {}

    with open(filename, 'r') as file:
        for line in file:
            line = line.strip()
            if not line:
                continue

            if '-' not in line:
                continue

            username, password = line.split('-')
            username = username.strip()
            password = password.strip()

            if len(password) < 8:
                continue

            success = signup(user_accounts, log_in, username, password)
            if success:
                log_in[username] = False

    return user_accounts, log_in

Login

def login(user_accounts, log_in, username, password):

    if username not in user_accounts or user_accounts[username] != password:
        return False

    log_in[username] = True
    return True

Update

def update(bank, log_in, username, amount):

    if username in log_in and log_in[username]:
        if username not in bank:
            bank[username] = 0

        new_balance = bank[username] + amount

        if new_balance >= 0:
            bank[username] = new_balance
            return True

    return False

Transfer

def transfer(bank, log_in, userA, userB, amount):

    if userA in log_in and log_in[userA]:
        if userB in log_in:
            balanceA = bank.get(userA, 0)
            balanceB = bank.get(userB, 0)

            if balanceA >= amount >= 0:
                bank[userA] = balanceA - amount
                bank[userB] = balanceB + amount
                return True

    return False

Change Password

def change_password(user_accounts, log_in, username, old_password, new_password):

    def valid(password):
        if (
            len(password) >= 8 and
            any(char.islower() for char in password) and
            any(char.isupper() for char in password) and
            any(char.isdigit() for char in password)
        ):
            return True
        else:
            return False

    if (
        username in user_accounts and
        log_in.get(username, False) and
        user_accounts[username] == old_password and
        old_password != new_password and
        valid(new_password)
    ):
        user_accounts[username] = new_password
        return True
    else:
        return False

Delete Account

def delete_account(user_accounts, log_in, bank, username, password):

    if (
        username in user_accounts and
        user_accounts[username] == password and
        log_in.get(username, False) is True
    ):
        del user_accounts[username]
        del log_in[username]
        if username in bank:
            del bank[username]
        return True
    else:
        return False

Did you find this article valuable?

Support Abhinavtej's Codespace by becoming a sponsor. Any amount is appreciated!