❶ 急求java購物車代碼
package bean;
import java.util.ArrayList;
import java.util.List;
/**
*
* @author Administrator
* 購物車類:
* 為了方便將商品信息綁訂到session上面而設計的一個
* 工具,提供了商品的添加,刪除,列表,計價,清空,
* 修改功能。
*/
public class Cart {
//items屬性:用來保存商品
private List<CartItem> items =
new ArrayList<CartItem>();
/**
* 將商品添加到購物車
*/
public boolean add(CartItem item){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == item.getC().getId()){
//該商品已經購買過
return false;
}
}
//沒有購買過,則添加該商品
items.add(item);
return true;
}
/**
* 從購物車當中刪除某件商品
*/
public void delete(int id){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
items.remove(curr);
return;
}
}
}
/**
* 獲得購物車中所有商品信息
*/
public List<CartItem> list(){
return items;
}
/**
* 商品總價
*/
public double cost(){
double total = 0;
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
total += curr.getC().getPrice() * curr.getQty();
}
return total;
}
/**
* 清空購物車中的所有商品
*/
public void clear(){
items.clear();
}
/**
* 修改購物車中某種商品的數量
*/
public void modify(int id,int qty){
for(int i=0;i<items.size();i++){
CartItem curr = items.get(i);
if(curr.getC().getId() == id){
curr.setQty(qty);
return;
}
}
}
}
❷ 急,寫一個購物車的代碼
我好像有一個類似的,購物車,回去給你找找,報個QQ,
❸ java購物車功能怎麼實現
設置基本的實體類就不用說了吧。再設置一個購物車的實體類,介面和實現類。利用Session機制來存儲所選的物品,然後同意購物的時候將session中所存儲的物品List存入表中。
❹ MVC模式編寫購物車代碼
需求寫的不錯啊。可以實現了,首先看下需要什麼表,根據實體建表,然後對這些表進行增刪改查就ok了。
購物車要注意處理事務。
❺ web前端購物車功能實現
其實思路都是很簡單的,如果你要純前端的實現,那用Javascript或者jquery就可以做了,如果想前後端聯動,那麼具體數據操作你就提交到後台,然後後台重新返回頁面就可以了
❻ java中購物車如何實現的求代碼,網上等。謝謝
我知道的有兩種方式:
面向對象實現: 這個需要兩個表(一個是購物車表,一個是購物項表)
在一個表也可以實現(存: 用戶id, 商品id, 商品數量),根據用戶id來查詢商品.
❼ jsp購物車代碼
//shopping.html
<html>
<head><title>shopping stor</title></head>
<body>
<form action="carts.jsp" target="post">
<br>
please select the item that you want to buy
<br>
<select name="item">
<option>book:old man and the sea
<option>x-box game machine
<option>mp3 player
<option>cce
<option>book:jsp programming
<option>cd "the endless love"
<option>dvd "gone with the wind"
</select>
<br>
<input type="submit" name="submit" value="add">
<input type="submit" name="submit" value="remove">
</form>
</body>
</html>
------------------------------------------------------------------
//carts.jsp
<%@page contentType="text/html;charset=ISO8859_1" %>
<html>
<jsp:useBean id="cart" scope="session" class="test.DummyCart"/>
<jsp:setProperty name="cart" property="*"/>
<%
cart.processRequest();
%>
<br>
<ol>
you have chosen these items:
<%
String []items=cart.getItems();
for(int i=0;i<items.length;i++)
{
%>
<li><%=items[i] %></li>
<%
}
%>
</ol>
<hr>
<%@include file="shopping.htm" %>
</html>
---------------------------------------------------------------------//DummyCart.java
package test;
import javax.servlet.http.*;
import java.util.Vector;
import java.util.Enumeration;
public class DummyCart
{
Vector v = new Vector();
String submit=null;
String item= null;
private void addItem(String name)
{
v.addElement(name);
}
private void removeItem(String name)
{
v.removeElement(name);
}
public void setItem(String s)
{
item=s;
}
public void setSubmit(String s)
{
submit=s;
}
public String[] getItems()
{
String []s=new String[v.size()];
v.Into(s);
return s;
}
public void processRequest()
{
if(submit==null)
addItem(item);
if(submit.equals("add"))
addItem(item);
else if (submit.equals("remove"))
removeItem(item);
reset();
}
private void reset()
{
submit=null;
item=null;
}
}
----------------------------------------------------------------------
上面是一個簡單的例子,功能都能實現,對網頁效果要求更漂亮些的可做一些修改。
❽ 求ASP.NET購物車實現代碼,用SESSION實現的那種
這個是我自己寫的代碼,希望對你有幫助: public void GetBuyShop(int shopId)
{
//獲取當前點擊的商品信息
Shop shop = ShopManager.GetShopByShopId(shopId);
//獲取購物車
Dictionary<string, ShopItem> cart = Session["cart"] as Dictionary<string, ShopItem>;
//判斷購物車是否存在
if (cart == null)
{
cart = new Dictionary<string, ShopItem>();
} ShopItem shopitem = null;
//判斷當前添加的商品在購物車中是否村
foreach (string str in cart.Keys)
{
//如果相等,表示存在
if (str == shop.ShopName)
{
shopitem = cart[str];
}
} //如果為null,表示當前添加的商品早購物車中是不存在的
if (shopitem == null)
{
cart.Add(shop.ShopName, new ShopItem(shop, 1));
}
else
{
shopitem.Count = shopitem.Count + 1;
} Session["cart"] = cart;
Response.Redirect("~/Cart.aspx");
}
❾ 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 如何實現類似購物車功能
給你介紹三種可以實現購物車功能的方法:
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中,而是直接持久化到資料庫中。