Advertisement
LDDestroier

(schoolwork) business math 8.5 table

Mar 20th, 2018
211
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. local nprompt = function(q)
  2.     print(q)
  3.     term.blit("> ","3 ","  ")
  4.     return tonumber(read())
  5. end
  6.  
  7. local round = function(num)
  8.     return math.floor((num*100)+0.5)/100
  9. end
  10.  
  11. term.clear()
  12. term.setCursorPos(1,1)
  13.  
  14. local amntOfMonths = 12 --nprompt("Length of loan? (months)")
  15. local monthlyPayment = nprompt("Monthly payment?")
  16. local principle = nprompt("Principle?")
  17. local interestRate = nprompt("Interest rate?")/100
  18. local payoffMonths = nprompt("Payoff after how long? (months)")
  19. local totalMonths = nprompt("Total months of loan?")
  20.  
  21. local getIt = function(payoffMonths)
  22.    
  23.     output = {{}}
  24.     output[1].interest = principle * interestRate * (1/amntOfMonths)
  25.     output[1].principle = monthlyPayment - output[1].interest
  26.     output[1].newPrinciple = principle - output[1].principle
  27.    
  28.     for a = 2, payoffMonths do
  29.         output[a] = {}
  30.         output[a].interest = output[a-1].newPrinciple * interestRate * (1/amntOfMonths)
  31.         output[a].principle = monthlyPayment - output[a].interest
  32.         output[a].newPrinciple = output[a-1].newPrinciple - output[a].principle
  33.     end
  34.  
  35.     output[#output].principle = output[#output-1].newPrinciple
  36.     output[#output].newPrinciple = 0
  37.     finalPayment = output[#output].interest + output[#output].principle
  38.     return output,finalPayment
  39. end
  40.  
  41. local printOutput = function(output,finalPayment)
  42.  
  43.     local max = {}
  44.     for a = 1, #output do
  45.         max.interest = math.max(#tostring(output[a].interest), max.interest or 0)
  46.         max.principle = math.max(#tostring(output[a].principle), max.principle or 0)
  47.         max.newPrinciple = math.max(#tostring(output[a].newPrinciple), max.newPrinciple or 0)
  48.     end
  49.     for k,v in pairs(max) do
  50.         max[k]=v+1
  51.     end
  52.    
  53.     term.setCursorPos(1,1)
  54.     term.write("Int.")
  55.     term.setCursorPos(max.interest+1,1)
  56.     term.write("Pri.")
  57.     term.setCursorPos(max.interest+max.principle+1,1)
  58.     term.write("NewPri.")
  59.    
  60.     for a = 1, #output do
  61.         term.setCursorPos(1,a+1)
  62.         term.write(round(output[a].interest))
  63.         term.setCursorPos(max.interest+1,a+1)
  64.         term.write(round(output[a].principle))
  65.         term.setCursorPos(max.interest+max.principle+1,a+1)
  66.         term.write(round(output[a].newPrinciple))
  67.     end
  68.  
  69.     term.setCursorPos(1,#output+3)
  70.  
  71.     print("Final payment: "..tostring(round(finalPayment)))
  72. end
  73.  
  74. local tab1, finalPay1 = getIt(payoffMonths)
  75. local tab2, finalPay2 = getIt(totalMonths)
  76. term.clear()
  77. printOutput(tab1,finalPay1)
  78. print("You saved: "..round(finalPay1-finalPay2))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement