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