Home > Net >  I'm can't return the balance of a bank account project
I'm can't return the balance of a bank account project

Time:07-20

I can't access the get method to return balance and such. the program works but without the get method used. The interpreter just ignore.

class Category:
     def __init__(self, balance = 0):
        self.balance = balance
     def deposit(self, value1, description1 = " "):
        depositHistory = {"amount": value1, "description": description1}       
        self.value1 = value1
        self.balance = self.balance   value1
        print("The value of deposit is: ", value1, " and the ledge account is: ", self.balance)
     def withdraw(self, value2, description2=" "):
        self.value2 = value2
        withdrawHistory = {"amount": value2, "description": description2}
        if self.balance >= value2:
           self.balance = self.balance - value2
           print("The value of withdraw is: ", value2, " and the ledge account is: ", self.balance)
        else:
           print("Insufficent balance!")
     def getDepositHistory(self):
        return depositHistory
     def getBalance(self):
        return self.balance
     def transfer(self, value3):
      if self.balance > value3:
        self.value3 = value3
        self.balance = self.balance - value3
        print("The value of the transference is: ", value3, " and the ledge account is: ", self.balance)
      else:
        print("Insuficient funds!")
      
c = Category(100)
c.deposit(300,"hello")
c.withdraw(200)
c.transfer(50)
c.getDepositHistory```

CodePudding user response:

There are a few places where your could use some improvement. I made some inline comments in the example below describing and one possible solution to your question.

class Category:
                # add 1 empty lines in between methods for readability
    def __init__(self, balance = 0):
        self.balance = balance
        self.depositHistory = []   # put depositHistory in constructor
        self.withdrawHistory = []  # put withdwarHistory in constructor

    def deposit(self, value1, description1=" "):
        deposit = {"amount": value1, "description": description1}
        self.depositHistory.append(deposit)  # append deposit record to history
        self.balance  =  value1   # combine operators where possible
        print("The value of deposit is: ", value1, " and the ledge account is: ", self.balance)

    def withdraw(self, value2, description2=" "):
        withdraw = {"amount": value2, "description": description2} 
        self.withdrawHistory.append(withdraw)  # add withdraw attempt record to history
        if self.balance >= value2:
           self.balance -= value2
           print("The value of withdraw is: ", value2, " and the ledge account is: ", self.balance)
        else:
           print("Insufficent balance!")

    def getDepositHistory(self):
        print(self.depositHistory)
        return self.depositHistory  # get deposit history

    def getWithdrawHistory(self):
        print(self.withdrawHistory) 
        return self.withdrawHistory # get withdraw history

    def getBalance(self):
        print(self.balance)
        return self.balance 

    def transfer(self, value3):
      if self.balance > value3:
        self.balance -= value3
        print("The value of the transference is: ", value3, " and the ledge account is: ", self.balance)
      else:
        print("Insuficient funds!")

# use descriptive variable names, avoid single letters
category1 = Category(100)
category1.deposit(300,"hello")
category1.withdraw(200)
category1.transfer(50)
category1.getDepositHistory()
  • Related