『壹』 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()放到個數的位置