Advertisement
Luninariel

Financial Calculator - WIP

Nov 21st, 2019
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. 'use strict'
  2.  
  3. const readline = require('readline')
  4. const rl = readline.createInterface({
  5.     input: process.stdin,
  6.     output: process.stdout
  7. })
  8.  
  9. function processLoan(number) {
  10.     // Insert code to do whatever with sum here.
  11.     console.log('The Estimated Amount of interest Is: $', number);
  12. }
  13.  
  14.  
  15. //A Boolean Function to test if an input value is an int
  16. function isInt(value) {
  17.     return !isNaN(value) && (function (x) { return (x | 0) === x })(parseFloat(value))
  18. }
  19.  
  20.  
  21. //A Function that Calculates the Tip, by taking the Total Bill and dividing it by the percent to be tipped.
  22. function tip(bill, tipAmount) {
  23.     return (bill * (tipAmount / 100)).toFixed(2)
  24. }
  25.  
  26. //A Function that splits the total amount of the bill, by the number of diners.
  27. function splitBill(numDiners, bill) {
  28.     return (bill /numDiners).toFixed(2)
  29. }
  30. //A function that splits a check among diners evenly, and if they wish to tip informs them of the amount
  31. function checkSplitter() {
  32.     rl.question('Will You Be Splitting The Check? (yes/no) \n ', (answer) => {
  33.         switch (answer) {
  34.             case 'yes':
  35.             case 'Yes':
  36.             case 'y':
  37.             case 'Y':
  38.                 rl.question('How Many Patrons Were There? (Please provide a whole number) ', (numDiners) => {
  39.                     console.log(`Got it, There Were ${numDiners} Patrons\n`)
  40.                     if (isInt(numDiners) == true) {
  41.                         rl.question('What Is The Total Amount Due? (Please Enter A Number Using Two Decimal Places)\n ', (totalBill) => {
  42.                             console.log(`Alright, The Total Amount Due Is: $${totalBill}\n`)
  43.                             if (isNaN(totalBill) == false) {
  44.                                 console.log('So Far An Even Split Would Be $' + splitBill(numDiners, parseFloat(totalBill)) + ' Between ' + numDiners + ' Patrons.\n')
  45.                                 rl.question('Will You Be Leaving Your Server A Tip? (yes/no)) \n', (answer) => {
  46.                                     switch (answer) {
  47.                                         case 'yes':
  48.                                         case 'Yes':
  49.                                         case 'y':
  50.                                         case 'Y':
  51.                                             rl.question('What Percent Tip Would You Like To Leave? (Please Enter A Whole Number) ', (tipAmount) => {
  52.                                                 console.log(`Received: ${tipAmount}`)
  53.                                                 if (isInt(tipAmount) == true && tipAmount > 0) {
  54.                                                     console.log('Alright If Everyone Is Sharing, The Total Cost Per Person Would Be: $' + tip(splitBill(numDiners, totalBill), tipAmount) + ' Otherwise, A Single Person Owes: $' + tip(totalBill, tipAmount) + ' For A Tip.')
  55.                                                     return process.exit(1)
  56.                                                 } else {
  57.                                                     console.log('Stop wasting our time.')
  58.                                                     return process.exit(1)
  59.                                                 }
  60.                                             })
  61.                                             break
  62.  
  63.                                         case 'no':
  64.                                         case 'No':
  65.                                         case 'n':
  66.                                         case 'N':
  67.                                             console.log("That Isn't Very Nice Of You. Your Total Bill Is: $" + splitBill(numDiners, parseFloat(totalBill))+' Per Person \n')
  68.                                             return process.exit(1)
  69.  
  70.                                         case 'exit':
  71.                                         case 'Exit':
  72.                                             return process.exit(1)
  73.  
  74.                                         default:
  75.                                             console.log('Did you even eat here?')
  76.                                             return process.exit(1)
  77.  
  78.                                     }
  79.                                 })
  80.                             } else {
  81.                                 console.log("Did you even eat here?")
  82.                                 return process.exit(1)
  83.                             }
  84.                         })
  85.                     } else {
  86.                         console.log("Did you even eat here?")
  87.                         return process.exit(1)
  88.  
  89.                     }
  90.                 })
  91.                 break
  92.  
  93.             case 'no':
  94.             case 'No':
  95.             case 'n':
  96.             case 'N':
  97.                 rl.question('What was the total bill? (give a numerical value with 2 decimals) ', (totalBill) => {
  98.                     console.log(`Alright, The Total Bill Was: $${totalBill}`)
  99.  
  100.                     if (isNaN(totalBill) == false) {
  101.                         rl.question('Do you want to tip (yes/no) \n', (answer) => {
  102.                             switch (answer) {
  103.  
  104.                                 case 'yes':
  105.                                 case 'Yes':
  106.                                 case 'y':
  107.                                 case 'Y':
  108.                                     rl.question('What percent tip would you like to leave? (type a whole number) \n', (tipAmount) => {
  109.                                         console.log(`Alright, The Waiter Is Getting A: ${tipAmount} % Tip\n`)
  110.                                         if (isInt(tipAmount) == true && tipAmount > 0) {
  111.                                             console.log('Alright, The Tip Would Be: $' + tip(totalBill, tipAmount) + '\n')
  112.                                             return process.exit(1)
  113.                                         } else {
  114.                                             console.log('Invalid Input Received. Exiting Check Splitter')
  115.                                             return process.exit(1)
  116.                                         }
  117.                                     })
  118.                                     break
  119.  
  120.                                 case 'no':
  121.                                 case 'No':
  122.                                 case 'n':
  123.                                 case 'N':
  124.                                     console.log("That Isn't Very Nice! You Should Reconsider. Your Total Is: $"+ totalBill)
  125.                                     return process.exit(1)
  126.  
  127.                                 case 'exit':
  128.                                 case 'Exit':
  129.                                     return process.exit(1)
  130.  
  131.                                 default:
  132.                                     console.log('Invalid Input Received. Exiting Check Splitter')
  133.                                     break
  134.                                     return process.exit(1)
  135.                             }
  136.                         })
  137.                     } else {
  138.                         console.log("Invalid Input Received. Exiting Check Splitter")
  139.                         return process.exit(1)
  140.                     }
  141.                 })
  142.                 break;
  143.  
  144.             case 'exit':
  145.             case 'Exit':
  146.                 return process.exit(1)
  147.  
  148.             default:
  149.                 console.log('Invalid Input Received. Exiting Check Splitter')
  150.                 return process.exit(1)
  151.  
  152.         }
  153.     })
  154.  
  155. }
  156.  
  157. function interestCalculator() {
  158.     rl.question('What is the Principal of the loan: ', function (PR) {
  159.         rl.question('What is the interest rate: ', function (IN) {
  160.             rl.question("How many Monthly payments are there", function (PE) {
  161.                 //   Monthly Payment/ ((1 + Interest rate per annum/100) ^ Term of loan) * Term of loan * 12
  162.                 var Interest = IN / 10
  163.                 var pay = (PR * Interest) / (1 - Math.pow(1 + IN, -PE));
  164.                 processLoan(pay);
  165.                 rl.close();
  166.             });
  167.         });
  168.     })
  169.  
  170. }
  171.  
  172. console.log('Type exit or press control C to quit')
  173. rl.question('Welcome To Our Finance App! \n Which Function Will You Be Using Today? \n1: Check Splitter\n2: Interest Calculator\n', (answer2) => {
  174.     switch (answer2) {
  175.         case '1':
  176.         case 'check splitter':
  177.             checkSplitter();
  178.         case '2':
  179.         case 'interest calculator':
  180.             interestCalculator();
  181.     }
  182. })
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement