(餐饮项目)餐桌缓存菜品总金额(Service实现)

本文介绍了如何在餐饮项目中实现Service层的方法,用于计算餐桌上的菜品总金额。详细阐述了在Service接口和实现类中进行的功能逻辑。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

①在对应的Service里写方法名(前面注释写下面方法的用途)

②在Implement里实现上诉方法,具体实现:

@Override
public BigDecimal returnTotalMoneyByTableId(int tableId) throws SSException
{
    //定义并初始化返回总金额
    BigDecimal totalMoney = new BigDecimal(0);
    //定义一个TableOrderCache对象(里面有个属性,包含所有菜品名)
    TableOrderCache tableOrderCache = new TableOrderCache();
    //定义tableOrderCache里的List数组orderDishCaches(所有菜品的集合)
    List<OrderDishCache> orderDishCaches = new ArrayList<OrderDishCache>();
    try {
        //餐台Id正常,才能通过Id去tableOrderCacheMap(缓存)里查找信息
        if(!Assert.lessOrEqualZero(tableId)){
            tableOrderCache = tableOrderCacheMap.get(tableId);
        }
        //tableOrderCache不为空,才可以获得该对象的orderDishCacheList
        if(tableOrderCache != null) {
            orderDishCaches = tableOrderCache.getOrderDishCacheList();
        }
        //orderDishCaches先得不返回null,才能判断该数组是否为空数组(空数组:包含0个元素)
        if(orderDishCaches != null && !orderDishCaches.isEmpty()) {
            // 在OrderDishCache对象中定一个dto(每个菜,里面有菜的数量),用来遍历数组orderDishCaches,
            for(OrderDishCache dto : orderDishCaches){
                //菜品售价(DishDto里有)定义一个DishDto
                DishDto dishDto = new DishDto();
                //通过dishService.queryById(dto.getDishId(菜对应的Id)),可以得到DishDto
                dishDto = dishService.queryById(dto.getDishId());
                //求所有菜的总和 totalMoney.add(), 类型相同才能*,将售价变为和数量相同的float类型.
                totalMoney = totalMoney.add(new BigDecimal(dishDto.getSalePrice().floatValue() * dto.getQuantity()));
            }
        }
    } catch (Exception e) {
        LogClerk.errLog.error(e);
        throw SSException.get(EmenuException.CountOfOrderFail, e);
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值