You should call methods on self
, not by passing the functions around:
def getMonthlyInterest(self):
return self.getBalance() * self.getMonthlyInterestRate()
and call it with:
print(accountA.getMonthlyInterest())
This goes for getMonthlyInterestRate
as well:
def getMonthlyInterestRate(self):
return self.getAnnualInterest() / 12
You use a lot of getters and setters; there is no need for these in Python; you don't need to make the attributes private, just access them directly instead:
class Account:
def __init__(self, id=0, balance=100.0, annualInterestRate=0.0):
self.id = id
self.balance = balance
self.annualInterestRate = annualInterestRate
def getMonthlyInterestRate(self):
return self.annualInterestRate / 12
def getMonthlyInterest(self):
return self.balance * self.getMonthlyInterestRate()
def withdraw(self, amount):
if amount <= self.balance:
self.balance -= amount
def deposit(self, amount):
self.balance += amount
def __str__(self):
return "Account ID : {0.id} Account Ballance : {0.balance} Annual Interest Rate : {0.annualInterestRate}".format(self)
then run:
def main():
accountA = Account(0,100,0)
accountA.id = 1234
accountA.balance = 20500
accountA.annualInterestRate = 0.375
print(accountA)
accountA.withdraw(500)
accountA.deposit(1500)
print(accountA)
print(accountA.getMonthlyInterest())
Result:
Account ID : 1234 Account Ballance : 20500 Annual Interest Rate : 0.375
Account ID : 1234 Account Ballance : 21500 Annual Interest Rate : 0.375
671.875