Banking System

Lab Python Advanced 30 points

Build a small bank with classes Account and Bank and a custom BankError. Commands: open NAME, deposit NAME AMT, withdraw NAME AMT, transfer FROM TO AMT, balance NAME. Print balances when asked and error: MESSAGE for unknown accounts, duplicate accounts or insufficient funds.

Input

Line 1: n. Then n commands.

Output

Balances and error messages.

Example

Input

9
open ann
open bob
deposit ann 100
transfer ann bob 30
withdraw bob 50
balance ann
balance bob
open ann
balance cy

Output

error: insufficient funds in bob
ann: 70
bob: 30
error: ann already exists
error: no account cy
Sample input available Sample output available
Editor
Python

Hints

Raise BankError for every rule violation and catch it once in the command loop.

transfer can reuse withdraw so the funds check lives in one place.

Solution explanation

Classes model the domain, exceptions report rule violations, and a single loop handles the input — the shape of most real applications.

Show solution
class BankError(Exception):
    pass


class Account:
    def __init__(self, owner):
        self.owner = owner
        self.balance = 0

    def withdraw(self, amount):
        if amount > self.balance:
            raise BankError(f"insufficient funds in {self.owner}")
        self.balance -= amount


class Bank:
    def __init__(self):
        self.accounts = {}

    def open(self, name):
        if name in self.accounts:
            raise BankError(f"{name} already exists")
        self.accounts[name] = Account(name)

    def get(self, name):
        if name not in self.accounts:
            raise BankError(f"no account {name}")
        return self.accounts[name]

    def transfer(self, src, dst, amount):
        source, target = self.get(src), self.get(dst)
        source.withdraw(amount)
        target.balance += amount


bank = Bank()
for _ in range(int(input())):
    cmd, *args = input().split()
    try:
        if cmd == "open":
            bank.open(args[0])
        elif cmd == "deposit":
            bank.get(args[0]).balance += int(args[1])
        elif cmd == "withdraw":
            bank.get(args[0]).withdraw(int(args[1]))
        elif cmd == "transfer":
            bank.transfer(args[0], args[1], int(args[2]))
        elif cmd == "balance":
            print(f"{args[0]}: {bank.get(args[0]).balance}")
    except BankError as err:
        print(f"error: {err}")
Output
Answer
HOME LEARN COMMUNITY DASHBOARD