Home > database >  How to mak a transaction method in a BankAccount class?
How to mak a transaction method in a BankAccount class?

Time:10-23

I am a rookie who tries to learn OOP in python. For the sake of learning and practice, I am working on a task that asks me to create a transaction method in the class BankAccount that transfers money from one bank account to another.

Here is the code I have written so far:

class BankAccount:
    def __init__(self, first_name, last_name, number, balance):
        self._first_name = first_name
        self._last_name = last_name
        self._number = number
        self._balance = balance         

    def deposit(self, amount):
        self._balance  = amount

    def withdraw(self, amount):
        self._balance -= amount

    def get_balance(self):
        return self._balance

    def transfer(self, amount_out):
        self.withdraw(amount_out)

        amount_in = amount_out                          #This is where i am unsure/stuck
        self.deposit(amount_in)

    def print_info(self):
        first = self._first_name
        last = self._last_name
        number = self._number
        balance = self._balance

        s = f"{first} {last}, {number}, balance: {balance}"

        print(s)

def main():

    account1 = BankAccount("Jesse", "Pinkman", "19371554951", 20_000)
    account2 = BankAccount("Walter", "White", "19371564853",500)

    a1.transfer(200)

    a1.print_info()
    a2.print_info()

main()

My question is: How can you make the transaction class in such a way that it transfers money from one BankAccount object to another?

Is there someone who is kind enough to help a motivated rookie out?

All help is massively welcomed and appriciated.

CodePudding user response:

If I were doing this task, I'd write transfer like this:

def transfer(self, other, amount):
    self.withdraw(amount)
    other.deposit(amount)

Then, let's say you wanted to transfer $100 from account1 to account2, you'd call the function like so: account1.transfer(account2, 100).

In your snippet of code, it would look like this:

class BankAccount:
    def __init__(self, first_name, last_name, number, balance):
        self._first_name = first_name
        self._last_name = last_name
        self._number = number
        self._balance = balance

    def deposit(self, amount):
        self._balance  = amount

    def withdraw(self, amount):
        self._balance -= amount

    def get_balance(self):
        return self._balance

    def transfer(self, other, amount_out):
        self.withdraw(amount_out)
        other.deposit(amount_out)

    def print_info(self):
        first = self._first_name
        last = self._last_name
        number = self._number
        balance = self._balance

        s = f"{first} {last}, {number}, balance: {balance}"

        print(s)


def main():

    a1 = BankAccount("Jesse", "Pinkman", "19371554951", 500)
    a2 = BankAccount("Walter", "White", "19371564853", 500)

    a1.transfer(a2, 200)

    a1.print_info()
    a2.print_info()


main()
  • Related