重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
点击数量进入购物车页面,这个应该好做吧,跳动一个Action转发到购物车页面
创新互联公司-专业网站定制、快速模板网站建设、高性价比溆浦网站开发、企业建站全套包干低至880元,成熟完善的模板库,直接使用。一站式溆浦网站制作公司更省心,省钱,快速模板网站建设找我们,业务覆盖溆浦地区。费用合理售后完善,10余年实体公司更值得信赖。
下面是我的图书购物旁隐穗车(自己写的)
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;
/**
* 该类提供购物车的操作
* */
public class CartItemsService {
private MapString,BookCar itemMap=null;//购物车Map集合
private CollectionBookCar items;//购物车项
public CartItemsService()
{
itemMap=new HashMapString ,BookCar();
}
public CollectionBookCar getItems() {
return items;
}
public void setItems(CollectionBookCar运卜 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)//根据图书ID增加数量
{
BookCar bookCar=itemMap.get(bookid);
if(bookCar!=null)
{
bookCar.setQuantity(quantity);
}
}
public void clear(){
itemMap.clear();
}
}
修改后传入这个方法就可以了setQuantityById()
★★★ 注意购物车一定要用Map 不能用List或ArrayList
因为购物车是顾客频繁操作的功能
Map在取值或删除值的效率比List或ArrayList要高的多
它基本不需要时间,而List或ArrayList还要遍历。。。。。。
希望对你有帮助!!
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到购物车
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 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; //清空总价,防答虚启止无清如限累计
for(int j=0;jal.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);
}
}
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可能显示不出来你的效果。不满意得话我在给你编一个。