1. SSH購物車例子
在你DAO層。。。SERVER層實現DAO層。。。最終是在DAO層的實現方法裡面
2. Java購物車源代碼,有發個例子給我。
告訴我郵箱,給你發。
3. JAVA編程購物車問題大神們幫幫忙
不會用Map,建議先學習怎麼用。不能熟練使用集合框架,嚴重影響開發效率。基礎很重要,沒基礎,什麼都不行。 當然,購物車可以不用Map也能實現。 // 從session里取購物車對象 List cart = (List) request.getSession().getAttribute("cart"); // 如果session里沒有,創建 if (cart == null) { cart = new ArrayList(); } // 創建Book對象 BookInfo book = new BookInfo("001", "abc", 1); // 是否購物車里已經有 if (!cart.contains(book)) { // 沒有的時候直接add cart.add(book); } else { // 有的時候,取出 BookInfo tmpBook = (BookInfo) cart.get(cart.indexOf(book)); // 加數量 tmpBook.setCount(tmpBook.getCount() + book.getCount()); } // 把cart放到session里 request.getSession().setAttribute("cart", cart); 補充: 注意:BookInfo要重寫equals方法,上面的代碼材有效。下面這個BookInfo重寫了equals方法,當id相同時,認為是同一本書。如果想用其他方法判斷,自己可以重寫。 public class BookInfo { private String id; private String name; private int count; public BookInfo(String id, String name, int count) { this.id = id; this.name = name; this.count = count; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public int getCount() { return count; } public void setCount(int count) { this.count = count; } public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; BookInfo other = (BookInfo) obj; if (id == null) { if (other.id != null) return false; } else if (!id.equals(other.id)) return false; return true; } } 追問: BookInfo tmpBook = (BookInfo) cart.get(cart.indexOf(book)); 這句裡面的cart.indexOf(book)是用於什麼用的啊?我學了半年JAVA..半年的JSP+servlet+ 框架 呢..我還是新手哈.. 回答: cart.indexOf(book)是查找對象在List中的 索引 。 cart.get(cart.indexOf(book))是根據找到的索引取對象。 追問: 現在能加入 購物車 了.但是不能過濾相同ID的這個怎麼樣在servlet裡面解決呢?我用的是struts的ACTION和form的 回答: 我上面不是寫了BookInfo類嗎!為了讓List能把相同ID的BookInfo對象認為是「同一本書」,要重寫equals方法。 cart.contains(book)是判斷List是否有某Book時,會調用BookInfo的equals方法。舉個例子,用上面那個BookInfo類。new BookInfo("1","abc",1)和new BookInfo("1","abc",1)會被List認為是相同的BookInfo,因為id都是「1」。 追問: 我是用struts中的form來寫的..我加入到form中.我程序就出錯了....我在實驗下吧..
4. 您好!之前看到您有.net購物車的小例子,我也是做畢設 需要做電子書店 我購物車的實現不太明白~~
我這里到是有電子書店的源碼。需要的話留個email.
其實購物車 就是先把要購買的數據插入資料庫,然後再做其實處理。
5. 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可能顯示不出來你的效果。不滿意得話我在給你編一個。
6. thinkphp做的session購物車,求詳細代碼。有完整例子的可以發下。非常感謝
<?php
classCartTool{
privatestatic$ins=null;
private$items=array();
finalprotectedfunction__construct(){
}
finalprotectedfunction__clone(){
}
//獲取實例
protectedstaticfunctiongetIns(){
if(!(self::$insinstanceofself)){
self::$ins=newself();
}
returnself::$ins;
}
//把購物車的單例對象放到session里
publicstaticfunctiongetCart(){
if(!isset($_SESSION['cart'])||!($_SESSION['cart']instanceofself)){
$_SESSION['cart']=self::getIns();
}
return$_SESSION['cart'];
}
/*
添加商品
paramint$id商品主鍵
paramstring$name商品名稱
paramfloat$ 商品價格
paramint$num購物數量
*/
publicfunctionaddItem($id,$name,$price,$brand,$thumb,$num=1){
if($this->hasItem($id)){//如果該商品已經存在,則直接加其數量
$this->incNum($id,$num);
return;
}
$item=array();
$item['id']=$id;
$item['name']=$name;
$item['price']=$price;
$item['brand']=$brand;
$item['thumb']=$thumb;
$item['num']=$num;
$this->items[$id]=$item;
return$this->items[$id];
}
/*
修改購物車中的商品數量
paramint$id商品主鍵
paramint$num某個商品修改後的數量,即直接把某商品的數量改為$num
*/
publicfunctionmodNum($id,$num=1){
if(!$this->hasItem($id)){
returnfalse;
}
$this->items[$id]['num']=$num;
}
/*
商品數量增加1
*/
publicfunctionincNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num']+=$num;
}
}
/*
商品數量減少1
*/
publicfunctiondecNum($id,$num=1){
if($this->hasItem($id)){
$this->items[$id]['num']-=$num;
}
//如果減少後,數量為0了,則把這個商品從購物車刪掉
if($this->items[$id]['num']<1){
$this->delItem($id);
}
}
/*
判斷某商品是否存在
*/
publicfunctionhasItem($id){
returnarray_key_exists($id,$this->items);
}
/*
刪除商品
*/
publicfunctiondelItem($id){
unset($this->items[$id]);
}
/*
查詢購物車中商品的種類
*/
publicfunctiongetCnt(){
returncount($this->items);
}
/*
查詢購物車中商品的個數
*/
publicfunctiongetNum(){
if($this->getCnt()==0){
return0;
}
$sum=0;
foreach($this->itemsas$item){
$sum+=$item['num'];
}
return$sum;
}
/*
查詢購物車中商品的總金額
*/
publicfunctiongetPrice(){
if($this->getCnt()==0){
return0;
}
$price=0.0;
foreach($this->itemsas$item){
$price+=$item['num']*$item['price'];
}
return$price;
}/*
返回購物車中的所有商品
*/
publicfunctionall(){
return$this->items;
}
/*
清空購物車
*/
publicfunctionclear(){
$this->items=array();
}
}
以上是類文件,需要引入的。
$cart=CartTool::getCart();
$car_goods_list=$cart->all();//獲取商品列表
$goods_num=$cart->getNum();//商品個數
$goods_sum_price=$cart->getPrice();//總價格
$cart->addItem($goods_id,$goods_list['goods_name'],$goods_list['shop_price'],$goods_list['brand'],$goods_list['goods_thumb'],$num);//增加一件商品到購物車
$cart->clear();//清空購物車
7. asp.net 三層架構購物車如何實現哪位高人能給個實例
分清層次,底層操作資料庫,中間處理業務邏輯,上層可簡單理解為頁面。上層調用下層,上層的修改不影響下層程序。實例很多,網路一下就有。
主要是積累分層的經驗。
8. 急,類似京東的購物車加減例子望高手想告
上面直接有選數量的啊,進入訂單頁面的時候,數量旁邊也有- +號 點這個就可以直接加減了
9. jsp+mysql購物車示例
通過這個方抄法request.getsession().setAttribute("cart",購物車的數據);放入,然後需要提取的時候就通過request.getsession().getAttribute("cart");得到數據,然後在往資料庫裡面插就好了。