Advertisement
Luninariel

Financial Calculator - Checksplitter WIP

Nov 26th, 2019
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2. Check splitter is the portion of the program that calculates the bill
  3. Splits it amongst the patrons, and calculates if a tip is needed.
  4.  */
  5.  
  6. //Read User Input
  7. const readline = require('readline');
  8. const rl = readline.createInterface({
  9.     input: process.stdin,
  10.     output: process.stdout
  11. });
  12.  
  13. //A Function used to check if a value is an int, makes use of lambda's
  14. function isInt(value) {
  15.     return !isNaN(value) && (function (x) { return (x | 0) === x })(parseFloat(value))
  16. }
  17.  
  18. //A simple function used to exit the software at its relevant points.
  19. function exit() {
  20.     return process.exit(1);
  21. }
  22.  
  23. //A Function that Calculates the Tip, by taking the Total Bill and dividing it by the percent to be tipped.
  24. function tip(bill, tipAmount) {
  25.     const Bill = parseFloat(bill);
  26.     const Tip = parseFloat(tipAmount);
  27.     console.log("\nAlright, You're Leaving A Tip Of: "+Tip+"%");
  28.     const tipNaked = ((Tip/100)*bill).toFixed(2);
  29.     console.log("\nThe tip will cost you an extra $" + tipNaked);
  30.     console.log("\nThe total after the tip is $"+((Bill * (Tip/100))+Bill).toFixed(2));
  31.     console.log("\nWe Hope You Enjoy Your Meal!\n");
  32.     return tipNaked;
  33.  
  34. }
  35.  
  36. //A Function that splits the total amount of the bill, by the number of diners.
  37. function splitBill(numDiners, bill) {
  38.     return (bill /numDiners).toFixed(2);
  39. }
  40.  
  41. //A function that will return an error if the input is invalid.
  42. function invalid() {
  43.     console.log("\nIt Seems You Entered An Invalid Input, Please Try Again\n");
  44. }
  45.  
  46. // A Function that asks if the check is to be split and uses that to move on to the next relevant question
  47. function CheckSplit() {
  48.     rl.question('Will You Be Splitting The Check? (yes/no) \n', (answer) => {
  49.         switch (answer.toLowerCase()) {
  50.             case 'yes':
  51.             case 'y':
  52.                 question1();
  53.                 break;
  54.             case 'no':
  55.             case 'n':
  56.                 question2NoSplit();
  57.                 break;
  58.             default:
  59.                 invalid();
  60.                 CheckSplit();
  61.                break
  62.         }
  63.     })
  64. }
  65. //A Function Used To Ask The User How Many Patrons There Were, This Sends The Response To Question 2 For It To Process
  66. function question1() {
  67.         rl.question('How Many Patrons Were There? (Please provide a whole number) ', (numDiners) => {
  68.             if (isNaN(numDiners) === false && numDiners > 0) {
  69.                 console.log(`Got it, There Were ${numDiners} Patron(s)\n`);
  70.                 question2(numDiners)
  71.             } else {
  72.                 invalid();
  73.                 question1();
  74.             }
  75.         })
  76. }
  77.  
  78. //A Function that asks for the total amount due, and sends that information combined with the number of patrons to Question 3
  79. function question2(numDiners) {
  80.     rl.question('What Is The Total Amount Due? (Please Enter A Number Using Two Decimal Places)\n', (totalBill) => {
  81.         if (isNaN(totalBill) === false && totalBill > 0) {
  82.             console.log(`Alright, The Total Amount Due Is: $${totalBill}\n`);
  83.             question3(numDiners,totalBill)}
  84.         else {
  85.             invalid();
  86.             question2(numDiners);
  87.         }
  88.     })
  89. }
  90.  
  91. //A Function to ask the user if a Tip will be left. Passes the value to the next relevant function depending on their response.
  92. function question3(numDiners,totalBill) {
  93.     if (isNaN(numDiners) === false) {
  94.         let split =parseFloat(splitBill(numDiners, parseFloat(totalBill)));
  95.         console.log('So Far An Even Split Would Be $' + split + ' Between ' + numDiners + ' Patron(s).\n');
  96.         rl.question('Will You Be Leaving Your Server A Tip? (yes/no)) \n', (answer) => {
  97.             switch (answer.toLowerCase()) {
  98.                 case 'yes':
  99.                 case 'y':
  100.                     question3Yes(numDiners,totalBill,split);
  101.                     break;
  102.  
  103.                 case 'no':
  104.                 case 'n':
  105.                     question3No(numDiners, totalBill);
  106.                     break;
  107.  
  108.                 case 'exit':
  109.                 case 'e':
  110.                     exit();
  111.                     break;
  112.                 default:
  113.                     invalid();
  114.                     question3(numDiners,totalBill);
  115.                     break;
  116.             }
  117.         })
  118.     } else {
  119.         console.log("Something isn't right with the numbers you entered please try again");
  120.         question1();
  121.     }
  122. }
  123. //A Function Following Question3 That asks the amount of tip and passes that information to be calculated
  124. function question3Yes(numdiners,totalbill,split) {
  125.     rl.question('What Percent Tip Would You Like To Leave?(Example: 5.5)', (tipAmount) => {
  126.         let tipAMT = parseFloat(tipAmount);
  127.             if (isNaN(tipAMT) === false && tipAMT > 0) {
  128.                 let tipNaked = parseFloat(tip(totalbill,tipAMT));
  129.                 let tipsplit =tipNaked/numdiners;
  130.                 let splitTipsplit = (split+tipsplit);
  131.                 console.log("\nAlright If Everyone Is Sharing, The Total Cost Per Person Would Be: $"+
  132.                     splitTipsplit.toFixed(2));
  133.                 console.log("\nWe Hope You Enjoyed Your Visit! \n");
  134.                 exit();
  135.  
  136.             } else {
  137.                 invalid();
  138.                 question3Yes(numdiners,totalbill,split);
  139.             }
  140.     })
  141. }
  142.  
  143. //A function For Users That Choose To Leave No Tip
  144. function question3No(numDiners,totalBill) {
  145.     console.log("Very Well Then.. Your Total Bill Is: $" + splitBill(numDiners, parseFloat(totalBill)) + ' Per Person \n');
  146.     exit();
  147. }
  148. //A Function that fires if the bill isn't in fact being split
  149. function question2NoSplit(){
  150.     rl.question('What Is The Total Amount Due? (Please Enter A Number Using Two Decimal Places)\n ', (totalBill) => {
  151.         if (isNaN(totalBill) === false && totalBill > 0) {
  152.             console.log(`Alright, The Total Amount Due Is: $${totalBill}\n`);
  153.             question3NoSplit(totalBill)
  154.         }
  155.         else {
  156.             invalid();
  157.             question2NoSplit();
  158.         }
  159.     })
  160. }
  161. //A Function to calculate a non split bills tip
  162. function question3NoSplit(totalBill){
  163.     rl.question('Will You Be Leaving Your Server A Tip? (yes/no)) \n', (tip) => {
  164.         switch (tip.toLowerCase()) {
  165.             case 'yes':
  166.             case 'y':
  167.                 TipNoSplit(totalBill);
  168.                 break;
  169.  
  170.             case 'no':
  171.             case 'n':
  172.                 NoTipNoSplit(totalBill);
  173.                 break;
  174.  
  175.             case 'exit':
  176.             case 'e':
  177.                 exit();
  178.                 break;
  179.             default:
  180.                 invalid();
  181.                 question3NoSplit(totalBill);
  182.                 break
  183.         }
  184.     })
  185. }
  186.  
  187. //A Function to calculate a tip that is not being split.
  188. function TipNoSplit(totalBill) {
  189.     rl.question('What Percent Tip Would You Like To Leave? (Please Enter A Whole Number) ', (tipAmount) => {
  190.         if (isInt(tipAmount) === true && tipAmount > 0) {
  191.             console.log(`Received: ${tipAmount}`);
  192.             tip(totalBill, tipAmount);
  193.             exit();
  194.         } else {
  195.             invalid();
  196.             TipNoSplit(totalBill);
  197.         }
  198.     })
  199. }
  200. //A Function To Print A Response To A User Who Chooses Not To Split The Bill, Or Leave A Tip
  201. function NoTipNoSplit(totalBill) {
  202.     console.log("Very Well Then.. Your Total Bill Is: $" + totalBill);
  203.     exit();
  204. }
  205.  
  206. //Exporting the module
  207. module.exports = {
  208.    CheckSplit,
  209. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement