In: Computer Science
Python question Design a SavingsAccount class that stores a savings account's annual interest rate and balance. The class constructor should accept the amount of the savings account;s starting balance. The class should also have methods for subtracting the amount of a withdraw, adding the amount of a deposit, and adding the amount of monthly interest to the balance. The monthly interest rate is the annual interest rate divided by twelve. To add the monthly interest to the balance, multiply the monthly interest rate by the balance, and add the result to the balance. Test the class in a program that calculates the balance of a savings account at the end of a period of time. it should ask the user for the annual interest rate, the starting balance, and the number of months that have passed since the account was established. A loop should then iterate once for every month, preforming the following: a. ask the user for the amount deposited into the account during the month. use the class method to add this amount to the account balance. b. ask the user for the amount withdrawn from the account during the month. use the class method to subtract this amount from the account balance. c. use the class method to calculate the monthly interest. After the last iteration, the program should display the ending balance, the total amount of deposits, the total amount of withdrawals, and the total interest earned. What I have so far: class SavingsAccount: def __init__(self,balance): self.__balance = balance self.__annualRate = 0 self.__monthInterest = 0 def withdrawl(self, amt): self.__balance -= amt def deposit(self, amt): self.__balance += amt def set_interestRate(self,rate): self.__annualRate = rate #This method calculates mothly interest based on the annual interest rate and adds it to the balance def mth_interest(self): self.__monthInterest = self.__annualRate/12 * self.__balance self.__add_mth_interest(self.__monthInterest) return self.__monthInterest def __add_mth_interest(self, interest): self.__balance += interest def get_balance(self): return self.__balance
Thanks for the question. Your class is all good, I have written the main function, that demonstrates your class. Here is the updated code with screenshot. =========================================================================== class SavingsAccount: def __init__(self, balance): self.__balance = balance self.__annualRate = 0 self.__monthInterest = 0 def withdrawl(self, amt): self.__balance -= amt def deposit(self, amt): self.__balance += amt def set_interestRate(self, rate): self.__annualRate = rate # This method calculates mothly interest based on the annual interest rate and adds it to the balance def mth_interest(self): self.__monthInterest = self.__annualRate / 1200 * self.__balance self.__add_mth_interest(self.__monthInterest) return self.__monthInterest def __add_mth_interest(self, interest): self.__balance += interest def get_balance(self): return self.__balance def main(): rate = float(input('Enter interest rate (%): ')) balance = float(input('Enter starting balance: ')) months = int(input('Enter the number of months: ')) savingsAcct = SavingsAccount(balance) savingsAcct.set_interestRate(rate) total_deposit = 0 total_withdrawn = 0 interest_earned = 0 for month in range(1, months + 1): amount = float(input('Enter amount deposited for month {}: '.format(month))) total_deposit += amount savingsAcct.deposit(amount) amount = float(input('Enter amount withdrawn for month {}: '.format(month))) total_withdrawn += amount savingsAcct.withdrawl(amount) interestEarned = savingsAcct.mth_interest() interest_earned += interestEarned print('Total Deposited Amount: ${:.2f}'.format(total_deposit)) print('Total Withdrawn Amount: ${:.2f}'.format(total_withdrawn)) print('Total Interest Earned : ${:.2f}'.format(interest_earned)) print('Ending Balance : ${:.2f}'.format(savingsAcct.get_balance())) main()
======================================================================