public void displayFoodList(String shopIdCode) throws Exception { if (shopList.get(shopIdCode) != null) { stdOut.print(shopList.get(shopIdCode).getName()); stdOut.print("\n Food list:\n"); for (Food food : shopList.get(shopIdCode).getFoodList()) { stdOut.println(food.toString()); } } else { stdErr.println("ShopidCode is not exist, please check the idcode"); } }
时间: 2024-03-28 15:36:51 浏览: 17
这是一个 Java 方法,用于显示指定餐厅的菜单信息。方法的输入参数是餐厅的 ID 编码,方法中使用 if-else 分支结构判断指定 ID 编码对应的餐厅是否存在。如果存在,则使用 System.out.print() 方法输出餐厅名称,并使用 for-each 循环遍历该餐厅的菜单列表,使用 System.out.println() 方法将每个菜单项的信息输出到控制台。每个菜单项的信息包括菜单名称、价格、描述等属性。如果指定 ID 编码对应的餐厅不存在,则使用 System.err.println() 方法输出错误信息,提示用户检查 ID 编码是否正确。这个方法的作用是根据指定的餐厅 ID 编码,获取该餐厅的菜单信息,并展示给用户。
相关问题
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception { if (handler instanceof HandlerMethod) { HandlerMethod handlerMethod = (HandlerMethod) handler; Method method = handlerMethod.getMethod(); RepeatSubmit annotation = method.getAnnotation(RepeatSubmit.class); if (annotation != null) { if (this.isRepeatSubmit(request, annotation)) { AjaxResult ajaxResult = AjaxResult.error(annotation.message()); ServletUtils.renderString(response, JSON.toJSONString(ajaxResult)); return false; } } } boolean flag = false; TokenService tokenService = SpringUtils.getBean(TokenService.class); LoginAppUser loginUser = tokenService.getLoginUser(request); if (loginUser != null && loginUser.getGuest() != null && loginUser.getGuest().getGuestNum() != null) { tokenService.verifyToken(loginUser); String guestNum = loginUser.getGuest().getGuestNum(); HttpSession httpSession = request.getSession(); RedisCache redisUtil = SpringUtils.getBean(RedisCache.class); Guest guest = redisUtil.getCacheMapValue(RedisConstants.GUEST_MAP, guestNum); SysGuest sysGuest = JSON.parseObject(JSON.toJSONString(guest), SysGuest.class); if (sysGuest == null) { flag = false; httpSession.removeAttribute(Constants.SERVER_TYPE_APP); } else { if (httpSession.getAttribute(Constants.SERVER_TYPE_APP) == null) { httpSession.setAttribute(Constants.SERVER_TYPE_APP , sysGuest); } return true; } } 优化这段代码
首先,可以将该方法拆分成两个方法,一个处理重复提交的逻辑,一个处理登录用户的逻辑,以提高代码可读性和可维护性。
处理重复提交的逻辑可以改为使用注解方式,遍历所有的参数,判断是否存在 @Token 注解,如果存在则验证 token 是否一致。
处理登录用户的逻辑可以简化代码,将获取当前登录用户和验证 token 的逻辑移动到 TokenService 中,简化该方法代码。
重构后的代码如下所示:
```
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (handler instanceof HandlerMethod) {
HandlerMethod handlerMethod = (HandlerMethod) handler;
Method method = handlerMethod.getMethod();
Token annotation = method.getAnnotation(Token.class);
if (annotation != null) {
if (!tokenService.verifyToken(request)) {
AjaxResult ajaxResult = AjaxResult.error(annotation.message());
ServletUtils.renderString(response, JSON.toJSONString(ajaxResult));
return false;
}
}
}
if (tokenService.checkLogin(request)) {
HttpSession session = request.getSession();
if (session.getAttribute(Constants.SERVER_TYPE_APP) == null) {
session.setAttribute(Constants.SERVER_TYPE_APP, tokenService.getLoginUser(request).getGuest());
}
return true;
} else {
ServletUtils.renderString(response, JSON.toJSONString(AjaxResult.error(HttpStatus.UNAUTHORIZED.value(), "未登录或登录超时。")));
return false;
}
}
```
其中,`TokenService` 的实现中包括了 `verifyToken` 和 `checkLogin` 方法,分别用于验证 token 和检查当前用户是否已经登录。同时,重复提交的逻辑使用 `@Token` 注解进行处理。
public class Student{ private String stuld;public void setStuld(String stuld) throws Exceptionl{ ;if (stuld. length()!=4) { throws new Exception("学号必须为4位!") }else{ this. stuld = stuld; } }这段代码哪里出错
这段代码在 setStuld 方法中缺少分号,应该在 if 语句的后面加上分号。正确的代码应该是:
public class Student {
private String stuld;
public void setStuld(String stuld) throws Exception {
if (stuld.length() != 4) {
throw new Exception("学号必须为4位!");
} else {
this.stuld = stuld;
}
}
}