当前位置:首页 » 网购平台 » java购物车未实现心得
扩展阅读
宁波奥德赛优惠价格 2021-03-15 14:26:02
丹尼斯购物卡能挂失么 2021-03-15 14:25:58
淘宝购物指纹验证失败 2021-03-15 14:24:44

java购物车未实现心得

发布时间: 2021-01-24 06:28:06

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数据。