import java.util.ArrayList;

專業(yè)成都網(wǎng)站建設(shè)公司,做排名好的好網(wǎng)站,排在同行前面,為您帶來客戶和效益!創(chuàng)新互聯(lián)公司為您提供成都網(wǎng)站建設(shè),五站合一網(wǎng)站設(shè)計(jì)制作,服務(wù)好的網(wǎng)站設(shè)計(jì)公司,網(wǎng)站建設(shè)、成都網(wǎng)站建設(shè)負(fù)責(zé)任的成都網(wǎng)站制作公司!
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);
}
//獲取購物車的所有信息 并計(jì)算總價(jià)
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; //清空總價(jià),防止無限累計(jì)
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價(jià)
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);
}
}
如果不使用 Ajax 技術(shù),可以在后臺(tái)做三個(gè) action 分別是 add(),sub() 和 delete() 分別表示加、減、刪。
然后前臺(tái)針對這三個(gè)操作做三個(gè)鏈接:
youproject/add?id=1
youproject/sub?id=1
youproject/delete?id=1
如果是要做 REST 風(fēng)格的鏈接,可以這樣:
youproject/add/1
youproject/sub/1
youproject/delete/1
后臺(tái) action 接收到請求后,處理購物車數(shù)據(jù),處理完成后返回前臺(tái),刷新頁面。
附件為使用session實(shí)現(xiàn)了購物車功能,包括添加商品到購物車、修改數(shù)量和刪除購物車商品信息功能!
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("一個(gè)面紙、");
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"+"總價(jià)為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購物車");
}
}
我沒用Swing可能顯示不出來你的效果。不滿意得話我在給你編一個(gè)。
很簡單,在else if(action.equals("CLEAR")){//清空購物車 邏輯里邊,加上session.setAttribute("shopping_cart",cart);
此時(shí)的cart 里邊已經(jīng)沒有元素。 貌似這句代碼里邊被注解了。
package bean;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
* 購物車類:
* 為了方便將商品信息綁訂到session上面而設(shè)計(jì)的一個(gè)
* 工具,提供了商品的添加,刪除,列表,計(jì)價(jià),清空,
* 修改功能。
*/
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;
}
/**
* 從購物車當(dāng)中刪除某件商品
*/
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;
}
/**
* 商品總價(jià)
*/
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;
}
}
}
}
新聞名稱:刪除購物車java代碼,javaee購物車的增刪改查
本文鏈接:http://chinadenli.net/article21/dsidijd.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供品牌網(wǎng)站設(shè)計(jì)、網(wǎng)站設(shè)計(jì)公司、移動(dòng)網(wǎng)站建設(shè)、軟件開發(fā)、搜索引擎優(yōu)化、商城網(wǎng)站
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來源: 創(chuàng)新互聯(lián)