package bean;
創(chuàng)新互聯(lián)專注為客戶提供全方位的互聯(lián)網(wǎng)綜合服務(wù),包含不限于成都網(wǎng)站設(shè)計、成都網(wǎng)站建設(shè)、昔陽網(wǎng)絡(luò)推廣、小程序定制開發(fā)、昔陽網(wǎng)絡(luò)營銷、昔陽企業(yè)策劃、昔陽品牌公關(guān)、搜索引擎seo、人物專訪、企業(yè)宣傳片、企業(yè)代運營等,從售前售中售后,我們都將竭誠為您服務(wù),您的肯定,是我們最大的嘉獎;創(chuàng)新互聯(lián)為所有大學生創(chuàng)業(yè)者提供昔陽建站搭建服務(wù),24小時服務(wù)熱線:028-86922220,官方網(wǎng)址:chinadenli.net
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
* 購物車類:
* 為了方便將商品信息綁訂到session上面而設(shè)計的一個
* 工具,提供了商品的添加,刪除,列表,計價,清空,
* 修改功能。
*/
public class Cart {
//items屬性:用來保存商品
private ListCartItem items =
new ArrayListCartItem();
/**
* 將商品添加到購物車
*/
public boolean add(CartItem item){
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == item.getC().getId()){
//該商品已經(jīng)購買過
return false;
}
}
//沒有購買過,則添加該商品
items.add(item);
return true;
}
/**
* 從購物車當中刪除某件商品
*/
public void delete(int id){
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
items.remove(curr);
return;
}
}
}
/**
* 獲得購物車中所有商品信息
*/
public ListCartItem list(){
return items;
}
/**
* 商品總價
*/
public double cost(){
double total = 0;
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
total += curr.getC().getPrice() * curr.getQty();
}
return total;
}
/**
* 清空購物車中的所有商品
*/
public void clear(){
items.clear();
}
/**
* 修改購物車中某種商品的數(shù)量
*/
public void modify(int id,int qty){
for(int i=0;iitems.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
curr.setQty(qty);
return;
}
}
}
}
附件為使用session實現(xiàn)了購物車功能,包括添加商品到購物車、修改數(shù)量和刪除購物車商品信息功能!
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到購物車
public void addBook(String bookId,String bookQuantity){
if(hm.containsKey(bookId)){
int value=Integer.parseInt(hm.get(bookId));
value+=Integer.parseInt(bookQuantity);
hm.put(bookId, value+"");
}else{
hm.put(bookId, bookQuantity);
}
}
//修改數(shù)量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
//獲取購物車的所有信息 并計算總價
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價,防止無限累計
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價
public float getTotlePrice(){
return totlePrice;
}
//根據(jù)ID獲取數(shù)量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購物車
public void clear(){
hm.clear();
}
//刪除購物車中的一本書
public void deleteById(String id){
hm.remove(id);
}
}
import java.awt.*;
import java.awt.event.*;
class ShopFrame extends Frame implements ActionListener
{ Label label1,label2,label3,label4;
Button button1,button2,button3,button4,button5;
TextArea text;
Panel panel1,panel2;
static float sum=0.0f;
ShopFrame(String s)
{ super(s);
setLayout(new BorderLayout());
label1=new Label("面紙:3元",Label.LEFT);
label2=new Label("鋼筆:5元",Label.LEFT);
label3=new Label("書:10元",Label.LEFT);
label4=new Label("襪子:8元",Label.LEFT);
button1=new Button("加入購物車");
button2=new Button("加入購物車");
button3=new Button("加入購物車");
button4=new Button("加入購物車");
button5=new Button("查看購物車");
text=new TextArea("商品有:"+"\n",5,10);
text.setEditable(false);
addWindowListener(new WindowAdapter()
{ public void windowClosing(WindowEvent e)
{ System.exit(0);
}
}
);
button1.addActionListener(this);
button2.addActionListener(this);
button3.addActionListener(this);
button4.addActionListener(this);
button5.addActionListener(this);
panel1=new Panel();
panel2=new Panel();
panel1.add(label1);
panel1.add(button1);
panel1.add(label2);
panel1.add(button2);
panel1.add(label3);
panel1.add(button3);
panel1.add(label4);
panel1.add(button4);
panel2.setLayout(new BorderLayout());
panel2.add(button5,BorderLayout.NORTH);
panel2.add(text,BorderLayout.SOUTH);
this.add(panel1,BorderLayout.CENTER);
this.add(panel2,BorderLayout.SOUTH);
setBounds(100,100,350,250);
setVisible(true);
validate();
}
public void actionPerformed(ActionEvent e)
{ if(e.getSource()==button1)
{ text.append("一個面紙、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只鋼筆、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本書、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一雙襪子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"總價為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購物車");
}
}
我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個。
很簡單,在else if(action.equals("CLEAR")){//清空購物車 邏輯里邊,加上session.setAttribute("shopping_cart",cart);
此時的cart 里邊已經(jīng)沒有元素。 貌似這句代碼里邊被注解了。
當前文章:購物車刪除代碼java,清空購物車代碼
轉(zhuǎn)載注明:http://chinadenli.net/article40/hecheo.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站維護、App開發(fā)、網(wǎng)站改版、小程序開發(fā)、網(wǎng)站建設(shè)、用戶體驗
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)