❶ java購物車怎麼寫
用Vector 或者是HashMap去裝
<下面有部分代碼你去看吧>
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 Map<String, CartItemBean> update(Map cart, String foodID,
boolean isAddorRemove) {
Map map;
if (isAddorRemove) {
map = add(cart, foodID);
} else {
map = remove(cart, foodID);
}
return map;
}
}
❷ java購物車功能怎麼實現
設置基本的實體類就不用說了吧。再設置一個購物車的實體類,介面和實現類。利用Session機制來存儲所選的物品,然後同意購物的時候將session中所存儲的物品List存入表中。
❸ 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可能顯示不出來你的效果。不滿意得話我在給你編一個。
❹ java中寫商品購物車怎麼實現每種商品總價都隨商品數量變化而變化
一般就是你在頁面增加監聽事件,每次修改商品數量就重新計算總價
❺ 面試JAVA軟體工程師 人家問你購物車怎麼做的用了哪些技術怎麼說
用cookie和資料庫(購物車信息持久化)實現購物車;
主要的流程:
A.用戶登錄前的數據迴流:用戶在沒有登錄系統的答時候,對喜歡的商品進行添加購物車,那麼這個時候,我們可以把購物車信息保存
到cookie中,這里會涉及到cookie的添加,修改操作;也即如果之前在cookie中不存對應的cookie,則就對cookie進行添加操作。
如果在cookie中存在對應的cookie,那麼,這時候,就要對cookie進行修改操作了(這里涉及到用戶對同一個商品進行多次添加購物車的情況)。
B.用戶登錄後的數據流:用戶在登錄後,系統首先做的第一件事就是去獲取對應的cookies,如果存在相關的購物車cookies,那麼就對該購物車
信息進行相應用戶User的持久化操作,要麼添加,要麼修改。(添加操作:該用戶所對應的購物車如果沒有相應的信息進行添加操作;修改操作:類似的,
如果存在對應用戶的購物車信息,就進行修改操作)。用戶登錄後,也可以進行購物車的添加操作,不過,這里不是添加到cookie中,而是直接持久化到
資料庫中。註:用戶登錄後的數據都是和資料庫打交道。
❻ java 如何編寫購物車
用Vector 或者是HashMap去裝
<下面有部分代碼你去看吧>
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 Map<String, CartItemBean> update(Map cart, String foodID,
boolean isAddorRemove) {
Map map;
if (isAddorRemove) {
map = add(cart, foodID);
} else {
map = remove(cart, foodID);
}
return map;
}
}
❼ java寫的簡易購物車 簡歷里描述一下
第一: 寫你的開發平台:比如windows 還是Linux。
第二:寫你的開發工具:MyEclipse 還是Eclipse。回 資料庫MySQL 還是Oracle。。。。。
第三:寫你的框答架:ssh+jquery +Ajax+jsp+......
第四:寫你參與的開發模塊。涉及到的技術亮點。比如登陸模塊,用到的是AJAX非同步數據驗證。或者復雜點的:在線文件預覽:用到技術 OpenOffice+FlexPaper+swfTools等。
第五 :寫你遇到技術難題,你自己是如何解決的(注意多寫自己獨立解決:面試官很在意個人自我解決能力或者自學能力),
第六:寫你完成這個項目的收獲。
大致也就這樣了。
❽ Java 如何實現類似購物車功能
給你介紹三種可以實現購物車功能的方法:
1.用cookie實現購物車;
2.用session實現購物車;
3.用cookie和資料庫(購物車信息持久化)實現購物車;
=======================================================
分析一下這三種方法的優缺點:
1.單純有cookie實現購物車,這樣的購物車不是很理想,設想一下,如果客戶端的瀏覽器把cookie給禁用了,這種方法就會在這里流產...
2.session中保存購物車的信息,這個只是在一個會話中可用,如果用戶沒有登錄,或者說登錄了以後,添加購物車,在關閉瀏覽器或者登出後,之前所添加的購物車通通都流產啦...
3.用cookie和資料庫(購物車信息持久化)實現購物車;
主要的流程:
A.用戶登錄前的數據流:用戶在沒有登錄系統的時候,對喜歡的商品進行添加購物車,那麼這個時候,我們可以把購物車信息保存
到cookie中,這里會涉及到cookie的添加,修改操作;也即如果之前在cookie中不存對應的cookie,則就對cookie進行添加操作。
如果在cookie中存在對應的cookie,那麼,這時候,就要對cookie進行修改操作了(這里涉及到用戶對同一個商品進行多次添加購物車的情況)。
B.用戶登錄後的數據流:用戶在登錄後,系統首先做的第一件事就是去獲取對應的cookies,如果存在相關的購物車cookies,那麼就對該購物車
信息進行相應用戶User的持久化操作,要麼添加,要麼修改。(添加操作:該用戶所對應的購物車如果沒有相應的信息進行添加操作;修改操作:類似的,
如果存在對應用戶的購物車信息,就進行修改操作)。用戶登錄後,也可以進行購物車的添加操作,不過,這里不是添加到cookie中,而是直接持久化到資料庫中。
❾ 編程 java 關於購物車
將session中的商品定義為對象
放入ArrayList中
修改了購物車內容後
把ArrayList.size()賦值到現在是0的位置回
如:
購物車添加物品答
List proctList = new ArrayList();
Proct proct1 = new Proct();
proctList.add(proct1);
...
Proct proctN = new Proct();
proctList.add(proctN);
session.setAttribute("proct", proctList);
頁面可以寫
List proctList = (List)session.getAttribute("proct");
把proctList.size()放到個數的位置
❿ 購物車的Java代碼
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;public class ShoppingCartManager {
HashMap<String, String> hm=new HashMap<String, 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 ArrayList<BookBean> getShoppingCart(){
ArrayList<BookBean> al=new ArrayList<BookBean>();
Iterator<String> 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;j<al.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);
}
}