how can I add and subtract values between two objects? I've got a task and I need to make some kind bank accounts as an objects. And transfer money between them.
class Account():
def __init__(self, name, balance, number):
self.name = name
self.balance = balance
self.number = number
def transfer(self, amount):
self.amount = amount
if self.balance >= amount:
self.balance = self.balance - amount
else:
print("Sorry but you do not have enough funds on your account")
def add(self, add):
self.add = add
self.balance = self.balance + add
def show(self):
print("Hello", self.name, "your current balance is:", self.balance)
acc_1 = Account("Darren William", 2000.50, 3694586)
acc_2 = Account("Jamie Lanister", 7500.34, 3687756)
acc_1.show()
acc_1.transfer(300.89)
acc_1.show()
acc_1.add(500.47)
acc_1.show()
So right now I did methods to add and subtract x amount of money from one object. But how can I connect it between account. For an example if I subtract from acc_1 100 add 100 to acc_2.
I suggest to make two classes. One is the Account itself and the other one is the Accounts Manager, something like this:
class Account():
def __init__(self, name, balance, number):
self.name = name
self.balance = balance
self.number = number
# This method checks if we have enough money
def hasBalance(self, amount):
if amount <= self.balance:
return True
else:
print("Sorry but you do not have enough funds on your account")
return False
# We need to check twice if you have the money to be sure
def transfer(self, amount):
if self.hasBalance(amount):
self.balance = self.balance - amount
def add(self, add):
self.balance = self.balance + add
class AccountsManager():
def __init__(self):
self.accounts = []
def addAccount(self, clientName, clientBalance, clientId):
self.accounts.append(Account(clientName, clientBalance, clientId))
# This method has 3 parameters
# fromNumber : Client ID from the first account (sender)
# toNumber : Client ID from the second account (recipent)
# amount : Amount of money to transfer
def transfer(self, fromNumber, toNumber, amount):
fromAccount = self.getById(fromNumber)
toAccount = self.getById(toNumber)
if fromAccount != None:
if toAccount != None:
if fromAccount.hasBalance(amount):
print ("Transfer completed successfully!")
fromAccount.transfer(amount)
toAccount.add(amount)
else:
print ("Recipent account do not exist!")
else:
print ("Sender account do not exist!")
# Check in the accounts array if there is an Account objet with
# the number property equals to 'clientId'
def getById (self, clientId):
for account in self.accounts:
if account.number == clientId:
return account
else:
account = None
def getInfo(self):
for account in self.accounts:
print("->", account.name, account.balance, account.number)
accountsManager = AccountsManager()
accountsManager.addAccount("Darren William", 100, 1)
accountsManager.addAccount("Jamie Lanister", 100, 2)
accountsManager.getInfo()
accountsManager.transfer(1,2,25)
accountsManager.getInfo()
So now you can control in a better way your accounts and have separate logics for each one of it. Sure, you can improve this if you want better control over the accounts, maybe using dictionaries.
Hope it helps!
if you have the two methods for transfer for the account that gets credited with the amount and transfer_to for the account that gets debited, program would work.
I can't guarantee that addition of below code is most scientific implementation but it works as desired.
def transfer(self, amount):
self.balance += amount
def transfer_to(self, amount, dest_account):
if self.balance >= amount:
self.balance -= amount
dest_account.transfer(amount)
print('Balance',amount,'transferred successfully')
else:
print('Error. You don\'t have enough balance in your account')
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.