当前位置:首页 » 编程语言 » 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 浏览:147
linux虚拟机用gcc编译时显示错误 发布:2025-02-02 09:14:01 浏览:240
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