Python Accounting
(The Account class) Design a class named Account that contains:
■ A private int data field named id for the account.
■ A private float data field named balance for the account.
■ A private float data field named annualInterestRate that stores the current interest rate.
■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0).
■ The accessor and mutator methods for id, balance, and annualInterestRate.
■ A method named getMonthlyInterestRate() that returns the monthly interest rate.
■ A method named getMonthlyInterest() that returns the monthly interest.
■ A method named withdraw that withdraws a specified amount from the account.
■ A method named deposit that deposits a specified amount to the account.
main()
■ A private int data field named id for the account.
■ A private float data field named balance for the account.
■ A private float data field named annualInterestRate that stores the current interest rate.
■ A constructor that creates an account with the specified id (default 0), initial balance (default 100), and annual interest rate (default 0).
■ The accessor and mutator methods for id, balance, and annualInterestRate.
■ A method named getMonthlyInterestRate() that returns the monthly interest rate.
■ A method named getMonthlyInterest() that returns the monthly interest.
■ A method named withdraw that withdraws a specified amount from the account.
■ A method named deposit that deposits a specified amount to the account.
Write a test program that creates an Account object with an account id of 1122, a balance of $20,000, and an annual interest rate of 4.5%. Use the withdraw method to withdraw $2,500, use the deposit method to deposit $3,000, and print the id, balance, monthly interest rate, and monthly interest.
class Account():
def __init__(self, id = 0, balance = 100, annualInterestRate = 0):
self.__id = id
self.__balance = balance
self.__annualInterestRate = annualInterestRate
def getId(self):
return self.__id
def getBalance(self):
return self.__balance
def getAnnualInterestRate(self):
return self.__annualInterestRate
def setId(self, id):
self.__id = id
def setBalance(self, balance):
self.__balance = balance
def setAnnualInterestRate(self, annualInterestRate):
self.__annualInterestRate = annualInterestRate
def getMonthlyInterestRate(self):
mir = (self.__annualInterestRate / 100) / 12
return round(mir, 3)
def getMonthlyInterest(slef):
mi = slef.__balance * Account.getMonthlyInterestRate(slef)
return round(mi, 3)
def withdraw(self, amount):
self.__balance = self.__balance - amount
def deposit(self, amount):
self.__balance = self.__balance + amount
def main():
t = Account(1122, 20000, 4.5)
print('Customer ID: {}, Account balance: {}, Mountly interest rate: {},'
' Mountly interest: {}'.format(t.getId(), t.getBalance(), t.getMonthlyInterestRate(),t.getMonthlyInterest()))
t.withdraw(2500)
t.deposit(3000)
print('Customer ID: {}, Account balance: {}, Mountly interest rate: {},'
' Mountly interest: {}'.format(t.getId(), t.getBalance(), t.getMonthlyInterestRate(),t.getMonthlyInterest()))
def __init__(self, id = 0, balance = 100, annualInterestRate = 0):
self.__id = id
self.__balance = balance
self.__annualInterestRate = annualInterestRate
def getId(self):
return self.__id
def getBalance(self):
return self.__balance
def getAnnualInterestRate(self):
return self.__annualInterestRate
def setId(self, id):
self.__id = id
def setBalance(self, balance):
self.__balance = balance
def setAnnualInterestRate(self, annualInterestRate):
self.__annualInterestRate = annualInterestRate
def getMonthlyInterestRate(self):
mir = (self.__annualInterestRate / 100) / 12
return round(mir, 3)
def getMonthlyInterest(slef):
mi = slef.__balance * Account.getMonthlyInterestRate(slef)
return round(mi, 3)
def withdraw(self, amount):
self.__balance = self.__balance - amount
def deposit(self, amount):
self.__balance = self.__balance + amount
def main():
t = Account(1122, 20000, 4.5)
print('Customer ID: {}, Account balance: {}, Mountly interest rate: {},'
' Mountly interest: {}'.format(t.getId(), t.getBalance(), t.getMonthlyInterestRate(),t.getMonthlyInterest()))
t.withdraw(2500)
t.deposit(3000)
print('Customer ID: {}, Account balance: {}, Mountly interest rate: {},'
' Mountly interest: {}'.format(t.getId(), t.getBalance(), t.getMonthlyInterestRate(),t.getMonthlyInterest()))
main()
SOLUTION:
Customer ID: 1122, Account balance: 20000, Mountly interest rate: 0.004, Mountly interest: 80.0
Customer ID: 1122, Account balance: 20500, Mountly interest rate: 0.004, Mountly interest: 82.0
Коментари
Постави коментар