From 1e3eeec2d3a470d066d21900586b912dfef58c91 Mon Sep 17 00:00:00 2001 From: zhubaomin <zhubaomin> Date: 星期四, 17 四月 2025 15:07:41 +0800 Subject: [PATCH] 优化代码 --- pipIrr-platform/pipIrr-mw/pipIrr-mwTest-server/src/main/java/com/dy/testServer/console/CommandConsole.java | 91 +++++++++++++++++++++++++++++++++++++++++++++ 1 files changed, 91 insertions(+), 0 deletions(-) diff --git a/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-server/src/main/java/com/dy/testServer/console/CommandConsole.java b/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-server/src/main/java/com/dy/testServer/console/CommandConsole.java new file mode 100644 index 0000000..1236153 --- /dev/null +++ b/pipIrr-platform/pipIrr-mw/pipIrr-mwTest-server/src/main/java/com/dy/testServer/console/CommandConsole.java @@ -0,0 +1,91 @@ +package com.dy.testServer.console; + +import java.io.Console; +import java.io.PrintWriter; + +public class CommandConsole { + + private Console console ; + private PrintWriter prtWrt ; + private static final String myPassword = "123456" ; + private static final String commandPrefix1 = ">>" ; + private static final String commandPrefix2 = "$: " ; + private static String commandPrefix = commandPrefix1 + commandPrefix2 ; + + + public CommandConsole(Console console)throws Exception{ + this.console = console ; + if(this.console == null){ + throw new Exception("涓ラ噸閿欒锛宩ava鎺у埗鍙板璞′负绌猴紒"); + } + prtWrt = console.writer() ; + } + + /** + * 鍒濆锛屾鏌ュ苟杈撳嚭涓�浜涗俊鎭� + * @param args 鍙傛暟 + * @throws Exception 寮傚父 + */ + public void init(String[] args) throws Exception{ + PrintWriter prtWrt = console.writer() ; + if(args != null && args.length > 0){ + String str = "" ; + for(String s : args){ + str += s ; + } + prtWrt.println("info锛氬惎鍔ㄥ弬鏁�" + str); + } + + prtWrt.println("----------------------------------------"); + prtWrt.println("-- --"); + prtWrt.println("-- 娆㈣繋浣跨敤mwTest 鏈嶅姟鎺у埗绔� --"); + prtWrt.println("-- --"); + prtWrt.println("----------------------------------------"); + } + + /** + * 鎵ц鎺у埗鍙版帶鍒� + * @return 杩斿洖 + */ + public boolean clientConsole(){ + boolean exit = false ; + try { + this.login() ; + exit = this.doConsole(); + } catch (Exception e) { + e.printStackTrace(); + } + return exit ; + } + + private boolean login(){ + while (true) { + //String username = console.readLine("Username: "); + char[] password = console.readPassword(">>Password: "); + String passwordStr = String.valueOf(password) ; + if (passwordStr.equals("help")) { + console.printf("瀵嗙爜鏄�%1$s.\n", "123456"); + // 浣跨敤鍚庡簲绔嬪嵆灏嗘暟缁勬竻绌猴紝浠ュ噺灏戝叾鍦ㄥ唴瀛樹腑鍗犵敤鐨勬椂闂达紝澧炲己瀹夊叏鎬� + password = null; + } else if(passwordStr.equals(myPassword)){ + return true; + } + } + } + + private boolean doConsole(){ + boolean exit = false ; + while (!exit) { + String command = console.readLine(commandPrefix); + exit = Command.dealCommand(command, prtWrt) ; + } + return exit ; + } + + public static void changeCommandPrefix(String prefix){ + commandPrefix = commandPrefix1 + prefix ; + } + public static void recoverCommandPrefix(){ + commandPrefix = commandPrefix1 + commandPrefix2 ; + } +} -- Gitblit v1.8.0