java的購物車代碼
A. 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可能顯示不出來你的效果。不滿意得話我在給你編一個。
B. JAVA語言編寫的網上訂餐系統購物車功能如何實現
用Vector 或者是HashMap去裝
<下面有部分代碼你去看吧>
packagecom.aptech.restrant.DAO;
importjava.util.ArrayList;
importjava.util.HashMap;
importjava.util.List;
importjava.util.Map;
importjava.util.Set;
importjava.sql.Connection;
importcom.aptech.restrant.bean.CartItemBean;
importcom.aptech.restrant.bean.FoodBean;
publicclassCartModel{
privateConnectionconn;
publicCartModel(Connectionconn){
this.conn=conn;
}
/**
*得到訂餐列表
*
*@return
*/
publicListchangeToList(Mapcarts){
//將Set中元素轉換成數組,以便使用循環進行遍歷
Object[]foodItems=carts.keySet().toArray();
//定義double變數total,用於存放購物車內餐品總價格
doubletotal=0;
Listlist=newArrayList();
//循環遍歷購物車內餐品,並顯示各個餐品的餐品名稱,價格,數量
for(inti=0;i<foodItems.length;i++){
//從Map對象cart中取出第i個餐品,放入cartItem中
CartItemBeancartItem=(CartItemBean)carts
.get((String)foodItems[i]);
//從cartItem中取出FoodBean對象
FoodBeanfood1=cartItem.getFoodBean();
//定義int類型變數quantity,用於表示購物車中單個餐品的數量
intquantity=cartItem.getQuantity();
//定義double變數price,表示餐品單價
doubleprice=food1.getFoodPrice();
//定義double變數,subtotal表示單個餐品總價
doublesubtotal=quantity*price;
////計算購物車內餐品總價格
total+=subtotal;
cartItem.setSubtotal(subtotal);
cartItem.setTotal(total);
list.add(cartItem);
}
returnlist;
}
/**
*增加訂餐
*/
publicMapadd(Mapcart,StringfoodID){
//購物車為空
if(cart==null){
cart=newHashMap();
}
FoodModelfd=newFoodModel(conn);
FoodBeanfood=fd.findFoodById(foodID);
//判斷購物車是否放東西(第一次點餐)
if(cart.isEmpty()){
CartItemBeancartBean=newCartItemBean(food,1);
cart.put(foodID,cartBean);
}else{
//判斷當前菜是否在購物車中,false表示當前菜沒有被點過。。
booleanflag=false;
//得到鍵的集合
Setset=cart.keySet();
//遍歷集合
Object[]obj=set.toArray();
for(inti=0;i<obj.length;i++){
Objectobject=obj[i];
//如果購物車已經存在當前菜,數量+1
if(object.equals(foodID)){
intquantity=((CartItemBean)cart.get(object))
.getQuantity();
quantity+=1;
System.out.println(quantity);
((CartItemBean)cart.get(object)).setQuantity(quantity);
flag=true;
break;
}
}
if(flag==false){
//把當前菜放到購物車裡面
CartItemBeancartBean=newCartItemBean(food,1);
cart.put(foodID,cartBean);
}
}
returncart;
}
/**
*取消訂餐
*/
publicMapremove(Mapcart,StringfoodID){
cart.remove(foodID);
returncart;
}
/**
*更新購物車信息
*
*@paramcart
*@paramfoodID
*@return
*/
publicMap<String,CartItemBean>update(Mapcart,StringfoodID,
booleanisAddorRemove){
Mapmap;
if(isAddorRemove){
map=add(cart,foodID);
}else{
map=remove(cart,foodID);
}
returnmap;
}
}