#๐Ÿ”’ OOP - Bank account program, confused how to structure

38 messages ยท Page 1 of 1 (latest)

shell junco
#

I have a class BankAccount that has methods like deposit, withdraw, etc. I have 2 questions:

  • How can I "pay" other members (transfer values between instances of BankAccount)
  • I want to build a casino for the bank members to gamble with. Would I create a separate class, file, or just have the functions for the casino outside of the BankAccount class? Preferably I would have a separate casino class but I don't understand OOP and inheritance enough to do so.
crisp flameBOT
#

@shell junco

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

mellow tide
shell junco
#

Sure

#

sorry its too big to post all in here

dusk nymph
shell junco
#

so any transferring happens outside the class itself?

dusk nymph
#

You can set the transfer as a class method

#

Ensuring that the arg is another BankAccount class

#
def transfer(self, amount, otheraccount:BankAccount):
  self.withdraw(amount)
  otheraccount.deposit(amount)
  ...

Something like that

shell junco
#

would otheraccount be whatever the other instance is named?

dusk nymph
mellow tide
#

This is my blind take, but you can have a static or class method, that takes both users as an argument.


class BankAccount
  ...
  
  @staticmethod
  def exchange(sender: BankAccount, reciever: BankAccount, amount: float | int):
    if sender.balance() - amount < 0:
      return OR raise
    sender.withdraw(amount)
    reciever.deposit(amount)

Id argue Agent Q's idea is better. Sorry, got caught up typing this.

dusk nymph
#

You don't really need to know what the variable/named instance is. Just know that it has to be an instance of BankAccount

shell junco
#

so if I had two accounts in my bank, Draco and Agent_Q I'd use self as the sender when calling Draco.transfer(amount) and Agent_Q would be referenced as Agent_Q: BankAccount as a parameter correct?

mellow tide
#

sorry wrong reply

mellow tide
shell junco
#

would it ever make sense to call it as the receiver given the context? usually a bank transfer is initiated by the sender

mellow tide
#

No it wouldn't but it all comes down to how you wish to implement it

shell junco
#

and thanks for taking the time both of you, been trying to nail down OOP forever

mellow tide
#

No problem lol

mellow tide
dusk nymph
mellow tide
#

I'd argue the method would better off being within a Bank class if you have one

shell junco
#
        recipient.account_balance += amount```

```Draco = BankAccount("Draco", 500, 5)
John = BankAccount("John", 500, 0)```

`Draco.transfer(John, 500)`

this seemed to work pretty well now I just have do all the logic
#
    def transfer(self, recipient, amount):
        if isinstance(recipient, BankAccount):
            if amount <= self.account_balance: 
                recipient.account_balance += amount
                self.account_balance -= amount
            else: print("Not enough money to make this transfer. ")
        else: print("This user is not a member of the bank.")``` got it down
dusk nymph
shell junco
#

is it because i'm rewriting?

dusk nymph
#

that's one reason

#

the other is common sense. The act of changing the state/behavior of one class is already defined. Might as well reuse it.

shell junco
#
        if isinstance(recipient, BankAccount):
            if amount <= self.account_balance: 
                recipient.deposit(amount)
                self.withdraw(amount)
            else: print("Not enough money to make this transfer. ")``` I kept the conditional at the top for semantics. If it just used the conditional of withdraw, I'd need a handler to show a different message upon failure
merry rivet
dusk nymph
#

I would add a return True or False to each function

#

So if withdraw returned False, it wouldn't do the deposit

merry rivet
#

something like this

if not isinstance(other, BankAccount):
   #return False
   raise NotImplementedError

if self.bal < amount:
   #return False
   raise ValueError("Not enough money.")

self.withraw(amount)
other.deposite(amount)
#return True
crisp flameBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.