點(diǎn)擊數(shù)量進(jìn)入購(gòu)物車頁(yè)面,這個(gè)應(yīng)該好做吧,跳動(dòng)一個(gè)Action轉(zhuǎn)發(fā)到購(gòu)物車頁(yè)面
創(chuàng)新互聯(lián)公司-專業(yè)網(wǎng)站定制、快速模板網(wǎng)站建設(shè)、高性價(jià)比溆浦網(wǎng)站開(kāi)發(fā)、企業(yè)建站全套包干低至880元,成熟完善的模板庫(kù),直接使用。一站式溆浦網(wǎng)站制作公司更省心,省錢(qián),快速模板網(wǎng)站建設(shè)找我們,業(yè)務(wù)覆蓋溆浦地區(qū)。費(fèi)用合理售后完善,10余年實(shí)體公司更值得信賴。
下面是我的圖書(shū)購(gòu)物旁隱穗車(自己寫(xiě)的)
package com.jc.ts.services;
import java.math.BigDecimal;
import java.util.Collection;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import com.jc.ts.entity.BookCar;
import com.jc.ts.entity.BookInfo;
/**
* 該類提供購(gòu)物車的操作
* */
public class CartItemsService {
private MapString,BookCar itemMap=null;//購(gòu)物車Map集合
private CollectionBookCar items;//購(gòu)物車項(xiàng)
public CartItemsService()
{
itemMap=new HashMapString ,BookCar();
}
public CollectionBookCar getItems() {
return items;
}
public void setItems(CollectionBookCar運(yùn)卜 items) {
this.items = items;
}
public MapString, BookCar攜轎 getItemMap() {
return itemMap;
}
public void setItemMap(MapString, BookCar itemMap) {
this.itemMap = itemMap;
}
public int getBookCarSize()
{
return itemMap.size();
}
public boolean containById(String bookid)
{
return itemMap.containsKey(bookid);
}
public void addBookCarItems(BookInfo bookInfo)
{
if(bookInfo!=null)
{
BookCar bookCar=(BookCar)itemMap.get(bookInfo.getBid());
if(bookCar==null)
{
bookCar=new BookCar();
bookCar.setBookinfo(bookInfo);
bookCar.increaseQuantity();
itemMap.put(bookInfo.getBid(),bookCar);
items=itemMap.values();
}else {
bookCar.increaseQuantity();
}
}
}
public BookInfo removeCarItem(String bookid)
{
BookCar bookCar=itemMap.remove(bookid);
if(bookCar==null)
{
return null;
}
items=itemMap.values();
return bookCar.getBookinfo();
}
public BigDecimal getBookCarTotal()//獲得總金額
{
BigDecimal carTotal=new BigDecimal("0.00");
IteratorBookCar iterator=this.getAllCartItems();
while(iterator.hasNext())
{
BookCar bookCar=iterator.next();
BigDecimal carPrice=bookCar.getBookinfo().getBprice();
BigDecimal quantity=new BigDecimal(String.valueOf(bookCar.getQuantity()));
carTotal=carTotal.add(carPrice.multiply(quantity));
}
return carTotal;
}
public IteratorBookCar getAllCartItems(){
return itemMap.values().iterator();
}
public void increaseQuantityById(String bookid)
{
BookCar bookCar=itemMap.get(bookid);
if(bookCar!=null)
{
bookCar.increaseQuantity();
}
}
public void setQuantityById(String bookid,int quantity)//根據(jù)圖書(shū)ID增加數(shù)量
{
BookCar bookCar=itemMap.get(bookid);
if(bookCar!=null)
{
bookCar.setQuantity(quantity);
}
}
public void clear(){
itemMap.clear();
}
}
修改后傳入這個(gè)方法就可以了setQuantityById()
★★★ 注意購(gòu)物車一定要用Map 不能用List或ArrayList
因?yàn)橘?gòu)物車是顧客頻繁操作的功能
Map在取值或刪除值的效率比List或ArrayList要高的多
它基本不需要時(shí)間,而List或ArrayList還要遍歷。。。。。。
希望對(duì)你有幫助!!
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMapString, String hm=new HashMapString, String();
float totlePrice=0;
//添加book到購(gòu)物車
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);
}
}
//修改數(shù)量
public void updateQuantity(String bookId,String bookQuantity){
hm.put(bookId, bookQuantity);
}
/譽(yù)高/獲取購(gòu)物車的所有信息 并計(jì)算總價(jià)
public ArrayListBookBean getShoppingCart(){
ArrayListBookBean al=new ArrayListBookBean();
IteratorString i=hm.keySet().iterator();
String ids="";
BookTableManager btm=new BookTableManager();
while(i.hasNext()){
ids=ids+","+i.next();
}
al= btm.selectByBookIds(ids);
totlePrice=0; //清空總價(jià),防答虛啟止無(wú)清如限累計(jì)
for(int j=0;jal.size();j++){
BookBean bb=al.get(j);
totlePrice+=bb.getPrice()*Integer.parseInt(getQuantityById(bb.getBookId()+""));
}
return al;
}
//獲取總價(jià)
public float getTotlePrice(){
return totlePrice;
}
//根據(jù)ID獲取數(shù)量
public String getQuantityById(String id){
String quantity=hm.get(id);
return quantity;
}
//清空購(gòu)物車
public void clear(){
hm.clear();
}
//刪除購(gòu)物車中的一本書(shū)
public void deleteById(String id){
hm.remove(id);
}
}
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("書(shū)伍談:10元",Label.LEFT);
label4=new Label("襪子:8元",Label.LEFT);
button1=new Button("加入購(gòu)物車");
button2=new Button("加入購(gòu)物車");
button3=new Button("加入購(gòu)物車");
button4=new Button("加入購(gòu)培賀物車");
button5=new Button("查看購(gòu)物車");
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("一個(gè)面紙、");
sum=sum+3;
}
else if(e.getSource()==button2)
{ text.append("一只鋼筆、");
sum=sum+5;
}
else if(e.getSource()==button3)
{ text.append("一本書(shū)、");
sum=sum+10;
}
else if(e.getSource()==button4)
{ text.append("一雙襪子、");
sum=sum+8;
}
else if(e.getSource()==button5)
{
text.append("\n"+"總價(jià)為:"+"\n"+sum);
}
}
}
public class Shopping {
public static void main(String[] args) {
new ShopFrame("購(gòu)物車");
}
}
我沒(méi)用Swing可能顯示不出來(lái)你的效果。不滿意得話我在給你編一個(gè)。
當(dāng)前文章:淘寶購(gòu)物車的java代碼 淘寶購(gòu)物車的java代碼怎么寫(xiě)
轉(zhuǎn)載來(lái)源:http://jinyejixie.com/article42/ddpiiec.html
成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供網(wǎng)站制作、網(wǎng)頁(yè)設(shè)計(jì)公司、網(wǎng)站營(yíng)銷、品牌網(wǎng)站建設(shè)、建站公司、網(wǎng)站策劃
聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請(qǐng)盡快告知,我們將會(huì)在第一時(shí)間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場(chǎng),如需處理請(qǐng)聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時(shí)需注明來(lái)源: 創(chuàng)新互聯(lián)