对象之间的加减值

2024-09-30 00:24:46 发布

您现在位置:Python中文网/ 问答频道 /正文

如何在两个对象之间加减值?我有一个任务,我需要做一些银行帐户作为一个对象。在他们之间转账。你知道吗

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()

所以现在我做了一些方法,从一个物体上加上和减去x的钱。但我怎样才能把它和账户联系起来。举个例子,如果我从acc1减去100,再加100到acc2。你知道吗


Tags: 对象nameselfaddnumberyourdefshow
2条回答

我建议上两节课。一个是客户本身,另一个是客户经理,类似这样:

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()

因此,现在您可以更好地控制您的帐户,并为每个帐户设置单独的逻辑。当然,如果你想更好地控制账户,你可以改进这一点,也许可以使用字典。你知道吗

希望有帮助!你知道吗

如果您有两种方法,分别用于转账记入贷方的帐户和转账到记入借方的帐户,则该程序将起作用。你知道吗

我不能保证添加下面的代码是最科学的实现,但它可以按预期工作。你知道吗

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')

相关问题 更多 >

    热门问题