當前位置:首頁 » 編程語言 » java購物車實現

java購物車實現

發布時間: 2023-09-06 16:48:20

java web 做購物車的大概思路,和實現步奏是什麼

購物車管理模塊主要功能有如下幾個部分:(1)創建購物車 當客戶登錄後,系統會給客戶創建一個購物車放入伺服器的Session會話中。使客戶在整個會話中都擁有一個相同的購物車。這里主要運用了Http協議中的會話機制,將購物車保存在客戶的會話中,這樣在整個客戶游覽不同頁面商品的過程中,都會使用同一個購物車對象。 具體執行步驟:(1)從客戶的請求對象中獲取Session會話對象(2)從會話對象中獲取購物車對象(3)判斷是購物車對象是不是空的,如果是空是就創建一個 /* * 在監聽到session被創建之後,就立即向session中添加一個購物車Car; */ public void sessionCreated(HttpSessionEvent arg0) { HttpSession session = arg0.getSession(); Cart cart=new Cart(); session.setAttribute("cart", cart); } /* * 從session中獲得購物車 */ Cart cart = (Cart) session.getAttribute("cart"); if (cart == null) { cart = new Cart(); }(2)向購物車中添加一個商品項 客戶在查看網頁上的一個商品時,當向伺服器發送一個「添加到購物車」的請求時,會執行這個功能。功能執行過程:(1)從客戶請求對象中獲取商品的ID(2)調用業務層的方法根據商品ID去數據查詢商品的信息,返回商品對象(3)從商品對象中獲取商品名,商品價格,來構建一個商品項對象(4)從Session會話中獲取購物車對象(5)調用業務層的方法來根據購物車對象和商品項對象來執行添加操作(6)將些商品項對象放入到購物車中 部分實現代碼: /* * 從資料庫中把商品取到; */ ProctService proctService = (ProctService) ServiceFactory.getInstance().getService(Globals.PRODUCT_SERVICE); Integer id = Integer.parseInt(request.getParameter("proctid")); Proct proct = proctService.getProctById(id); /* * 在向購物車中添加商品的時候會判斷商品是否已經存在, * 已存在的就不讓在加入了; */ if (cart.isExist(id)) { message = "該商品已經存在!請<a onclick='javascript:history.go(-1)'>返回</a>!"; request.setAttribute("message", message); return mapping.findForward("error"); } else { /* * 向購物車添加一個商品; */ cart.addCart(proct); session.setAttribute("cart", cart); return mapping.findForward("addcartsuccess"); }

⑵ java 如何編寫購物車

用Vector 或者是HashMap去裝
<下面有部分代碼你去看吧>

package com.aptech.restrant.DAO;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import java.sql.Connection;

import com.aptech.restrant.bean.CartItemBean;
import com.aptech.restrant.bean.FoodBean;

public class CartModel {
private Connection conn;
public CartModel(Connection conn) {
this.conn=conn;
}

/**
* 得到訂餐列表
*
* @return
*/
public List changeToList(Map carts) {

// 將Set中元素轉換成數組,以便使用循環進行遍歷
Object[] foodItems = carts.keySet().toArray();

// 定義double變數total,用於存放購物車內餐品總價格
double total = 0;
List list = new ArrayList();
// 循環遍歷購物車內餐品,並顯示各個餐品的餐品名稱,價格,數量
for (int i = 0; i < foodItems.length; i++) {

// 從Map對象cart中取出第i個餐品,放入cartItem中
CartItemBean cartItem = (CartItemBean) carts
.get((String) foodItems[i]);
// 從cartItem中取出FoodBean對象
FoodBean food1 = cartItem.getFoodBean();
// 定義int類型變數quantity,用於表示購物車中單個餐品的數量
int quantity = cartItem.getQuantity();
// 定義double變數price,表示餐品單價
double price = food1.getFoodPrice();
// 定義double變數,subtotal表示單個餐品總價
double subtotal = quantity * price;
// // 計算購物車內餐品總價格
total += subtotal;

cartItem.setSubtotal(subtotal);
cartItem.setTotal(total);
list.add(cartItem);
}
return list;

}

/**
* 增加訂餐
*/
public Map add(Map cart, String foodID) {
// 購物車為空
if (cart == null) {
cart = new HashMap();
}

FoodModel fd = new FoodModel(conn);
FoodBean food = fd.findFoodById(foodID);

// 判斷購物車是否放東西(第一次點餐)
if (cart.isEmpty()) {
CartItemBean cartBean = new CartItemBean(food, 1);
cart.put(foodID, cartBean);

} else {
// 判斷當前菜是否在購物車中,false表示當前菜沒有被點過。。
boolean flag = false;
// 得到鍵的集合
Set set = cart.keySet();
// 遍歷集合
Object[] obj = set.toArray();
for (int i = 0; i < obj.length; i++) {
Object object = obj[i];
// 如果購物車已經存在當前菜,數量+1
if (object.equals(foodID)) {
int quantity = ((CartItemBean) cart.get(object))
.getQuantity();
quantity += 1;
System.out.println(quantity);
((CartItemBean) cart.get(object)).setQuantity(quantity);
flag = true;
break;
}

}
if (flag == false) {
// 把當前菜放到購物車裡面
CartItemBean cartBean = new CartItemBean(food, 1);
cart.put(foodID, cartBean);

}
}

return cart;
}

/**
* 取消訂餐
*/
public Map remove(Map cart, String foodID) {
cart.remove(foodID);
return cart;
}

/**
* 更新購物車信息
*
* @param cart
* @param foodID
* @return
*/
public Map<String, CartItemBean> update(Map cart, String foodID,
boolean isAddorRemove) {
Map map;
if (isAddorRemove) {
map = add(cart, foodID);
} else {
map = remove(cart, foodID);
}
return map;
}
}

⑶ JAVA 購物車示例代碼

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可能顯示不出來你的效果。不滿意得話我在給你編一個。

⑷ 購物車的Java代碼

import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {

HashMap<String, String> hm=new HashMap<String, 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);
}
}

//修改數量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}

//獲取購物車的所有信息 並計算總價
public ArrayList<BookBean> getShoppingCart(){
ArrayList<BookBean> al=new ArrayList<BookBean>();
Iterator<String> 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;j<al.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}

return al;
}

//獲取總價
public float getTotlePrice(){
return totlePrice;
}

//根據ID獲取數量
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);
}
}

熱點內容
滑板鞋腳本視頻 發布:2025-02-02 09:48:54 瀏覽:433
群暉怎麼玩安卓模擬器 發布:2025-02-02 09:45:23 瀏覽:557
三星安卓12彩蛋怎麼玩 發布:2025-02-02 09:44:39 瀏覽:743
電腦顯示連接伺服器錯誤 發布:2025-02-02 09:24:10 瀏覽:537
瑞芯微開發板編譯 發布:2025-02-02 09:22:54 瀏覽:146
linux虛擬機用gcc編譯時顯示錯誤 發布:2025-02-02 09:14:01 瀏覽:239
java駝峰 發布:2025-02-02 09:13:26 瀏覽:652
魔獸腳本怎麼用 發布:2025-02-02 09:10:28 瀏覽:538
linuxadobe 發布:2025-02-02 09:09:43 瀏覽:212
sql2000資料庫連接 發布:2025-02-02 09:09:43 瀏覽:726