①在对应的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); } }