⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 shoppingcart.java

📁 一个jsp与servlet的购物车的小事例。
💻 JAVA
字号:
package cn.com.s251.team1.movieshow.vo;

import java.util.*;
import cn.com.s251.team1.movieshow.dao.*;
import cn.com.s251.team1.movieshow.dao.interfaces.*;
public class ShoppingCart {

    Map cart = new Hashtable();

    public ShoppingCart() {
    }

    // 添加商品
    public void addmovie(String mid) {
        int amount = 0;
        if (cart.containsKey(mid)) {
            Object count = cart.get(mid);
            if (count instanceof Integer) {
                amount = ((Integer) count).intValue();
            }
        }
        amount++;
        cart.put(mid, new Integer(amount));
    }

    public void setAmount(String mid, String amount) {
        Object count = cart.get(mid);
        if (count instanceof Integer) {
            try {
                int num = Integer.parseInt(amount);
                if (num < 1) {
                    throw new Exception("商品数量不能小于等于0!");
                }
                cart.put(mid, new Integer(num));
            } catch (Exception ex) {
            }
        }
    }

    // 删除商品
    public void removemovie(String mid) {
        cart.remove(mid);
    }

    // 获取购物车中所有商品的信息
    public Object[][] getItems() throws Exception {
        IMovieInfoDao movieInfoDao = DaoFactory.movieInfoDao();
        String mid;
        MovieInfo movie = null;
        Set keys = cart.keySet();
        Object[][] items = new Object[keys.size()][5];
        Iterator it = keys.iterator();
        for (int i = 0; i < keys.size(); i++) {
            mid = (String) it.next();
            Integer amount = (Integer) cart.get(mid);
            movie = movieInfoDao.findMovieByID(Integer.parseInt(mid));
            items[i][0] = mid;
            items[i][1] = movie.getMovieName();
            items[i][2] = movie.getMoney();
            items[i][3] = cart.get(mid);
            items[i][4] = new Float(amount.intValue() * new Integer(movie.getMoney()).intValue());
        }
        return items;
    }
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -