Updated

mail@pastecode.io avatar
unknown
python
a year ago
3.6 kB
4
Indexable
Never

accounts = ["account1", "account2", "account3"]
account_balances = {}
account_transactions = {}

def load_accounts():
    for account in accounts:

        try:
            with open(f"{account}_balance.txt", 'r') as file:
                balance = int(file.read())
            account_balances[account] = balance
        except FileNotFoundError:
            account_balances[account] = 0
        except ValueError:
            account_balances[account] = 0

        # Load transactions
        try:
            with open(f"{account}_transactions.txt", 'r') as file:
                transactions = file.readlines()
            account_transactions[account] = [transaction.strip() for transaction in transactions]
        except FileNotFoundError:
            account_transactions[account] = []

def update_balance_file(account):
    with open(f"{account}_balance.txt", 'w') as file:
        file.write(str(account_balances[account]))

def save_transaction(account, transaction):
    with open(f"{account}_transactions.txt", 'a') as file:
        file.write(transaction + "\n")
    account_transactions[account].append(transaction)

def view_transactions(account):
    if account_transactions[account]:
        print(f"\nTransaction History for {account}:")
        for transaction in account_transactions[account]:
            print(transaction)
    else:
        print(f"No transactions found for {account}.")

def atm_menu():
    print("\nATM Machine")
    print("1: Check Balance")
    print("2: Deposit Money")
    print("3: Withdraw Money")
    print("4: View Transaction History")
    print("0: Exit")
    choice = int(input("Choose an option: "))
    return choice

def account_menu():
    print("\nSelect an Account:")
    for i, account in enumerate(accounts, start=1):
        print(f"{i}: {account}")
    choice = int(input("Choose an account: "))
    if 1 <= choice <= len(accounts):
        return accounts[choice - 1]
    return None

def main():
    load_accounts()
    while 1:
        account = account_menu()
        if account is None:
            print("Invalid option. Please choose a valid account.")
            continue
        while 1:
            choice = atm_menu()
            if choice == 0:
                print("Returning to account selection...")
                break
            elif choice == 1:
                print("Your current balance is:", account_balances[account])
            elif choice == 2:
                amount = int(input("Enter amount to deposit: "))
                if amount > 0:
                    account_balances[account] += amount
                    update_balance_file(account)
                    save_transaction(account, f"Deposit: {amount}")
                    print("Deposit successful!")
                else:
                    print("Invalid amount.")
            elif choice == 3:
                amount = int(input("Enter amount to withdraw: "))
                if 0 < amount <= account_balances[account]:
                    account_balances[account] -= amount
                    update_balance_file(account)
                    save_transaction(account, f"Withdrawal: {amount}")
                    print("Withdrawal successful!")
                else:
                    print("Invalid amount or insufficient balance.")
            elif choice == 4:
                view_transactions(account)
            else:
                print("Invalid option. Please choose a number between 0 and 4.")

main()