導航:首頁 > 編程語言 > java加入購物車代碼

java加入購物車代碼

發布時間:2024-09-15 17:13:13

⑴ 編程 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()放到個數的位置

⑵ 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) {
.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.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);
}
}

⑷ 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的購物車商品 不能重復添加 的代碼。

在購物車中寫:購物車一般放到session域中。其中有商品小計,有商品總計回的一些求和計算。我記答得用Map來弄的。 在購物車中添加map集合。根據放到購物車中的key來判斷在map中是否存在,如果存在那就在原有商品上+1,如果不存在,那麼就可以添加商品到購物車。

⑹ java 裡面編寫網上購物車的問題

有兩個地方錯了:
主要的一個錯誤是BookDetail類不存在,而你在ShopCar
類中引用了BookDetail,得寫一個這樣的類才行。

第二個錯誤的地方為你在類中使用了包,如果使用包的話就不能像一般的類那樣直接使用javac 類名.java這樣的形式編譯了。
而要使用 javac -d . 類名.java才行,其中的-d .的意思是把輸出的.class文件放以包名稱的形式到當前文件夾下,當然你也可以輸入其它路徑也是可以的。

運行的話使用java 包名+類名就可以了,其中是以.分開包和類名稱的。

看一下javac的幫助和java的幫助,裡面說的很明白的。
編譯的時候使用javac -d . 類名.java
運行的時候 java 包名.類名
注意如果包里又有子包的話運行的時候包與子包使用.分隔開來

閱讀全文

與java加入購物車代碼相關的資料

熱點內容
電腦列印東西用什麼文件 瀏覽:473
win8如何關掉程序 瀏覽:131
編程怎麼編寫手機簡訊驗證 瀏覽:791
牽手是什麼網站 瀏覽:379
哪個編程課是直播 瀏覽:272
win10顯示不出手機文件夾 瀏覽:705
word文件閱讀器 瀏覽:766
網站小圖標怎麼弄 瀏覽:58
怎麼弄電腦密碼 瀏覽:114
labview中將表格轉為excel文件 瀏覽:454
口袋腕力升級 瀏覽:436
word刻錄到光碟刻錄大師 瀏覽:124
更改壓縮文件後卡死是什麼原因 瀏覽:201
電腦上如何發qq紅包 瀏覽:230
如何刪除word的空白頁 瀏覽:822
創建swap文件 瀏覽:468
數控銑床編程在哪裡編寫 瀏覽:544
iphone文藝軟體 瀏覽:938
紀元1404人口升級 瀏覽:527
公司網路如何設置自己路由器 瀏覽:649

友情鏈接