A. javaweb開發 在購物車實現的問題
第3中比較好。第一種,javaweb本身在cookie中存放復雜對象就很麻煩,讀寫量大。第二種,客戶下內線sesson一般就會容被移除,這樣用戶下次登陸時就沒有之前購物車信息了。第3中,可以避免上面的不足
B. 求教java實現購物車的思路
寫個用於封裝購物車的類,將商品信息都存進去,將其保存在session中就完了
C. 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;
}
}
D. JAVA map編寫購物車的問題
publicclassProct{
privateIntegerproctid;
privateStringproctname;
privateStringcategory;
privatefloatprice;
privateStringpicture;
publicProct(Integerproctid,Stringproctname,Stringcategory,floatprice,Stringpicture){
.proctid=proctid;
this.proctname=proctname;
this.category=category;
this.price=price;
this.picture=picture;
}
publicIntegergetProctid(){
returnproctid;
}
publicvoidsetProctid(Integerproctid){
this.proctid=proctid;
}
publicStringgetProctname(){
returnproctname;
}
publicvoidsetProctname(Stringproctname){
this.proctname=proctname;
}
publicStringgetCategory(){
returncategory;
}
publicvoidsetCategory(Stringcategory){
this.category=category;
}
publicfloatgetPrice(){
returnprice;
}
publicvoidsetPrice(floatprice){
this.price=price;
}
publicStringgetPicture(){
returnpicture;
}
publicvoidsetPicture(Stringpicture){
this.picture=picture;
}
}
publicclassProctItem{
privateProctproct;
privateIntegercount;
publicProctItem(Proctproct,Integercount){
this.proct=proct;
this.count=count;
}
publicProctgetProct(){
returnproct;
}
publicvoidsetProct(Proctproct){
this.proct=proct;
}
publicIntegergetCount(){
returncount;
}
publicvoidsetCount(Integercount){
this.count=count;
}
publicfloatgetTotalPrice(){
returnthis.proct.getPrice()*count;
}
}
publicclassShoppingCart{
privateHashMap<Integer,ProctItem>items=newHashMap<Integer,ProctItem>();
publicstaticvoidmain(String[]args){
ShoppingCartcart=newShoppingCart();
System.out.println("初始化產品");
Proctbanana=newProct(10000,"香蕉","水果",2.50F,"banana.jpg");
Proctapple=newProct(10001,"蘋果","水果",3.99F,"apple.jpg");
Proctorange=newProct(10002,"桔子","水果",1.80F,"orange.jpg");
Proctpen=newProct(10003,"鋼筆","文具",12.00F,"pen.jpg");
Proctpencil=newProct(10004,"鉛筆","文具",1.00F,"pencil.jpg");
System.out.println("###############################################");
System.out.println("添加蘋果");
cart.addToCart(apple);
System.out.println(cart.showAll());
System.out.println("當前購物車總價:"+cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("添加香蕉");
cart.addToCart(banana);
System.out.println(cart.showAll());
System.out.println("當前購物車總價:"+cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("修改香蕉數量");
cart.modifyCart(banana,5);
System.out.println(cart.showAll());
System.out.println("當前購物車總價:"+cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("刪除蘋果");
cart.delFromCart(apple);
System.out.println(cart.showAll());
System.out.println("當前購物車總價:"+cart.getTotalPrice());
System.out.println("***********************************************");
System.out.println("添加鉛筆");
cart.addToCart(pencil);
System.out.println(cart.showAll());
System.out.println("當前購物車總價:"+cart.getTotalPrice());
System.out.println("***********************************************");
}
publicvoidaddToCart(Proctproct){
this.items.put(proct.getProctid(),newProctItem(proct,1));
}
publicvoiddelFromCart(Proctproct){
this.items.remove(proct.getProctid());
}
publicStringshowAll(){
Stringdetail="";
for(Integerkey:this.items.keySet()){
detail+=this.items.get(key).getProct().getProctname()+":"+this.items.get(key).getCount()+"件,單價"+this.items.get(key).getProct().getPrice()+",小計"+this.items.get(key).getTotalPrice()+" ";
}
returndetail;
}
publicbooleanmodifyCart(Proctproct,Integercount){
if(this.items.containsKey(proct.getProctid())){
this.items.get(proct.getProctid()).setCount(count);
returntrue;
}
returnfalse;
}
publicfloatgetTotalPrice(){
floattotal=0;
for(Integerkey:this.items.keySet()){
total+=this.items.get(key).getTotalPrice();
}
returntotal;
}
}
E. java web 做購物車的大概思路,和實現步奏是什麼
購物車是網上購物平台中的一個重要模塊,模擬超市中的購物車功能,即用戶在結賬前挑選商品的過程。
看完圖就會明白,採納哦。
F. 用JAVA做購物車的思路
先判斷Session中是否有這個購物車List<ShopingItem>沒有就創建一個。商品購物項ShopingItem用一個JavaBean封裝起來版添加到購物物車權中List<ShopingItem>在遍歷裡面的集合,是否存在。存在就把封裝在Javabean 的數量修改以下就可以了。在保存在Session中去。
G. java中購物車的功能怎麼實現
session+cookie。先從cookie里讀取上次的購物清單到session,本次添加的購物清單也將加入session。
H. Java 如何實現類似購物車功能
給你介紹三種可以實現購物車功能的方法:
1.用cookie實現購物車;
2.用session實現購物車;
3.用cookie和資料庫(購物車信息持久化)實現購物車;
=======================================================
分析一下這三種方法的優缺點:
1.單純有cookie實現購物車,這樣的購物車不是很理想,設想一下,如果客戶端的瀏覽器把cookie給禁用了,這種方法就會在這里流產...
2.session中保存購物車的信息,這個只是在一個會話中可用,如果用戶沒有登錄,或者說登錄了以後,添加購物車,在關閉瀏覽器或者登出後,之前所添加的購物車通通都流產啦...
3.用cookie和資料庫(購物車信息持久化)實現購物車;
主要的流程:
A.用戶登錄前的數據流:用戶在沒有登錄系統的時候,對喜歡的商品進行添加購物車,那麼這個時候,我們可以把購物車信息保存
到cookie中,這里會涉及到cookie的添加,修改操作;也即如果之前在cookie中不存對應的cookie,則就對cookie進行添加操作。
如果在cookie中存在對應的cookie,那麼,這時候,就要對cookie進行修改操作了(這里涉及到用戶對同一個商品進行多次添加購物車的情況)。
B.用戶登錄後的數據流:用戶在登錄後,系統首先做的第一件事就是去獲取對應的cookies,如果存在相關的購物車cookies,那麼就對該購物車
信息進行相應用戶User的持久化操作,要麼添加,要麼修改。(添加操作:該用戶所對應的購物車如果沒有相應的信息進行添加操作;修改操作:類似的,
如果存在對應用戶的購物車信息,就進行修改操作)。用戶登錄後,也可以進行購物車的添加操作,不過,這里不是添加到cookie中,而是直接持久化到資料庫中。
I. 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;
}
}
J. JavaWeb開發中,怎麼根據用戶來實現單個的購物車,小白入門,請指教。能附上源碼跟容易理解謝謝!
1、這個如果連抄資料庫就很簡襲單了,cart資料庫,首先有id,proctID,然後userid,就是買家的id,然後PublishID,視為賣家id,其他的數據從proct表裡面讀,避免了重復存。
2、如果單純的起步學習,我還記得我小時候,老師講用session,用戶進入系統後如果要登錄就用用戶id去存session,不登錄就隨即生成一個id存session,購物車就根據用戶id讀session數據。