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; } } }

相关推荐

优化这段代码: 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; } } //获取网名 也就是/login String servletPath = request.getServletPath(); if(servletPath.contains("/app/card/isCard") || servletPath.contains("/app/bank/signingBankCard") || servletPath.contains("/app/bank/sendMessage") || servletPath.contains("/app/bank/sendSysMessage") || servletPath.contains("/app/bank/login") || servletPath.contains("/app/card/guestIdAndPhone") || servletPath.contains("/app/family/isDevice") || servletPath.contains("/websocket/") || servletPath.contains("/upload/") || servletPath.contains("/app/deviceSocket/toSocket")) { flag = true; } if (!flag) { noLogin(response); } return flag; }

重构这个方法将其认知复杂度从17降低到允许的15: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; } } //获取网名 也就是/login String servletPath = request.getServletPath(); if(servletPath.contains("/app/card/isCard") || servletPath.contains("/app/bank/signingBankCard") || servletPath.contains("/app/bank/sendMessage") || servletPath.contains("/app/bank/sendSysMessage") || servletPath.contains("/app/bank/login") || servletPath.contains("/app/card/guestIdAndPhone") || servletPath.contains("/app/family/isDevice") || servletPath.contains("/websocket/") || servletPath.contains("/upload/") || servletPath.contains("/app/deviceSocket/toSocket")) { flag = true; } if (!flag) { noLogin(response); } return flag; }

请逐句解释一下下面的代码import java.util.Arrays; import org.apache.http.client.fluent.Request; import ece448.iot_sim.SimConfig; import ece448.iot_sim.Main; public class GradeP2 { public static void main(String[] args) throws Exception { SimConfig config = new SimConfig( 8080, Arrays.asList("xxxx", "yyyy", "zzzz.789"), null, null, null); try (Main m = new Main(config)) { Grading.run(new GradeP2(), 10); } } private String get(String pathParams) throws Exception { return Request.Get("http://127.0.0.1:8080"+pathParams) .userAgent("Mozilla/5.0") .connectTimeout(1000) .socketTimeout(1000) .execute().returnContent().asString(); } public boolean testCase00() throws Exception { String ret = get("/xxxx"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1) && (ret.indexOf("Power reading is 0.000") != -1); } public boolean testCase01() throws Exception { String ret = get("/xxxx?action=on"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase02() throws Exception { String ret = get("/xxxx?action=off"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1); } public boolean testCase03() throws Exception { String ret = get("/xxxx?action=on"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase04() throws Exception { String ret = get("/xxxx?action=toggle"); return (ret.indexOf("xxxx is off") != -1) && (ret.indexOf("xxxx is on") == -1); } public boolean testCase05() throws Exception { String ret = get("/xxxx?action=toggle"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase06() throws Exception { String ret = get("/yyyy"); return (ret.indexOf("yyyy is off") != -1) && (ret.indexOf("yyyy is on") == -1); } public boolean testCase07() throws Exception { String ret = get("/xxxx"); return (ret.indexOf("xxxx is on") != -1) && (ret.indexOf("xxxx is off") == -1); } public boolean testCase08() throws Exception { String ret = get("/zzzz.789"); return (ret.indexOf("Power reading is 0.000") != -1); } public boolean testCase09() throws Exception { get("/zzzz.789?action=on"); Thread.sleep(1500); String ret = get("/zzzz.789"); return (ret.indexOf("Power reading is 789.000") != -1); } } private static final Logger logger = LoggerFactory.getLogger(HTTPCommands.class); }

最新推荐

recommend-type

内鬼V4 cat版 scratch版.sb3

这是一个未做好的作品,但还原了绿色周!!!
recommend-type

2024-2030中国mRNA癌症疫苗和治疗市场现状研究分析与发展前景预测报告 Sample.pdf

QYResearch是全球知名的大型咨询公司,行业涵盖各高科技行业产业链细分市场,横跨如半导体产业链(半导体设备及零部件、半导体材料、集成电路、制造、封测、分立器件、传感器、光电器件)、光伏产业链(设备、硅料/硅片、电池片、组件、辅料支架、逆变器、电站终端)、新能源汽车产业链(动力电池及材料、电驱电控、汽车半导体/电子、整车、充电桩)、通信产业链(通信系统设备、终端设备、电子元器件、射频前端、光模块、4G/5G/6G、宽带、IoT、数字经济、AI)、先进材料产业链(金属材料、高分子材料、陶瓷材料、纳米材料等)、机械制造产业链(数控机床、工程机械、电气机械、3C自动化、工业机器人、激光、工控、无人机)、食品药品、医疗器械、农业等。 邮箱:market@qyresearch.com
recommend-type

STC89C51 简单时钟

STC89C51 简单时钟,叫你从基础开始学习单片机,
recommend-type

管理建模和仿真的文件

管理Boualem Benatallah引用此版本:布阿利姆·贝纳塔拉。管理建模和仿真。约瑟夫-傅立叶大学-格勒诺布尔第一大学,1996年。法语。NNT:电话:00345357HAL ID:电话:00345357https://theses.hal.science/tel-003453572008年12月9日提交HAL是一个多学科的开放存取档案馆,用于存放和传播科学研究论文,无论它们是否被公开。论文可以来自法国或国外的教学和研究机构,也可以来自公共或私人研究中心。L’archive ouverte pluridisciplinaire
recommend-type

MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?

![MATLAB归一化方法大比拼:5种方法,哪种最适合你的数据?](https://www.finebi.com/wp-content/uploads/2019/11/FineBI%E8%A1%8C%E4%B8%9A%E9%A9%BE%E9%A9%B6%E8%88%B1-1024x510.png) # 1. MATLAB归一化概述 归一化是一种数据预处理技术,用于将数据缩放到特定范围内,从而消除不同特征之间的尺度差异。在MATLAB中,有各种归一化方法可用于不同类型的数据和应用程序。 归一化的主要目的是: - 提高模型的训练效率和准确性,通过消除特征之间的尺度差异,使模型能够更有效地学习
recommend-type

failed to register layer: ApplyLayer exit status 1 stdout: stderr: archive/tar: invalid tar header

很抱歉听到这个问题。这个错误通常是由于Docker镜像损坏或Docker存储库中缺少所需的图层引起的。您可以尝试以下几种方法来解决这个问题: 1. 清理Docker镜像和容器:使用命令“docker system prune”清理不再使用的Docker对象。 2. 检查Docker存储库:确保您正在使用可靠的Docker存储库,并检查是否存在所有必需的图层。 3. 重新下载Docker镜像:如果您确定问题是由于损坏的Docker镜像引起的,则可以尝试重新下载Docker镜像。 4. 更新Docker版本:如果您使用的是旧版Docker,则可能会出现此问题。尝试更新到最新版本的Docke
recommend-type

Linux系统常用操作命令大全手册

附件是Linux系统常用操作命令大全手册,是 markdown格式,其中覆盖了Linux系统管理、文件操作、网络配置等多个方面,都是日常工作中非常常用的命令,欢迎大家下载学习使用!
recommend-type

"互动学习:行动中的多样性与论文攻读经历"

多样性她- 事实上SCI NCES你的时间表ECOLEDO C Tora SC和NCESPOUR l’Ingén学习互动,互动学习以行动为中心的强化学习学会互动,互动学习,以行动为中心的强化学习计算机科学博士论文于2021年9月28日在Villeneuve d'Asq公开支持马修·瑟林评审团主席法布里斯·勒菲弗尔阿维尼翁大学教授论文指导奥利维尔·皮耶昆谷歌研究教授:智囊团论文联合主任菲利普·普雷教授,大学。里尔/CRISTAL/因里亚报告员奥利维耶·西格德索邦大学报告员卢多维奇·德诺耶教授,Facebook /索邦大学审查员越南圣迈IMT Atlantic高级讲师邀请弗洛里安·斯特鲁布博士,Deepmind对于那些及时看到自己错误的人...3谢谢你首先,我要感谢我的两位博士生导师Olivier和Philippe。奥利维尔,"站在巨人的肩膀上"这句话对你来说完全有意义了。从科学上讲,你知道在这篇论文的(许多)错误中,你是我可以依
recommend-type

MATLAB归一化实战指南:从零到一,轻松提升模型性能

![MATLAB归一化实战指南:从零到一,轻松提升模型性能](https://pic1.zhimg.com/80/v2-fd366800ef0bdf29c804ce25c0276778_1440w.webp) # 1. 归一化的理论基础** 归一化是一种数据预处理技术,旨在将数据缩放到特定范围内,通常是[0, 1]或[-1, 1]。通过归一化,可以消除数据量纲上的差异,使不同特征具有可比性,从而提高机器学习模型的性能。 归一化背后的基本原理是,在训练机器学习模型时,模型的学习过程会受到数据分布的影响。如果数据分布不均匀,某些特征可能具有较大的值域,而其他特征的值域较小。这会导致模型在训练过
recommend-type

File path = new File(ResourceUtils.getURL("classpath:static").getPath());

这段代码的作用是获取项目中静态资源文件夹的路径,其中`ResourceUtils.getURL("classpath:static")`表示获取`classpath`下的`static`文件夹的URL,`getPath()`方法则将URL转换成文件路径。最终得到的`File`对象可以用于读取或写入静态资源文件。需要注意的是,这段代码只适用于Spring Boot项目,因为它使用了Spring的`ResourceUtils`类。如果不是Spring Boot项目,可能需要使用其他方式获取静态资源文件夹的路径。