Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import sys
- import re
- ## run CreditLine.py with -u flag (unbuffered )
- ## python -u CreditLine.py
- ## -a flag : set APR, will read in input as decimal unless '%' specified
- ## default APR is 35%
- ## -l flag : set Credit Limit, will read in input as decimal
- ## default Credit Limit is 1000
- ## TO IMPLEMENT FURTHER:
- ## - Better way for two decimal point rounding or treat money as integers (dollars & cents)
- ## - clean up transaction_list and add further functionality (e.g. show payments and draws)
- ## - don't use global variables
- ## Default APR and Credit Limit
- APR = .35
- CREDIT_LIMIT = 1000
- DAY = 0
- BALANCE = 0
- ## transactions_list = [[Day, 'Drew'/'Paid', amount, Remaining Credit, Principial Balance], [...], ...]
- transactions_list = []
- ## TO IMPLEMENT FURTHER FUNCTIONALITY:
- ## instead of ending program after payment period end, start a new period
- # def start(usrInput):
- # APR = .35
- # CREDIT_LIMIT = 1000
- # DAY = 0
- # BALANCE = 0
- ## input: Boolean for end of period
- ## Function: Prints transactions and current results
- def print_summary(end):
- print_results(end)
- if not transactions_list:
- print("You have not made any transactions!")
- return
- for item in transactions_list:
- print("Day " + str(item[0]) + ": " + item[1] + " $" + str(item[2]) +
- "\n> Remaining Credit Limit: $" + str(item[3]) + "\n> Principal Balance: $" + str(item[4]))
- ## input: Boolean for end of period
- ## Function: Prints current results
- def print_results(end):
- global DAY, CREDIT_LIMIT, BALANCE, transactions_list
- print("Today is Day " + str(DAY))
- if (end):
- interest = calc_interest()
- print("> Principal Balance: $%.2f" % BALANCE)
- print("> Total Interest Payment: $%.2f" % interest)
- print("> Total Payment: $%.2f" % (BALANCE + interest))
- sys.exit(">>> Credit Line quitting")
- else:
- print("> Remaining Credit Limit: $%.2f" % CREDIT_LIMIT)
- print("> Principal Balance: $%.2f" % BALANCE)
- print("> Current Total Interest: $%.2f" % calc_interest())
- ## Function: Calculates interest
- ## transactions_lust = [[Day, 'Drew'/'Paid', amount, Remaining Credit, Principial Balance],
- ## [...],
- ## ...
- ## [...]]
- def calc_interest():
- global DAY, transactions_list, APR, BALANCE
- interest = 0
- for i in xrange(1, len(transactions_list)):
- ## for each day period, calculates interest
- interest += transactions_list[i-1][4] * APR/(float(365)) * (transactions_list[i][0] - transactions_list[i-1][0])
- ## adds interest for final balance
- if (len(transactions_list) == 0):
- return interest
- interest += BALANCE * APR/(float(365)) * (DAY - transactions_list[len(transactions_list) - 1][0])
- return interest
- def draw_fun(amount):
- global DAY, CREDIT_LIMIT, BALANCE, transactions_list
- if (amount < CREDIT_LIMIT):
- CREDIT_LIMIT -= amount
- BALANCE += amount
- transactions_list.append([DAY, 'Drew', amount, CREDIT_LIMIT, BALANCE])
- else:
- print(">>> You can't draw more than credit limit!")
- print_results(False)
- def pay_fun(amount):
- global DAY, CREDIT_LIMIT, BALANCE, transactions_list
- if (amount < BALANCE):
- BALANCE -= amount
- CREDIT_LIMIT += amount
- transactions_list.append([DAY, 'Paid', amount, CREDIT_LIMIT, BALANCE])
- else:
- print(">>> You don't owe that much money!")
- print_results(False)
- def is_number(s):
- try:
- float(s)
- except ValueError:
- return False
- return (float(s) >= 0.01)
- if __name__ == "__main__":
- ## Sets the APR and Credit Limit
- for i in xrange(1,len(sys.argv)):
- if sys.argv[i] == '-a':
- APR = float(re.sub(r'[^\d.]+', '', sys.argv[i+1])) # strips to leave decimal
- if ('%' in sys.argv[i+1]):
- APR = APR * 0.01
- if sys.argv[i] == '-l':
- CREDIT_LIMIT = float(re.sub(r'[^\d.]+', '', sys.argv[i+1]))
- print ("APR: %.2f" % (APR*100) + '%')
- print ("Line of Credit: $%.2f" % CREDIT_LIMIT)
- print ("Type 'h' for help and list of commands")
- ## Implemented some basic typechecking
- while True:
- if (DAY == 30):
- print_summary(True)
- usrInput = sys.stdin.readline().rstrip('\n').split()
- for i in xrange(0, len(usrInput)):
- if (usrInput[i] in ['quit', 'Quit', 'q']):
- print_summary(True)
- sys.exit(">>> Credit Line quitting")
- elif (usrInput[i] in ["Next", "next", 'n']): # Next DAY
- if (DAY < 30):
- DAY += 1
- print_results(False)
- else:
- print(">>> End of Period, cannot go to next day")
- elif (usrInput[i] in ["Day", 'day']): # Skips to that DAY
- if is_number(usrInput[i+1]) == False:
- print("Please input a valid integer number for Day")
- elif (int(usrInput[i+1]) > DAY) and (int(usrInput[i+1]) <= 30):
- DAY = int(usrInput[i+1])
- print_results(False)
- else:
- DAY = 30
- print_summary(True)
- elif (usrInput[i] in ["Draw", "draw", 'd']):
- if is_number(usrInput[i+1]) == False:
- print("Please input a valid two decimal point number for draw amount")
- else:
- draw_fun(float(re.sub(r'[^\d.]+', '', usrInput[i+1])))
- elif (usrInput[i] in ["Pay", "pay", 'p']):
- if is_number(usrInput[i+1]) == False:
- print("Please input a valid two decimal point number for pay amount")
- else:
- pay_fun(float(re.sub(r'[^\d.]+', '', usrInput[i+1])))
- elif (usrInput[i] in ["Summary", "summary", 's']):
- print_summary(False)
- elif (usrInput[i] in ["Help", "help", 'h']):
- print("This program calculates your total payment at the end of the 30-day period.")
- print("Commands are not case sensitive. Separate commands by spaces or press enter")
- print("Type 'q' or 'quit' to quit the program")
- print("Type 's' or 'summary' for a Transaction Summary")
- print("Type 'n' or 'next' to proceed to the Next Day or 'day' followed by a space and integer value to jump to a specific day")
- print("Type 'd' or 'draw' to draw money")
- print("Type 'p' or 'pay' to pay money")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement