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,码j码ivy 下载源码 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;
}
}
JavaWeb开发中,怎么根据用户来实现单个的购物购物购物车,小白入门,车源车源请指教。码j码能附上源码跟容易理解谢谢!购物购物
1、车源车源这个如果连数据库就很简单了,码j码cart数据库,购物购物红包扫尾源码首先有id,车源车源productID,码j码然后userid,反馈表单源码就是买家的id,然后PublishID,视为卖家id,c dotnetbar源码其他的数据从product表里面读,避免了重复存。
2、如果单纯的起步学习,我还记得我小时候,老师讲用session,用户进入系统后如果要登录就用用户id去存session,不登录就随即生成一个id存session,购物车就根据用户id读session数据。
ç¼ç¨ java å ³äºè´ç©è½¦
ç¹å»æ°éè¿å ¥è´ç©è½¦é¡µé¢ï¼è¿ä¸ªåºè¯¥å¥½åå§ï¼è·³å¨ä¸ä¸ªAction转åå°è´ç©è½¦é¡µé¢
ä¸é¢æ¯æçå¾ä¹¦è´ç©è½¦(èªå·±åç)
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 Map<String,BookCar> itemMap=null;//è´ç©è½¦Mapéå
private Collection<BookCar> items;//è´ç©è½¦é¡¹
public CartItemsService()
{
itemMap=new HashMap<String ,BookCar>();
}
public Collection<BookCar> getItems() {
return items;
}
public void setItems(Collection<BookCar> items) {
this.items = items;
}
public Map<String, BookCar> getItemMap() {
return itemMap;
}
public void setItemMap(Map<String, 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.");
Iterator<BookCar> 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 Iterator<BookCar> 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è¿è¦éåãããããã
å¸æå¯¹ä½ æ帮å©!!
ç¨JAVA+SERVLET+JAVABEANåè´ç©è½¦ä»£ç
ä½ ç¨
åä¸ä¸ªè´ç©è½¦çbeanï¼éè¾¹æ产ååæ°é
public class CarProduct {
private XXX XXX;
private int count;
public Book getXXX() {
return XXX;
}
public void setXXX(XXX XXX) {
this.XXX= XXX;
}
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
}
ç¨sessionåè´ç©è½¦éçä¸è¥¿ï¼æ交订åçæ¶åï¼æè´ç©è½¦æ¸ 空就è¡ï¼
2024-11-13 10:27
2024-11-13 10:06
2024-11-13 09:23
2024-11-13 09:03
2024-11-13 08:42