用Vector 或者是HashMap去装
<下面有部分代码你去看吧>
package com.aptech.restrant.DAOimport 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
}
}
购物车管理模块主要功能有如下几个部分:(1)创建购物车 当客户登录后,系统会给客户创建一个购物车放入服务器的Session会话中。使客户在整个会话中都拥有一个相同的购物车。这里主要运用了Http协议中的会话机制,将购物车保存在客户的会话中,这样在整个客户游览不同页面商品的过程中,都会使用同一个购物车对象。 具体执行步骤:(1)从客户的请求对象中获取Session会话对象(2)从会话对象中获取购物车对象(3)判断是购物车对象是不是空的,如果是空是就创建一个/* * 在监听到session被创建之后,就立即向session中添加一个购物车Car; */public void sessionCreated(HttpSessionEvent arg0) { HttpSession session = arg0.getSession() Cart cart=new Cart() session.setAttribute("cart", cart) }/** 从session中获得购物车*/ Cart cart = (Cart) session.getAttribute("cart") if (cart == null) { cart = new Cart() }(2)向购物车中添加一个商品项 客户在查看网页上的一个商品时,当向服务器发送一个“添加到购物车”的请求时,会执行这个功能。功能执行过程:(1)从客户请求对象中获取商品的ID(2)调用业务层的方法根据商品ID去数据查询商品的信息,返回商品对象(3)从商品对象中获取商品名,商品价格,来构建一个商品项对象(4)从Session会话中获取购物车对象(5)调用业务层的方法来根据购物车对象和商品项对象来执行添加操作(6)将些商品项对象放入到购物车中 部分实现代码: /** 从数据库中把商品取到;*/ ProductService productService = (ProductService) ServiceFactory.getInstance().getService(Globals.PRODUCT_SERVICE) Integer id = Integer.parseInt(request.getParameter("productid")) Product product = productService.getProductById(id) /** 在向购物车中添加商品的时候会判断商品是否已经存在,* 已存在的就不让在加入了;*/ if (cart.isExist(id)) { message = "该商品已经存在!请<a onclick='javascript:history.go(-1)'>返回</a>!" request.setAttribute("message", message) return mapping.findForward("error") } else { /** 向购物车添加一个商品;*/ cart.addCart(product) session.setAttribute("cart", cart) return mapping.findForward("addcartsuccess") } package Testimport java.util.LinkedHashMap
import java.util.Map
import java.util.Map.Entry
import java.util.Scanner
public class Test {
public static void main(String[] args) {
init()//初始化
Map<String,String> map = new LinkedHashMap<>()
while(true){
Scanner in= new Scanner(System.in)
map = buy(in,map)//选择
System.out.println()
System.out.println("还要继续购物吗?(Y/N)")
String jx = in.nextLine()
if(jx.equals("N")){
break
}
}
print(map)
}
public static void print(Map<String, String> m){
System.out.println("\n\n\n******************")
System.out.println(" 购物车清单")
Integer hj = 0
for (Entry<String, String> entry : m.entrySet()) {
String key = entry.getKey()
String value = entry.getValue()
if(key.equals("1")){
hj += Integer.parseInt(value)*3
System.out.println("哇哈哈纯净水: "+value+"件,合计:¥"+Integer.parseInt(value)*3)
}else if(key.equals("2")){
hj += Integer.parseInt(value)*5
System.out.println("康师傅方便面: "+value+"件,合计:¥"+Integer.parseInt(value)*5)
}else if(key.equals("3")){
hj += Integer.parseInt(value)*4
System.out.println("可口可乐: "+value+"件,合计:¥"+Integer.parseInt(value)*4)
}
}
System.out.println("合计金额:¥"+hj)
}
public static void init(){
System.out.println("******************")
System.out.println("\t商品列表\n")
System.out.println(" 商品名称 价格")
System.out.println("1. 哇哈哈纯净水 ¥3")
System.out.println("2. 康师傅方便面 ¥5")
System.out.println("3. 可口可乐 ¥4")
System.out.println("******************")
}
public static Map<String,String> buy(Scanner scan,Map<String,String> m){
System.out.print("请输入编号:")
String bh = scan.nextLine()
System.out.print("请输入购买数量:")
String num = scan.nextLine()
if(m.size()>0 && m.keySet().contains(bh)){
m.put(bh,(Integer.parseInt(bh)+Integer.parseInt(num))+"")
}else{
m.put(bh, num)
}
return m
}
}