Jump to content

First try at OOPHP, comments please! (New code)


Korken

Recommended Posts

Edit: Changed the code a bit.

 

Hello!

 

This is my 2nd post here (Sorry for the wrong forum with the first one! :| ) and my first try at OOPHP.

I watched the beginner videos, looked at the first three (free) movies of the shopping cart tutorial and decided to finnish it without any more help to see how it became.

So after a few hours I ended up with this:

Please comment if I should have done anything in a different way or if it looks okey. :)

 

Thanks for the tutorials KillerPHP!

 

//Emil Fresk

Sweden

 

 

Code:

<?php
/**********************************************************************
* - Made in the Eclipse IDE: www.eclipse.org
* - Tested on PHP 5.2.
* - Kickstart in OOPHP: www.killerphp.com
* 
* Usage of the class ShoppingCart
* 
* Attention!: product_id is a string and value is an integer.
* 
* -> AddItem("product_id"):
* This will add an item to the cart.
* If the item already exists, the quantity of that item will increased by 1.
* 
* -> DeleteItem("product_id"):
* This will remove an item from the cart.
* Remove as in it will be gone, if you want to change the quantity see 'SetQuantity'.
* 
* -> GetItems():
* This will return an array with all the items in the cart.
* 
* -> SetQuantity("product_id", value):
* This will change the quantity of the item to value.
* If value is 0 the item will be automatically removed.
* 
* -> GetQuantity("product_id"):
* This will return the quantity of the item.
* 
* 
* Design of the cart array:
*  _______________________
* | Product ID | Quantity |
* |  (string)  |  (int)   |
* |------------|----------|
* | exampleid1 |    5     |
* |- - - - - - | - - - - -|
* | exampleid2 |    3     |
* |- - - - - - | - - - - -|
* | exampleid5 |    13    |
*  -----------------------
**********************************************************************/

class ShoppingCart
{
   private $cart = array();

   public function AddItem($product_id)
   {
       if (array_key_exists($product_id, $this->cart))
           $this->cart[$product_id] += 1;
       else
           $this->cart[$product_id] = 1;
   }

   public function DeleteItem($product_id)
   {
       if (array_key_exists($product_id, $this->cart))
           unset($this->cart[$product_id]);
   }

   public function GetItems()
   {
       return array_keys($this->cart);
   }

   public function SetQuantity($product_id, $value)
   {
       if (array_key_exists($product_id, $this->cart))
       {
           if ((int)$value > 0)
               $this->cart[$product_id] = (int)$value;
           else
               unset($this->cart[$product_id]);
       }
   }

   public function GetQuantity($product_id)
   {
       if (array_key_exists($product_id, $this->cart))
           return $this->cart[$product_id];
   }
}

Edited by Korken
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

Loading...
×
×
  • Create New...