⑴ 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 關於購物車
將session中的商品定義為對象
放入ArrayList中
修改了購物車內容後
把ArrayList.size()賦值到現在是0的位置回
如:
購物車添加物品答
List proctList = new ArrayList();
Proct proct1 = new Proct();
proctList.add(proct1);
...
Proct proctN = new Proct();
proctList.add(proctN);
session.setAttribute("proct", proctList);
頁面可以寫
List proctList = (List)session.getAttribute("proct");
把proctList.size()放到個數的位置
⑶ 用JAVA編寫購物車程序.
我這有你要不?剛寫的!
不過不是保存在內存上了,而是保存在資料庫中,用完刪不得了!
⑷ 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做購物車的思路
先判斷Session中是否有這個購物車List<ShopingItem>沒有就創建一個。商品購物項ShopingItem用一個JavaBean封裝起來版添加到購物物車權中List<ShopingItem>在遍歷裡面的集合,是否存在。存在就把封裝在Javabean 的數量修改以下就可以了。在保存在Session中去。
⑹ java中購物車的功能怎麼實現
session+cookie。先從cookie里讀取上次的購物清單到session,本次添加的購物清單也將加入session。
⑺ 當當網購物車功能如何用java實現
放進購物車的東西,添加到List,然後用Sesion......發送、接收
⑻ java不使用資料庫實現購物車功能
全放session中,使用map。。。。。。。。。。。
⑼ JAVA語言編寫的網上訂餐系統購物車功能如何實現
用Vector 或者是HashMap去裝
<下面有部分代碼你去看吧>
packagecom.aptech.restrant.DAO;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importjava.sql.Connection;
importcom.aptech.restrant.bean.CartItemBean;
importcom.aptech.restrant.bean.FoodBean;
publicclassCartModel{
privateConnectionconn;
publicCartModel(Connectionconn){
this.conn=conn;
}
/**
*得到訂餐列表
*
*@return
*/
publicListchangeToList(Mapcarts){
//將Set中元素轉換成數組,以便使用循環進行遍歷
Object[]foodItems=carts.keySet().toArray();
//定義double變數total,用於存放購物車內餐品總價格
doubletotal=0;
Listlist=newArrayList();
//循環遍歷購物車內餐品,並顯示各個餐品的餐品名稱,價格,數量
for(inti=0;i<foodItems.length;i++){
//從Map對象cart中取出第i個餐品,放入cartItem中
CartItemBeancartItem=(CartItemBean)carts
.get((String)foodItems[i]);
//從cartItem中取出FoodBean對象
FoodBeanfood1=cartItem.getFoodBean();
//定義int類型變數quantity,用於表示購物車中單個餐品的數量
intquantity=cartItem.getQuantity();
//定義double變數price,表示餐品單價
doubleprice=food1.getFoodPrice();
//定義double變數,subtotal表示單個餐品總價
doublesubtotal=quantity*price;
////計算購物車內餐品總價格
total+=subtotal;
cartItem.setSubtotal(subtotal);
cartItem.setTotal(total);
list.add(cartItem);
}
returnlist;
}
/**
*增加訂餐
*/
publicMapadd(Mapcart,StringfoodID){
//購物車為空
if(cart==null){
cart=newHashMap();
}
FoodModelfd=newFoodModel(conn);
FoodBeanfood=fd.findFoodById(foodID);
//判斷購物車是否放東西(第一次點餐)
if(cart.isEmpty()){
CartItemBeancartBean=newCartItemBean(food,1);
cart.put(foodID,cartBean);
}else{
//判斷當前菜是否在購物車中,false表示當前菜沒有被點過。。
booleanflag=false;
//得到鍵的集合
Setset=cart.keySet();
//遍歷集合
Object[]obj=set.toArray();
for(inti=0;i<obj.length;i++){
Objectobject=obj[i];
//如果購物車已經存在當前菜,數量+1
if(object.equals(foodID)){
intquantity=((CartItemBean)cart.get(object))
.getQuantity();
quantity+=1;
System.out.println(quantity);
((CartItemBean)cart.get(object)).setQuantity(quantity);
flag=true;
break;
}
}
if(flag==false){
//把當前菜放到購物車裡面
CartItemBeancartBean=newCartItemBean(food,1);
cart.put(foodID,cartBean);
}
}
returncart;
}
/**
*取消訂餐
*/
publicMapremove(Mapcart,StringfoodID){
cart.remove(foodID);
returncart;
}
/**
*更新購物車信息
*
*@paramcart
*@paramfoodID
*@return
*/
publicMap<String,CartItemBean>update(Mapcart,StringfoodID,
booleanisAddorRemove){
Mapmap;
if(isAddorRemove){
map=add(cart,foodID);
}else{
map=remove(cart,foodID);
}
returnmap;
}
}
⑽ java購物車功能怎麼實現
設置基本的實體類就不用說了吧。再設置一個購物車的實體類,介面和實現類。利用Session機制來存儲所選的物品,然後同意購物的時候將session中所存儲的物品List存入表中。