java購物車原代碼實現(xiàn) java購物車應該用哪個集合實現(xiàn)
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中元素轉(zhuǎn)換成數(shù)組,以便使用循環(huán)進行遍歷
Object[] foodItems = carts.keySet().toArray();
// 定義double變量total,用于存放購物車內(nèi)餐品總價格
double total = 0;
List list = new ArrayList();
// 循環(huán)遍歷購物車內(nèi)餐品,并顯示各個餐品的餐品名稱,價格,數(shù)量
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,用于表示購物車中單個餐品的數(shù)量
int quantity = cartItem.getQuantity();
// 定義double變量price,表示餐品單價
double price = food1.getFoodPrice();
// 定義double變量,subtotal表示單個餐品總價
double subtotal = quantity * price;
// // 計算購物車內(nèi)餐品總價格
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];
// 如果購物車已經(jīng)存在當前菜,數(shù)量+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 MapString, 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中購物車的功能怎么實現(xiàn)
一般利用session,當貨物提交后,讓session失效,這樣就可以完成簡單的購物車。用cookie保存本地也可以??茨愕木唧w需求了。
上一篇:穿梭框后端JAVA代碼 穿梭框如何獲取右邊數(shù)據(jù)
欄 目:Java編程
下一篇:沒有了
本文標題:java購物車原代碼實現(xiàn) java購物車應該用哪個集合實現(xiàn)
本文地址:http://mengdiqiu.com.cn/a1/Javabiancheng/17369.html
您可能感興趣的文章
- 04-10穿梭框后端JAVA代碼 穿梭框如何獲取右邊數(shù)據(jù)
- 04-10java代碼打印信息嗎 java打印的代碼
- 04-10家電控制系統(tǒng)JAVA代碼 家電智能化控制系統(tǒng)
- 04-10五子棋游戲的java代碼 基于java的五子棋游戲的設計代碼
- 04-10矩陣乘法java代碼 java編寫矩陣乘法
- 04-10找質(zhì)數(shù)java實現(xiàn)代碼 找質(zhì)數(shù)java實現(xiàn)代碼怎么做
- 04-10前端寫java代碼 java寫前端還是后端
- 04-10java改變字體代碼 java怎么改變字體
- 04-10java學習代碼庫 java代碼教學
- 04-10b2bjava開源代碼的簡單介紹


閱讀排行
本欄相關(guān)
- 04-11java購物車原代碼實現(xiàn) java購物車應該
- 04-10穿梭框后端JAVA代碼 穿梭框如何獲取右
- 04-10java代碼打印信息嗎 java打印的代碼
- 04-10家電控制系統(tǒng)JAVA代碼 家電智能化控制
- 04-10五子棋游戲的java代碼 基于java的五子
- 04-10矩陣乘法java代碼 java編寫矩陣乘法
- 04-10找質(zhì)數(shù)java實現(xiàn)代碼 找質(zhì)數(shù)java實現(xiàn)代
- 04-10前端寫java代碼 java寫前端還是后端
- 04-10java改變字體代碼 java怎么改變字體
- 04-10java學習代碼庫 java代碼教學
隨機閱讀
- 01-10HTML頁面跳轉(zhuǎn)及參數(shù)傳遞問題
- 01-11帝國CMS會員注冊加入問答驗證的方法
- 08-05DedeCMS首頁網(wǎng)址中帶index.html的解決方法
- 01-10C++ 中實現(xiàn)把EXCEL的數(shù)據(jù)導入數(shù)據(jù)庫(
- 01-11帝國cms 二級域名綁定欄目的最完美的
- 01-11在Swift中如何使用正則表達式詳解
- 01-10希爾排序算法的C語言實現(xiàn)示例
- 01-10C# WebApi Get請求方式傳遞實體參數(shù)的方
- 01-10Eclipse對printf()不能輸出到控制臺的快
- 01-10用vbs列出機器上所有能調(diào)用的組件