Recently, shopping cart was needed for a project, which may be frequently used. Therefore, it is encapsulated into a class for future calls. You can simply modify this class and use it in your own program. For more information, see .? **************************************** Not long ago, ********************************** to achieve a project, you need to use the shopping cart, because it may be frequently used, it is encapsulated into a class for future calls. You can simply modify this class to use it in your own program. For more information, see.
/*************************************** **************************************/
/**/
/* File type: contains files. the recommended suffix is. inc */
/**/
/* File name: cart. inc */
/**/
/* Description: define a car purchase class */
/**/
/* Func list: class cart */
/**/
/* Author: bigeagle */
/**/
/* Date: 2000/12/24 */
/**/
/* History: 2000/12/24 finished */
/**/
/*************************************** **************************************/
// Define the constant of this file
Define ("_ CART_INC _", "exists ");
/* Shopping cart */
Class TCart
{
Var $ SortCount; // number of product types
Var $ TotalCost; // total product value
Var $ Id; // The ID of each type of item (array)
Var $ Name; // The Name of each type of item (array)
Var $ Price; // Price of each type of product (array)
Var $ Discount; // discounts for items (array)
Var $ GoodPrice; // The preferential price of the product (array)
Var $ Count; // The number of items in each category (array)
Var $ MaxCount; // item limit (array)
// ******** Private, update the corresponding data in the class according to the session value
Function Update ()
{
Global $ sId, $ sName, $ sPrice, $ sCount, $ sDiscount, $ sMaxCount, $ sGoodPrice;
// Calculate the total number of items
$ This-> SortCount = count ($ sId );
}
// ******** Private, calculate the value of each type of commodity and the total price of all commodities based on new data
Function Calculate ()
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
/* Calculate the value of each item. if the discount is 0, the discount price */
$ GiftPrice = ($ this-> Discount [$ I] = 0? $ This-> GoodPrice:
Ceil ($ this-> Price [$ I] * $ this-> Discount [$ I])/100 );
$ This-> TotalCost + = $ GiftPrice * $ this-> Count [$ I];
}
}
// The interface functions are as follows:
// *** Add one item
// Determine whether blue already exists. If yes, add count. otherwise, add a new product.
// Change the session value first, and then call update () and calculate () to update the member variable.
Function Add ($ a_ID, $ a_Name, $ a_Price, $ a_Discount,
$ A_GoodPrice, $ a_MaxCount, $ a_Count)
{
Global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount,
$ SGoodPrice, $ sMaxCount;
$ K = count ($ sId );
For ($ I = 0; $ I <$ k; $ I ++)
{// First check whether such a product has been added
If ($ sId [$ I] ==$ a_ID)
{
$ SCount [$ I] + = $ a_Count;
Break;
}
}
If ($ I >=$ k)
{// If not, add a new product type.
$ SId [] = $ a_ID;
$ SName [] = $ a_Name;
$ SPrice [] = $ a_Price;
$ SCount [] = $ a_Count;
$ SGoodPrice [] = $ a_GoodPrice;
$ SDiscount [] = $ a_Discount;
$ SMaxCount [] = $ a_MaxCount;
}
$ This-> Update (); // updates the member data of the class.
$ This-> Calculate ();
}
// Remove a commodity
Function Remove ($ a_ID)
{
Global $ sId, $ sName, $ sCount, $ sPrice, $ sDiscount,
$ SGoodPrice, $ sMaxCount;
$ K = count ($ sId );
For ($ I = 0; $ I <$ k; $ I ++)
{
If ($ sId [$ I] ==$ a_ID)
{
$ SCount [$ I] = 0;
Break;
}
}
$ This-> Update ();
$ This-> Calculate ();
}
// Change the number of items
Function ModifyCount ($ a_ I, $ a_Count)
{
Global $ sCount;
// Whether a product is already in blue. the parameter specifies the ID of the product.
Function Exists ($ a_ID)
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
If ($ this-> Id [$ I] = $ a_ID) return TRUE;
}
Return FALSE;
}
// The position of a product in blue
Function IndexOf ($ a_ID)
{
For ($ I = 0; $ I <$ this-> SortCount; $ I ++)
{
If ($ this-> Id [$ I] ==$ id) return $ I;
}
Return 0;
}
// Obtain the information of a commodity, the main function
// Returns an associated array,
Function Item ($ I)
{
$ Result [id] = $ this-> Id [$ I];
$ Result [name] = $ this-> Name [$ I];
$ Result [price] = $ this-> Price [$ I];
$ Result [count] = $ this-> Count [$ I];
$ Result [discount] = $ this-> Discount [$ I];
$ Result [goodprice] = $ this-> GoodPrice [$ I];
$ Result [maxcount] = $ this-> MaxCount [I];
Return $ Result;
}
// Obtain the total number of product types
Function CartCount ()
{
Return $ this-> SortCount;
}
// Obtain the total commodity value
Function GetTotalCost ()
{
Return $ this-> TotalCost;
}
}
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.
A Free Trial That Lets You Build Big!
Start building with 50+ products and up to 12 months usage for Elastic Compute Service