Advertisement
dimipan80

Calculate Interest

Apr 14th, 2015
279
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 3.08 KB | None | 0 0
  1. <!--Write a PHP script CalculateInterest.php which generates an HTML page that contains:
  2. An input text field to hold the amount of money; Radio buttons to choose the currency;
  3. An input text field to enter the compound annual interest amount;
  4. A dropdown menu to choose the period of time; A submit button.
  5. When the information is submitted, the script should print out the amount of money you will have after the selected period, rounded to 2 decimal places. Semantic HTML is required. Styling is not required.-->
  6.  
  7. <!DOCTYPE html>
  8. <html>
  9. <head lang="en">
  10.     <meta content="text/html" charset="UTF-8">
  11.     <title>Calculate Interest</title>
  12. </head>
  13. <body>
  14. <form action="#" method="post">
  15.     <p>
  16.         <label for="startAmount">Enter Amount</label>
  17.         <input type="text" name="startAmount" id="startAmount" autofocus required/>
  18.     </p>
  19.  
  20.     <p>
  21.         <input type="radio" name="currency" id="currency1" value="0" checked/><label
  22.             for="currency1">USD</label>
  23.         <input type="radio" name="currency" id="currency2" value="1"/><label
  24.             for="currency2">EUR</label>
  25.         <input type="radio" name="currency" id="currency3" value="2"/><label
  26.             for="currency3">BGN</label>
  27.     </p>
  28.  
  29.     <p>
  30.         <label for="compound-interest">Compound Interest Amount</label>
  31.         <input type="text" name="compound-interest" id="compound-interest"/>
  32.     </p>
  33.     <select name="periodOfTime" id="dropdown-list">
  34.         <option value="0">3 Months</option>
  35.         <option value="1">6 Months</option>
  36.         <option value="2">1 Year</option>
  37.         <option value="3">2 Years</option>
  38.         <option value="4">3 Years</option>
  39.         <option value="5">5 Years</option>
  40.     </select>
  41.     <input type="submit" value="Calculate"/>
  42.     <?php
  43.     mb_internal_encoding('UTF-8');
  44.  
  45.     if ($_POST && array_key_exists('startAmount', $_POST) && array_key_exists('currency', $_POST) &&
  46.         array_key_exists('compound-interest', $_POST) && array_key_exists('periodOfTime', $_POST)
  47.     ) {
  48.         if (!preg_match("/^\d+\.?\d{0,2}$/", trim($_POST['startAmount']))) {
  49.             exit('Wrong input!!! - The Amount must been integer or float-point number with 2 digits after decimal
  50.             point!');
  51.         }
  52.  
  53.         if (!preg_match("/^\d+\.?\d*$/", trim($_POST['compound-interest']))) {
  54.             exit('Wrong input!!! - The Compound Interest Amount must been integer or float-point number!');
  55.         }
  56.  
  57.         $amount = (float)$_POST['startAmount'];
  58.         $currencyTypes = array('&#36;', '&euro;', 'ะปะฒ.');
  59.         $currency = (array_key_exists($_POST['currency'], $currencyTypes)) ? $currencyTypes[$_POST['currency']] :
  60.             null;
  61.         $compoundInterest = (float)$_POST['compound-interest'];
  62.         $months = array(3, 6, 12, 24, 36, 60);
  63.         $timePeriod = (array_key_exists($_POST['periodOfTime'], $months)) ? $months[$_POST['periodOfTime']] :
  64.             null;
  65.         if (!$currency || !$timePeriod) {
  66.             exit('Unexpected error!!! - Reload the Web Site again and carefully enter input fields!');
  67.         }
  68.  
  69.         $compoundInterest = (($compoundInterest / 12) + 100) / 100;
  70.         for ($i = 0; $i < $timePeriod; $i++) {
  71.             $amount *= $compoundInterest;
  72.         }
  73.  
  74.         $amount = round($amount, 2);
  75.         echo ($_POST['currency'] == 2) ? $amount . ' ' . $currency : $currency . ' ' . $amount;
  76.     }
  77.     ?>
  78. </form>
  79. </body>
  80. </html>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement