Предлагаю сделать два занятия.Одна - это сама учетная запись, а другая - менеджер учетных записей, что-то вроде этого:
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()
Так что теперь вы можете лучше контролировать свои учетные записи и иметь отдельную логику для каждой из них.Конечно, вы можете улучшить это, если хотите лучше контролировать учетные записи, возможно, используя словари.
Надеюсь, это поможет!