Advertisement
Guest User

Untitled

a guest
Feb 9th, 2016
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.63 KB | None | 0 0
  1. import sys
  2. import re
  3.  
  4. ## run CreditLine.py with -u flag (unbuffered )
  5. ## python -u CreditLine.py
  6. ## -a flag : set APR, will read in input as decimal unless '%' specified
  7. ## default APR is 35%
  8. ## -l flag : set Credit Limit, will read in input as decimal
  9. ## default Credit Limit is 1000
  10.  
  11. ## TO IMPLEMENT FURTHER:
  12. ## - Better way for two decimal point rounding or treat money as integers (dollars & cents)
  13. ## - clean up transaction_list and add further functionality (e.g. show payments and draws)
  14. ## - don't use global variables
  15.  
  16. ## Default APR and Credit Limit
  17. APR = .35
  18. CREDIT_LIMIT = 1000
  19. DAY = 0
  20.  
  21. BALANCE = 0
  22.  
  23. ## transactions_list = [[Day, 'Drew'/'Paid', amount, Remaining Credit, Principial Balance], [...], ...]
  24. transactions_list = []
  25.  
  26.  
  27. ## TO IMPLEMENT FURTHER FUNCTIONALITY:
  28. ## instead of ending program after payment period end, start a new period
  29. # def start(usrInput):
  30. # APR = .35
  31. # CREDIT_LIMIT = 1000
  32. # DAY = 0
  33. # BALANCE = 0
  34.  
  35. ## input: Boolean for end of period
  36. ## Function: Prints transactions and current results
  37. def print_summary(end):
  38. print_results(end)
  39. if not transactions_list:
  40. print("You have not made any transactions!")
  41. return
  42. for item in transactions_list:
  43. print("Day " + str(item[0]) + ": " + item[1] + " $" + str(item[2]) +
  44. "\n> Remaining Credit Limit: $" + str(item[3]) + "\n> Principal Balance: $" + str(item[4]))
  45.  
  46. ## input: Boolean for end of period
  47. ## Function: Prints current results
  48. def print_results(end):
  49. global DAY, CREDIT_LIMIT, BALANCE, transactions_list
  50. print("Today is Day " + str(DAY))
  51. if (end):
  52. interest = calc_interest()
  53. print("> Principal Balance: $%.2f" % BALANCE)
  54. print("> Total Interest Payment: $%.2f" % interest)
  55. print("> Total Payment: $%.2f" % (BALANCE + interest))
  56. sys.exit(">>> Credit Line quitting")
  57. else:
  58. print("> Remaining Credit Limit: $%.2f" % CREDIT_LIMIT)
  59. print("> Principal Balance: $%.2f" % BALANCE)
  60. print("> Current Total Interest: $%.2f" % calc_interest())
  61.  
  62. ## Function: Calculates interest
  63. ## transactions_lust = [[Day, 'Drew'/'Paid', amount, Remaining Credit, Principial Balance],
  64. ## [...],
  65. ## ...
  66. ## [...]]
  67. def calc_interest():
  68. global DAY, transactions_list, APR, BALANCE
  69. interest = 0
  70. for i in xrange(1, len(transactions_list)):
  71. ## for each day period, calculates interest
  72. interest += transactions_list[i-1][4] * APR/(float(365)) * (transactions_list[i][0] - transactions_list[i-1][0])
  73. ## adds interest for final balance
  74. if (len(transactions_list) == 0):
  75. return interest
  76. interest += BALANCE * APR/(float(365)) * (DAY - transactions_list[len(transactions_list) - 1][0])
  77. return interest
  78.  
  79. def draw_fun(amount):
  80. global DAY, CREDIT_LIMIT, BALANCE, transactions_list
  81. if (amount < CREDIT_LIMIT):
  82. CREDIT_LIMIT -= amount
  83. BALANCE += amount
  84. transactions_list.append([DAY, 'Drew', amount, CREDIT_LIMIT, BALANCE])
  85. else:
  86. print(">>> You can't draw more than credit limit!")
  87. print_results(False)
  88.  
  89. def pay_fun(amount):
  90. global DAY, CREDIT_LIMIT, BALANCE, transactions_list
  91. if (amount < BALANCE):
  92. BALANCE -= amount
  93. CREDIT_LIMIT += amount
  94. transactions_list.append([DAY, 'Paid', amount, CREDIT_LIMIT, BALANCE])
  95. else:
  96. print(">>> You don't owe that much money!")
  97. print_results(False)
  98.  
  99. def is_number(s):
  100. try:
  101. float(s)
  102. except ValueError:
  103. return False
  104. return (float(s) >= 0.01)
  105.  
  106. if __name__ == "__main__":
  107.  
  108. ## Sets the APR and Credit Limit
  109. for i in xrange(1,len(sys.argv)):
  110. if sys.argv[i] == '-a':
  111. APR = float(re.sub(r'[^\d.]+', '', sys.argv[i+1])) # strips to leave decimal
  112. if ('%' in sys.argv[i+1]):
  113. APR = APR * 0.01
  114. if sys.argv[i] == '-l':
  115. CREDIT_LIMIT = float(re.sub(r'[^\d.]+', '', sys.argv[i+1]))
  116.  
  117. print ("APR: %.2f" % (APR*100) + '%')
  118. print ("Line of Credit: $%.2f" % CREDIT_LIMIT)
  119. print ("Type 'h' for help and list of commands")
  120.  
  121.  
  122. ## Implemented some basic typechecking
  123. while True:
  124. if (DAY == 30):
  125. print_summary(True)
  126. usrInput = sys.stdin.readline().rstrip('\n').split()
  127.  
  128. for i in xrange(0, len(usrInput)):
  129.  
  130. if (usrInput[i] in ['quit', 'Quit', 'q']):
  131. print_summary(True)
  132. sys.exit(">>> Credit Line quitting")
  133.  
  134. elif (usrInput[i] in ["Next", "next", 'n']): # Next DAY
  135. if (DAY < 30):
  136. DAY += 1
  137. print_results(False)
  138. else:
  139. print(">>> End of Period, cannot go to next day")
  140.  
  141. elif (usrInput[i] in ["Day", 'day']): # Skips to that DAY
  142. if is_number(usrInput[i+1]) == False:
  143. print("Please input a valid integer number for Day")
  144. elif (int(usrInput[i+1]) > DAY) and (int(usrInput[i+1]) <= 30):
  145. DAY = int(usrInput[i+1])
  146. print_results(False)
  147. else:
  148. DAY = 30
  149. print_summary(True)
  150.  
  151. elif (usrInput[i] in ["Draw", "draw", 'd']):
  152. if is_number(usrInput[i+1]) == False:
  153. print("Please input a valid two decimal point number for draw amount")
  154. else:
  155. draw_fun(float(re.sub(r'[^\d.]+', '', usrInput[i+1])))
  156.  
  157. elif (usrInput[i] in ["Pay", "pay", 'p']):
  158. if is_number(usrInput[i+1]) == False:
  159. print("Please input a valid two decimal point number for pay amount")
  160. else:
  161. pay_fun(float(re.sub(r'[^\d.]+', '', usrInput[i+1])))
  162.  
  163. elif (usrInput[i] in ["Summary", "summary", 's']):
  164. print_summary(False)
  165.  
  166. elif (usrInput[i] in ["Help", "help", 'h']):
  167. print("This program calculates your total payment at the end of the 30-day period.")
  168. print("Commands are not case sensitive. Separate commands by spaces or press enter")
  169. print("Type 'q' or 'quit' to quit the program")
  170. print("Type 's' or 'summary' for a Transaction Summary")
  171. 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")
  172. print("Type 'd' or 'draw' to draw money")
  173. print("Type 'p' or 'pay' to pay money")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement