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

用java购物车功能

发布时间: 2021-02-28 22:05:03

『壹』 java中购物车的功能怎么实现

session+cookie。先从cookie里读取上次的购物清单到session,本次添加的购物清单也将加入session。

『贰』 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实现

放进购物车的东西,添加到List,然后用Sesion......发送、接收

『肆』 java购物车功能怎么实现

设置基本的实体类就不用说了吧。再设置一个购物车的实体类,接口和实现类。利用Session机制来存储所选的物品,然后同意购物的时候将session中所存储的物品List存入表中。

『伍』 用JAVA编写购物车程序.

我这有你要不?刚写的!
不过不是保存在内存上了,而是保存在数据库中,用完删不得了!

『陆』 用java模拟购物车

packagecom.shopping;

importjava.util.HashMap;
importjava.util.Map;

publicclassShoppingTrolley{
publicShoppingTrolley(){
super();
}

privateMap<String,Integer>amountOfItem=newHashMap<String,Integer>();

/**
*Additemtoshoppingtrolley.
*
*@paramitemID
*@paramamount
*@.
*/
publicvoidaddItem(StringitemID,intamount){
if(amountOfItem.containsKey(itemID)){
amountOfItem.put(itemID,amountOfItem.get(itemID)+amount);

}else{
amountOfItem.put(itemID,amount);

}
}

/**
*Removeitemfromshoppingtrolley.
*
*@paramitemID
*@paramamount
*@.
*/
publicvoidremoveItem(StringitemID,intamount){
if(amountOfItem.containsKey(itemID)){
if(amount>=amountOfItem.get(itemID)){
amountOfItem.remove(itemID);

}else{
amountOfItem.put(itemID,amountOfItem.get(itemID)-amount);

}
}
}

}

上面代码是问题1


问题2 下图是类图 代码都在附件里

『柒』 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购物车怎么写

用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 如何实现类似购物车功能

给你介绍三种可以实现购物车功能的方法:

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中,而是直接持久化到数据库中。

『拾』 编程 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()放到个数的位置