January 6, 2005

Assignment 4

I finally uploaded Assignment 4 this morning.  It is a simple PHP script that takes user information like name, age, and type of beverage and prints a user-specified number of coupons for the chosen product.  It does a little bit of error checking for proper alcohol purchasing age and proper string entry, but it is very simple and straightforward.

Inspect the program here=>  coupons.php

It uses a class definition for the coupon as identified in the code below.

< ?php
    class Coupons {
        //variables of the class
        var $name;
        var $age;
        var $count;
        var $type;
        var $cost;
        
        //class constructor
        function Coupons($name, $age, $count, $type) {
            $this->name = $name;
            $this->age = $age;
            $this->count = $count;
            $this->type = $type;
            $this->cost = 20;
            //this is how much they must spend to redeem the coupons
        }
        
        //the print coupons function
        function print_coupons() {
            //test for proper age
            if ($this->age < 21)
            {
                print ''VOID VOID VOID VOID VOID VOID VOID'';
                print ''|'';
                print ''|   You are too young to shop here!'';
                print ''|'';
                print ''VOID VOID VOID VOID VOID VOID VOID'';
            }
            else {
                print ''---------------------------------'';
                //test for kind of booze requested
                switch ($this->type) {
                    case ("b") :
                        print ''|   5% off your next purchase of beer of $''.$this->cost.'' or more.'';
                        print ''|   Save at least $''.($this->cost).05.''!'';
                        break;
                    case ("w") :
                        print ''|   7% off your next purchase of wine of $''.$this->cost.'' or more.'';
                        print ''|   Save at least $''.($this->cost).07.''!'';
                        break;
                    case ("x") :
                        print ''|   10% off your next purchase of liquor of $''.$this->cost.'' or more.'';
                        print ''|   Save at least $''.($this->cost)*.1.''!'';
                        break;
                    default :
                        print ''|   Coupons are only available for purchases of beer, wine, and liquor.'';
                        break;
                    
                    // print ''|   The type is ''.$type.'''';
                    print ''|   Congrats, ''.$this->name.'''';
                    print ''---------------------------------'';
                }
                return 1;
            }
            
            function get_count() {
                return $this->count; }
        }
?>

After completing this assignment, I decided to change my VMS system from straight database organization to incorporate classes as well.  This should make the program more robust and extensible in the future (I hope).