用Vector 或者是HashMap去裝
成都創新互聯公司于2013年成立,先為昌吉等服務建站,昌吉等地企業,進行企業商務咨詢服務。為昌吉企業網站制作PC+手機+微官網三網同步一站式服務解決您的所有建站問題。
下面有部分代碼你去看吧
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) {
this.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 MapString, CartItemBean update(Map cart, String foodID,
boolean isAddorRemove) {
Map map;
if (isAddorRemove) {
map = add(cart, foodID);
} else {
map = remove(cart, foodID);
}
return map;
}
}
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可能顯示不出來你的效果。不滿意得話我在給你編一個。
有兩個地方錯了:
主要的一個錯誤是BookDetail類不存在,而你在ShopCar
類中引用了BookDetail,得寫一個這樣的類才行。
第二個錯誤的地方為你在類中使用了包,如果使用包的話就不能像一般的類那樣直接使用javac 類名.java這樣的形式編譯了。
而要使用 javac -d . 類名.java才行,其中的-d .的意思是把輸出的.class文件放以包名稱的形式到當前文件夾下,當然你也可以輸入其它路徑也是可以的。
運行的話使用java 包名+類名就可以了,其中是以.分開包和類名稱的。
看一下javac的幫助和java的幫助,里面說的很明白的。
編譯的時候使用javac -d . 類名.java
運行的時候 java 包名.類名
注意如果包里又有子包的話運行的時候包與子包使用.分隔開來
本文題目:JAVA購物車代碼翻譯 java購物車源碼
本文網址:http://m.2m8n56k.cn/article40/ddgohho.html
成都網站建設公司_創新互聯,為您提供App設計、網站排名、動態網站、品牌網站設計、微信公眾號、云服務器
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:[email protected]。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯