0

I have to create a bankaccount class that has functions such as: deposit, withdraw, getbalance; all that beginner stuff. All very easy and I finished it no problem. However, there is a function called Interest that takes in an interest_rate where I am running into a lot of trouble. To explain, this function is supposed to add interest per the user's request only once a month to the balance of the bankaccount. So let's say I decide to add 5% interest on today's date (Oct 13) to my balance. The function would execute and spit out my new balance. Then let's say I want to add 10% interest the next day (Oct 14th), the function would not execute and would spit out something like "Cannot add interest until Nov.1", which is the first of the next month. On Nov.1, if I tried to add 10% interest, I would succeed and then if on Nov. 2 I tried to add interest again, it would say "Cannot add interest until Dec.1" so on and so forth. I'm having a very hard time with this. Below is the code that I wrote, however it would not only ever execute because the date will always be ahead a month, but it would also always add interest to the balance.

Here's what I have so far, but it's a mess. Anyone have any tips on what I should do? I know it's not inside a function the way it's supposed to be in the assignment but I thought I would figure out the main picture of the program first and then worry about incorporating it into the rest of my class.

from datetime import *
from dateutil.relativedelta import *


rate = 0.10
balance = 1000.00 # i just made up a number for the sake of the code but the
#  balance would come from the class itself
balancewInterest = rate*balance
print(balancewInterest)

# this is where I'm having the most trouble
todayDate = date.today()
print(todayDate)
todayDay = todayDate.day
todayMonth = todayDate.month
if todayDay == 1: # if it's the first of the month, just change the month
    # not the day
    nextMonth = todayDate + relativedelta(months=+1)
else: # if not the 1st of the month, change day to 1, and add month
    nextMonth = todayDate + relativedelta(months=+1, days=-(todayDay-1))
print(nextMonth)
difference = (todayDate - nextMonth)
print(difference.days)

if todayDate >= nextMonth: # add interest only when it's the first of the next
    # month or greater
     interestPaid = rate*balance
     print(interestPaid)
else:
    print("You can add interest again in " + str(abs(difference.days)) \
    + " days.")
mmerjem
  • 11
  • 1
  • This is not a class, you mention in your question you create a BankAccount class, can you add that to the question? – Isma Oct 13 '18 at 21:17
  • I'm not having trouble with the class I'm having trouble with the particular function that adds interest to the balance – mmerjem Oct 13 '18 at 21:23
  • The specifications as exposed in your question are not completely clear, but if you want your program to work this way, you need to store the information that the interests were added for some month somewhere. This should be in the class, or in some file/db if you want this to work when the program is stopped and launched again. – Thierry Lathuille Oct 13 '18 at 21:25
  • I think the calculation is: balancewInterest = (rate * balance) + balance – SynchroDynamic Oct 13 '18 at 21:31
  • Ive learned more about calculating monthly interest from SO than coding :S – vash_the_stampede Oct 13 '18 at 21:36

2 Answers2

0

I have now completed how to do it.

import datetime
import os
import time
x = datetime.datetime.now()

try:
    s = open("Months.txt","r")
    ss = s.read()
    s.close()
except IOError:
    s = open("Months.txt","w")
    s.write("Hello:")
    s.close()
try:
    Mo = open("Deposit and interest.txt","r")
    Mon = Mo.read()
    Mo.close()
except IOError:
    Deposit = input("How much do you wish to deposit")
    M = open("Deposit and interest.txt","w")
    M.write(Deposit)
    M.close()
s1 = open("Months.txt","r")
s2 = s1.read()
s1.close()

Mone = open("Deposit and interest.txt","r")
Money = Mone.read()
Mone.close()

if s2 != x.strftime("%B"):
    Rate = input("How much interest do you want?")
    Deposit1 = int(Money)/int(100)
    Deposit2 = Deposit1*int(Rate)
    Deposit = int(Money) + int(Deposit2)
    print("Calculation of interest")
    time.sleep(2)
    print("The final total is: "+str(Deposit))
    os.remove("Months.txt")
    file_one=open("Months.txt","w")
    file_one.write(x.strftime("%B"))
    file_one.close()
    os.remove("Deposit and interest.txt")
    file_one=open("Deposit and interest.txt","w")
    file_one.write(str(Deposit))
    file_one.close()
    print("Used up this month, try again next month")
else:
    print("Sorry, you have used this months intrest limit, try again next month")
Tommy Lawrence
  • 300
  • 1
  • 12
-3
    def calculate_interest(amount_deposited, time):
        rate_of_interest = 5
        int rest = (amount_deposited*time*rate)
        return int rest
        
        
    print(calculate_interest(1000, 2))


''' for eg: here 1000 is the amount deposited and for 2 years at the rate of interest 5.'''
Rohit P
  • 1
  • 2
  • OP is not interested in the very basics of calculus. Read their question: what they want is a way to do this **once a month**. *That* Is the question. – Eric Aya Dec 26 '20 at 13:24