Announcement

Collapse
No announcement yet.

PHP help

Collapse
X
 
  • Filter
  • Time
  • Show
Clear All
new posts

  • PHP help

    Hiya, I'm doing an assignment for my uni course on PHP, and I've got a bit stuck.

    We've got to make a basic insurance quote site. I've managed to get all the forms and confirmation pages to output fine. I just need some help/guidence on how to get the following to work;

    Calculate the cost of insurance, where the basic cost of insurance is £300 per year. However increasing in the following cases;

    a. Engine size over 2 litres
    b. Age less than 17; decline.
    c. Age between 17 and 20 add £200 to quote
    d. License points greater than 9, decline the quote.
    e. Over 3 licensepoints but less than 9 add £500 to the quote.



    I can get them to work individually, using if statements, but am struggling to get them to work together.

    Any help appriciated!

  • #2
    PHP Code:
    <?php

    function delcine() {
        die(
    'You have been declined.' "\n");
    }

    function 
    check_age($quote$age) {
        if (
    $age 17) { 
            
    delcine(); 
        }    
        else if (
    $age >= 17 && $age <= 20) { 
            
    $quote $quote 200;
        }
        return 
    $quote;
        
    }

    function 
    check_license_points($quote$points) {
        if (
    $points 9) { 
            
    delcine(); 
        }    
        else if (
    $points && $points 9) { 
            
    $quote $quote 500;
        }
        return 
    $quote;
        
    }

    // A fifty year old with 5 license points, results = 800
    $quote 300;
    $quote check_age($quote50);
    $quote check_license_points($quote5);
    echo 
    $quote "\n";

    // A 50 year old with 2 license points, results = 300
    $quote 300;
    $quote check_age($quote50);
    $quote check_license_points($quote2);
    echo 
    $quote "\n";

    // A 50 year old with 50 license points, results = die
    $quote 300;
    $quote check_age($quote50);
    $quote check_license_points($quote50);
    echo 
    $quote "\n";

    ?>

    Comment


    • #3
      Here's the "Object Oriented" way:

      PHP Code:
      <?php

      class Quote {
          var 
      $quote 300;
          var 
      $age;
          var 
      $points;
          
          function 
      delcine() {
              die(
      'You have been declined.' "\n");
          }    
          
          function 
      set_age($new_age) {
              
      $this->age $new_age;    
          }
          
          function 
      set_points($new_points) {
              
      $this->points $new_points;    
          }
          
          function 
      get_quote() {
              
              
      //Age
              
      if ($this->age 17) { 
                  
      $this->delcine(); 
              }    
              else if (
      $this->age >= 17 && $this->age <= 20) { 
                  
      $this->quote $this->quote 200;
              }
              
              
      // Points
              
      if ($this->points 9) { 
                  
      $this->delcine(); 
              }    
              else if (
      $this->points && $this->points 9) { 
                  
      $this->quote $this->quote 500;
              }        
              
              return 
      $this->quote;  
          }    
          
      }

      // A fifty year old with 5 license points, results = 800
      $quote = new Quote;
      $quote->set_age(50);
      $quote->set_points(5);
      echo 
      $quote->get_quote() . "\n";


      // A 50 year old with 2 license points, results = 300
      $quote = new Quote;
      $quote->set_age(50);
      $quote->set_points(2);
      echo 
      $quote->get_quote() . "\n";

      // A 50 year old with 50 license points, results = die
      $quote = new Quote;
      $quote->set_age(50);
      $quote->set_points(50);
      echo 
      $quote->get_quote() . "\n";

      ?>

      Comment


      • #4
        PS: Check the >, <, <=, >= conditions for 9 points.

        I followed your wording to do the boolean but the code ends up having no calculable value for 9.

        Comment

        widgetinstance 262 (Related Topics) skipped due to lack of content & hide_module_if_empty option.
        Working...
        X