"""
Klasa BankAccount, reprezentujaca konto bankowe (nie kredytowe - saldo musi byc nieujemne)
Dbamy o zalozenie nieujemnosci we wszystkich metodach, oraz rzucamy wyjatek przy
podejrzanych operacjach (np. wybierania ujemnej kwoty z konta)
"""


class BankAccount:
    def __init__(self, number, balance=0):
        if balance < 0:
            raise ValueError("Cannot create an account with negative balance.")
        self.number = number
        self.balance = balance

    def deposit(self, amount):
        if amount < 0:
            raise ValueError(f"Account number {self.number}: cannot deposit a negative amount.")
        self.balance += amount

    def withdraw(self, amount):
        if amount < 0:
            raise ValueError(f"Account number {self.number}: cannot withdraw a negative amount.")
        if amount > self.balance:
            raise ValueError(f"Account number {self.number}: cannot withdraw {amount} (current balance: {self.balance})")
        self.balance -= amount

    def merge_to(self, other_account):
        if self is other_account:
            raise ValueError(f"Cannot merge account number {self.number} with itself.")
        '''
        Ponizsza implementacja dziala
        rowniez dla `self is other_account`, ale przypadek ten
        i tak odrzucamy co do zasady:
        '''
        n = self.balance
        self.withdraw(n)
        other_account.deposit(n)

    def __str__(self):
        return "Account number {}, balance: {}".format(self.number, self.balance)


if __name__ == "__main__":
    account1 = BankAccount("ACCOUNT-0001", 2000)
    account2 = BankAccount("ACCOUNT-0002", 10000)
    account1.deposit(4000)
    account2.deposit(10000)
    # account1.merge_to(account1)
    print(account1)
    print(account2)
    account1.merge_to(account2)
    print(account1)
    print(account2)


