JSP---online mall shopping cart

Source: Internet
Author: User

Shopping items Cartitem.java

1  PackageZyz.shop.cart;2 3 Importzyz.shop.product.Product;4 5  Public classCartitem {6     PrivateProduct product;//Commodity (not ProductID, but object for better encapsulation)7     Private intNumber of count;//8 9      PublicProduct getproduct () {Ten         returnproduct; One     } A  -      Public voidsetproduct (product product) { -          This. Product =product; the     } -  -      Public intGetCount () { -         returncount; +     } -  +      Public voidSetCount (intcount) { A          This. Count =count; at     } -}

Shopping Cart Cart.java

1  PackageZyz.shop.cart;2 3 Importjava.util.ArrayList;4 ImportJava.util.Iterator;5 Importjava.util.List;6 7  Public classCart {8list<cartitem> items =NewArraylist<cartitem>();//all Shopping items9 Ten      PublicList<cartitem>GetItems () { One         returnitems; A     } -  -      Public voidSetitems (list<cartitem>items) { the          This. Items =items; -     } -Add a shopping item CI to the shopping cart -      Public voidAdd (Cartitem ci) { +Iterator<cartitem> it=items.iterator (); -          while(It.hasnext ()) {//If the item is in the cart, the number of items added is 1 +Cartitem item=It.next (); A             if(Item.getproduct (). Getpid () = =ci.getproduct (). Getpid ()) { atItem.setcount (Item.getcount () +1); -                 return; -             } -         } - Items.Add (CI);//If you do not have this item, add the shopping cart -     } inCalculate the total price of a shopping cart -      Public DoubleGettotalprice () { to         Doubles=0.0; +Iterator<cartitem> it=items.iterator (); -          while(It.hasnext ()) { theCartitem item=It.next (); *S+=item.getproduct (). GetPrice () *Item.getcount ();//Price * Quantity accumulation $         }Panax Notoginseng         returns; -     } theDelete a specified shopping item +      Public voidDeletecartitembyid (intproductId) { AIterator<cartitem> it=items.iterator (); the          while(It.hasnext ()) { +Cartitem item=It.next (); -             if(Item.getproduct (). Getpid () = =productId) {//If you have this item, remove it from your shopping cart $ It.remove (); $             } -         } -     } the}

JSP---online mall shopping cart

Contact Us

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

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.