From e5bf631975449de464b038f17ab39658bdfd1be4 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 05:13:30 +0300 Subject: [PATCH 1/6] refactor: replace magic strings with command constants (fixes #4) --- src/main/java/com/mycmd/App.java | 102 +++++++++++++++++++++---------- 1 file changed, 69 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 2abfd95..7ab4fea 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -73,40 +73,76 @@ private static String resolveAliases(String input, ShellContext context) { return input; } + public final class CommandNames { + private CommandNames() {} + public static final String DIR = "dir"; + public static final String CD = "cd"; + public static final String ECHO = "echo"; + public static final String MKDIR = "mkdir"; + public static final String RMDIR = "rmdir"; + public static final String COPY = "copy"; + public static final String DEL = "del"; + public static final String TYPE = "type"; + public static final String CLS = "cls"; + public static final String HELP = "help"; + public static final String EXIT = "exit"; + public static final String VER = "ver"; + public static final String TITLE = "title"; + public static final String COLOR = "color"; + public static final String HOSTNAME = "hostname"; + public static final String WHOAMI = "whoami"; + public static final String TOUCH = "touch"; + public static final String TIME = "time"; + public static final String TASKLIST = "tasklist"; + public static final String TREE = "tree"; + public static final String DATE = "date"; + public static final String HISTORY = "history"; + public static final String PING = "ping"; + public static final String TELNET = "telnet"; + public static final String PWD = "pwd"; + public static final String UPTIME = "uptime"; + public static final String CLEARHISTORY = "clearhistory"; + public static final String IPCONFIG = "ipconfig"; + public static final String ALIAS = "alias"; + public static final String UNALIAS = "unalias"; + public static final String RENAME = "rename"; + public static final String SET = "set"; + } private static void registerCommands(Map commands) { - commands.put("dir", new DirCommand()); - commands.put("cd", new CdCommand()); - commands.put("echo", new EchoCommand()); - commands.put("mkdir", new MkdirCommand()); - commands.put("rmdir", new RmdirCommand()); - commands.put("copy", new CopyCommand()); - commands.put("del", new DelCommand()); - commands.put("type", new TypeCommand()); - commands.put("cls", new ClsCommand()); - commands.put("help", new HelpCommand(commands)); - commands.put("exit", new ExitCommand()); - commands.put("ver", new VersionCommand()); - commands.put("title", new TitleCommand()); - commands.put("color", new ColorCommand()); - commands.put("hostname", new HostnameCommand()); - commands.put("whoami", new WhoamiCommand()); - commands.put("touch", new TouchCommand()); - commands.put("time", new TimeCommand()); - commands.put("tasklist", new TasklistCommand()); - commands.put("tree", new TreeCommand()); - commands.put("date", new DateCommand()); - commands.put("history", new HistoryCommand()); - commands.put("ping", new PingCommand()); - commands.put("telnet", new TelnetCommand()); - commands.put("pwd", new PwdCommand()); - commands.put("uptime", new UptimeCommand()); - commands.put("clearhistory", new ClearHistoryCommand()); - commands.put("ipconfig", new IpConfig()); - commands.put("alias", new AliasCommand()); - commands.put("unalias", new UnaliasCommand()); - commands.put("rename", new RenameCommand()); - commands.put("set", new SetCommand()); - } + commands.put(CommandNames.DIR, new DirCommand()); + commands.put(CommandNames.CD, new CdCommand()); + commands.put(CommandNames.ECHO, new EchoCommand()); + commands.put(CommandNames.MKDIR, new MkdirCommand()); + commands.put(CommandNames.RMDIR, new RmdirCommand()); + commands.put(CommandNames.COPY, new CopyCommand()); + commands.put(CommandNames.DEL, new DelCommand()); + commands.put(CommandNames.TYPE, new TypeCommand()); + commands.put(CommandNames.CLS, new ClsCommand()); + commands.put(CommandNames.HELP, new HelpCommand(commands)); + commands.put(CommandNames.EXIT, new ExitCommand()); + commands.put(CommandNames.VER, new VersionCommand()); + commands.put(CommandNames.TITLE, new TitleCommand()); + commands.put(CommandNames.COLOR, new ColorCommand()); + commands.put(CommandNames.HOSTNAME, new HostnameCommand()); + commands.put(CommandNames.WHOAMI, new WhoamiCommand()); + commands.put(CommandNames.TOUCH, new TouchCommand()); + commands.put(CommandNames.TIME, new TimeCommand()); + commands.put(CommandNames.TASKLIST, new TasklistCommand()); + commands.put(CommandNames.TREE, new TreeCommand()); + commands.put(CommandNames.DATE, new DateCommand()); + commands.put(CommandNames.HISTORY, new HistoryCommand()); + commands.put(CommandNames.PING, new PingCommand()); + commands.put(CommandNames.TELNET, new TelnetCommand()); + commands.put(CommandNames.PWD, new PwdCommand()); + commands.put(CommandNames.UPTIME, new UptimeCommand()); + commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand()); + commands.put(CommandNames.IPCONFIG, new IpConfig()); + commands.put(CommandNames.ALIAS, new AliasCommand()); + commands.put(CommandNames.UNALIAS, new UnaliasCommand()); + commands.put(CommandNames.RENAME, new RenameCommand()); + commands.put(CommandNames.SET, new SetCommand()); + } + } From 266f1b933a260a26bf3df92239cea13189013864 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 05:30:29 +0300 Subject: [PATCH 2/6] Fix small bugs --- src/main/java/com/mycmd/App.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 7ab4fea..9e308fa 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -42,7 +42,7 @@ public static void main(String[] args) { } } else { // Single, clear not-recognized message + optional suggestion - System.out.println("Unknown command: '" + cmd + "'. Enter 'help' to list all available commands."); + System.out.println("Unknown command: '" + cmd + "'. Enter '" + CommandNames.HELP + "' to list all available commands."); // compute suggestion safely try { @@ -73,8 +73,8 @@ private static String resolveAliases(String input, ShellContext context) { return input; } - public final class CommandNames { - private CommandNames() {} + private static final class CommandNames { + private CommandNames() {} public static final String DIR = "dir"; public static final String CD = "cd"; public static final String ECHO = "echo"; From 70d4e70cde2d40e6ae42eb7fc9a3c73b9f245d18 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 05:35:10 +0300 Subject: [PATCH 3/6] Make the commandNames private --- src/main/java/com/mycmd/App.java | 64 ++++++++++++++++---------------- 1 file changed, 32 insertions(+), 32 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 9e308fa..9f933fc 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -75,38 +75,38 @@ private static String resolveAliases(String input, ShellContext context) { private static final class CommandNames { private CommandNames() {} - public static final String DIR = "dir"; - public static final String CD = "cd"; - public static final String ECHO = "echo"; - public static final String MKDIR = "mkdir"; - public static final String RMDIR = "rmdir"; - public static final String COPY = "copy"; - public static final String DEL = "del"; - public static final String TYPE = "type"; - public static final String CLS = "cls"; - public static final String HELP = "help"; - public static final String EXIT = "exit"; - public static final String VER = "ver"; - public static final String TITLE = "title"; - public static final String COLOR = "color"; - public static final String HOSTNAME = "hostname"; - public static final String WHOAMI = "whoami"; - public static final String TOUCH = "touch"; - public static final String TIME = "time"; - public static final String TASKLIST = "tasklist"; - public static final String TREE = "tree"; - public static final String DATE = "date"; - public static final String HISTORY = "history"; - public static final String PING = "ping"; - public static final String TELNET = "telnet"; - public static final String PWD = "pwd"; - public static final String UPTIME = "uptime"; - public static final String CLEARHISTORY = "clearhistory"; - public static final String IPCONFIG = "ipconfig"; - public static final String ALIAS = "alias"; - public static final String UNALIAS = "unalias"; - public static final String RENAME = "rename"; - public static final String SET = "set"; + private static final String DIR = "dir"; + private static final String CD = "cd"; + private static final String ECHO = "echo"; + private static final String MKDIR = "mkdir"; + private static final String RMDIR = "rmdir"; + private static final String COPY = "copy"; + private static final String DEL = "del"; + private static final String TYPE = "type"; + private static final String CLS = "cls"; + private static final String HELP = "help"; + private static final String EXIT = "exit"; + private static final String VER = "ver"; + private static final String TITLE = "title"; + private static final String COLOR = "color"; + private static final String HOSTNAME = "hostname"; + private static final String WHOAMI = "whoami"; + private static final String TOUCH = "touch"; + private static final String TIME = "time"; + private static final String TASKLIST = "tasklist"; + private static final String TREE = "tree"; + private static final String DATE = "date"; + private static final String HISTORY = "history"; + private static final String PING = "ping"; + private static final String TELNET = "telnet"; + private static final String PWD = "pwd"; + private static final String UPTIME = "uptime"; + private static final String CLEARHISTORY = "clearhistory"; + private static final String IPCONFIG = "ipconfig"; + private static final String ALIAS = "alias"; + private static final String UNALIAS = "unalias"; + private static final String RENAME = "rename"; + private static final String SET = "set"; } private static void registerCommands(Map commands) { commands.put(CommandNames.DIR, new DirCommand()); From 172da5ed127732db45d29172f7a55ff0b2210f38 Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 16:31:41 +0300 Subject: [PATCH 4/6] fix conflict --- src/main/java/com/mycmd/App.java | 72 ++++++++++++++++---------------- 1 file changed, 37 insertions(+), 35 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 9f933fc..8f6c5ce 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -75,39 +75,42 @@ private static String resolveAliases(String input, ShellContext context) { private static final class CommandNames { private CommandNames() {} - private static final String DIR = "dir"; - private static final String CD = "cd"; - private static final String ECHO = "echo"; - private static final String MKDIR = "mkdir"; - private static final String RMDIR = "rmdir"; - private static final String COPY = "copy"; - private static final String DEL = "del"; - private static final String TYPE = "type"; - private static final String CLS = "cls"; - private static final String HELP = "help"; - private static final String EXIT = "exit"; - private static final String VER = "ver"; - private static final String TITLE = "title"; - private static final String COLOR = "color"; - private static final String HOSTNAME = "hostname"; - private static final String WHOAMI = "whoami"; - private static final String TOUCH = "touch"; - private static final String TIME = "time"; - private static final String TASKLIST = "tasklist"; - private static final String TREE = "tree"; - private static final String DATE = "date"; - private static final String HISTORY = "history"; - private static final String PING = "ping"; - private static final String TELNET = "telnet"; - private static final String PWD = "pwd"; - private static final String UPTIME = "uptime"; + private static final String DIR = "dir"; + private static final String CD = "cd"; + private static final String ECHO = "echo"; + private static final String MKDIR = "mkdir"; + private static final String RMDIR = "rmdir"; + private static final String COPY = "copy"; + private static final String DEL = "del"; + private static final String TYPE = "type"; + private static final String CLS = "cls"; + private static final String HELP = "help"; + private static final String EXIT = "exit"; + private static final String VER = "ver"; + private static final String TITLE = "title"; + private static final String COLOR = "color"; + private static final String HOSTNAME = "hostname"; + private static final String WHOAMI = "whoami"; + private static final String TOUCH = "touch"; + private static final String TIME = "time"; + private static final String TASKLIST = "tasklist"; + private static final String TREE = "tree"; + private static final String DATE = "date"; + private static final String HISTORY = "history"; + private static final String PING = "ping"; + private static final String TELNET = "telnet"; + private static final String PWD = "pwd"; + private static final String UPTIME = "uptime"; private static final String CLEARHISTORY = "clearhistory"; - private static final String IPCONFIG = "ipconfig"; - private static final String ALIAS = "alias"; - private static final String UNALIAS = "unalias"; - private static final String RENAME = "rename"; - private static final String SET = "set"; + private static final String IPCONFIG = "ipconfig"; + private static final String ALIAS = "alias"; + private static final String UNALIAS = "unalias"; + private static final String RENAME = "rename"; + private static final String SET = "set"; + private static final String SYSTEMINFO = "systeminfo"; + private static final String PAUSE = "pause"; } + private static void registerCommands(Map commands) { commands.put(CommandNames.DIR, new DirCommand()); commands.put(CommandNames.CD, new CdCommand()); @@ -141,8 +144,7 @@ private static void registerCommands(Map commands) { commands.put(CommandNames.UNALIAS, new UnaliasCommand()); commands.put(CommandNames.RENAME, new RenameCommand()); commands.put(CommandNames.SET, new SetCommand()); + commands.put(CommandNames.SYSTEMINFO, new SysteminfoCommand()); + commands.put(CommandNames.PAUSE, new PauseCommand()); } - -} - - +} \ No newline at end of file From 7231c63a7ea30ecfab86e4040e91967dd6144a5e Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 16:45:40 +0300 Subject: [PATCH 5/6] Consider alphabetical ordering for easier navigation --- src/main/java/com/mycmd/App.java | 96 ++++++++++++++++---------------- 1 file changed, 48 insertions(+), 48 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 8260461..374636d 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -75,76 +75,76 @@ private static String resolveAliases(String input, ShellContext context) { private static final class CommandNames { private CommandNames() {} - private static final String DIR = "dir"; + private static final String ALIAS = "alias"; private static final String CD = "cd"; - private static final String ECHO = "echo"; - private static final String MKDIR = "mkdir"; - private static final String RMDIR = "rmdir"; - private static final String COPY = "copy"; - private static final String DEL = "del"; - private static final String TYPE = "type"; + private static final String CLEARHISTORY = "clearhistory"; private static final String CLS = "cls"; - private static final String HELP = "help"; - private static final String EXIT = "exit"; - private static final String VER = "ver"; - private static final String TITLE = "title"; private static final String COLOR = "color"; - private static final String HOSTNAME = "hostname"; - private static final String WHOAMI = "whoami"; - private static final String TOUCH = "touch"; - private static final String TIME = "time"; - private static final String TASKLIST = "tasklist"; - private static final String TREE = "tree"; + private static final String COPY = "copy"; private static final String DATE = "date"; + private static final String DEL = "del"; + private static final String DIR = "dir"; + private static final String ECHO = "echo"; + private static final String EXIT = "exit"; + private static final String HELP = "help"; private static final String HISTORY = "history"; + private static final String HOSTNAME = "hostname"; + private static final String IPCONFIG = "ipconfig"; + private static final String MKDIR = "mkdir"; + private static final String PAUSE = "pause"; private static final String PING = "ping"; - private static final String TELNET = "telnet"; private static final String PWD = "pwd"; - private static final String UPTIME = "uptime"; - private static final String CLEARHISTORY = "clearhistory"; - private static final String IPCONFIG = "ipconfig"; - private static final String ALIAS = "alias"; - private static final String UNALIAS = "unalias"; private static final String RENAME = "rename"; + private static final String RMDIR = "rmdir"; private static final String SET = "set"; private static final String SYSTEMINFO = "systeminfo"; - private static final String PAUSE = "pause"; + private static final String TASKLIST = "tasklist"; + private static final String TELNET = "telnet"; + private static final String TIME = "time"; + private static final String TITLE = "title"; + private static final String TOUCH = "touch"; + private static final String TREE = "tree"; + private static final String TYPE = "type"; + private static final String UNALIAS = "unalias"; + private static final String UPTIME = "uptime"; + private static final String VER = "ver"; + private static final String WHOAMI = "whoami"; } private static void registerCommands(Map commands) { - commands.put(CommandNames.DIR, new DirCommand()); + commands.put(CommandNames.ALIAS, new AliasCommand()); commands.put(CommandNames.CD, new CdCommand()); - commands.put(CommandNames.ECHO, new EchoCommand()); - commands.put(CommandNames.MKDIR, new MkdirCommand()); - commands.put(CommandNames.RMDIR, new RmdirCommand()); - commands.put(CommandNames.COPY, new CopyCommand()); - commands.put(CommandNames.DEL, new DelCommand()); - commands.put(CommandNames.TYPE, new TypeCommand()); + commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand()); commands.put(CommandNames.CLS, new ClsCommand()); - commands.put(CommandNames.HELP, new HelpCommand(commands)); - commands.put(CommandNames.EXIT, new ExitCommand()); - commands.put(CommandNames.VER, new VersionCommand()); - commands.put(CommandNames.TITLE, new TitleCommand()); commands.put(CommandNames.COLOR, new ColorCommand()); - commands.put(CommandNames.HOSTNAME, new HostnameCommand()); - commands.put(CommandNames.WHOAMI, new WhoamiCommand()); - commands.put(CommandNames.TOUCH, new TouchCommand()); - commands.put(CommandNames.TIME, new TimeCommand()); - commands.put(CommandNames.TASKLIST, new TasklistCommand()); - commands.put(CommandNames.TREE, new TreeCommand()); + commands.put(CommandNames.COPY, new CopyCommand()); commands.put(CommandNames.DATE, new DateCommand()); + commands.put(CommandNames.DEL, new DelCommand()); + commands.put(CommandNames.DIR, new DirCommand()); + commands.put(CommandNames.ECHO, new EchoCommand()); + commands.put(CommandNames.EXIT, new ExitCommand()); + commands.put(CommandNames.HELP, new HelpCommand(commands)); commands.put(CommandNames.HISTORY, new HistoryCommand()); + commands.put(CommandNames.HOSTNAME, new HostnameCommand()); + commands.put(CommandNames.IPCONFIG, new IpConfig()); + commands.put(CommandNames.MKDIR, new MkdirCommand()); + commands.put(CommandNames.PAUSE, new PauseCommand()); commands.put(CommandNames.PING, new PingCommand()); - commands.put(CommandNames.TELNET, new TelnetCommand()); commands.put(CommandNames.PWD, new PwdCommand()); - commands.put(CommandNames.UPTIME, new UptimeCommand()); - commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand()); - commands.put(CommandNames.IPCONFIG, new IpConfig()); - commands.put(CommandNames.ALIAS, new AliasCommand()); - commands.put(CommandNames.UNALIAS, new UnaliasCommand()); commands.put(CommandNames.RENAME, new RenameCommand()); + commands.put(CommandNames.RMDIR, new RmdirCommand()); commands.put(CommandNames.SET, new SetCommand()); commands.put(CommandNames.SYSTEMINFO, new SysteminfoCommand()); - commands.put(CommandNames.PAUSE, new PauseCommand()); + commands.put(CommandNames.TASKLIST, new TasklistCommand()); + commands.put(CommandNames.TELNET, new TelnetCommand()); + commands.put(CommandNames.TIME, new TimeCommand()); + commands.put(CommandNames.TITLE, new TitleCommand()); + commands.put(CommandNames.TOUCH, new TouchCommand()); + commands.put(CommandNames.TREE, new TreeCommand()); + commands.put(CommandNames.TYPE, new TypeCommand()); + commands.put(CommandNames.UNALIAS, new UnaliasCommand()); + commands.put(CommandNames.UPTIME, new UptimeCommand()); + commands.put(CommandNames.VER, new VersionCommand()); + commands.put(CommandNames.WHOAMI, new WhoamiCommand()); } } From 72c88e635585ced86e8c5cb189180dd2f6d21fce Mon Sep 17 00:00:00 2001 From: HossamSaberr Date: Tue, 28 Oct 2025 16:54:49 +0300 Subject: [PATCH 6/6] remove extra spacing --- src/main/java/com/mycmd/App.java | 66 ++++++++++++++++---------------- 1 file changed, 33 insertions(+), 33 deletions(-) diff --git a/src/main/java/com/mycmd/App.java b/src/main/java/com/mycmd/App.java index 374636d..a3a0e02 100644 --- a/src/main/java/com/mycmd/App.java +++ b/src/main/java/com/mycmd/App.java @@ -75,40 +75,40 @@ private static String resolveAliases(String input, ShellContext context) { private static final class CommandNames { private CommandNames() {} - private static final String ALIAS = "alias"; - private static final String CD = "cd"; + private static final String ALIAS = "alias"; + private static final String CD = "cd"; private static final String CLEARHISTORY = "clearhistory"; - private static final String CLS = "cls"; - private static final String COLOR = "color"; - private static final String COPY = "copy"; - private static final String DATE = "date"; - private static final String DEL = "del"; - private static final String DIR = "dir"; - private static final String ECHO = "echo"; - private static final String EXIT = "exit"; - private static final String HELP = "help"; - private static final String HISTORY = "history"; - private static final String HOSTNAME = "hostname"; - private static final String IPCONFIG = "ipconfig"; - private static final String MKDIR = "mkdir"; - private static final String PAUSE = "pause"; - private static final String PING = "ping"; - private static final String PWD = "pwd"; - private static final String RENAME = "rename"; - private static final String RMDIR = "rmdir"; - private static final String SET = "set"; - private static final String SYSTEMINFO = "systeminfo"; - private static final String TASKLIST = "tasklist"; - private static final String TELNET = "telnet"; - private static final String TIME = "time"; - private static final String TITLE = "title"; - private static final String TOUCH = "touch"; - private static final String TREE = "tree"; - private static final String TYPE = "type"; - private static final String UNALIAS = "unalias"; - private static final String UPTIME = "uptime"; - private static final String VER = "ver"; - private static final String WHOAMI = "whoami"; + private static final String CLS = "cls"; + private static final String COLOR = "color"; + private static final String COPY = "copy"; + private static final String DATE = "date"; + private static final String DEL = "del"; + private static final String DIR = "dir"; + private static final String ECHO = "echo"; + private static final String EXIT = "exit"; + private static final String HELP = "help"; + private static final String HISTORY = "history"; + private static final String HOSTNAME = "hostname"; + private static final String IPCONFIG = "ipconfig"; + private static final String MKDIR = "mkdir"; + private static final String PAUSE = "pause"; + private static final String PING = "ping"; + private static final String PWD = "pwd"; + private static final String RENAME = "rename"; + private static final String RMDIR = "rmdir"; + private static final String SET = "set"; + private static final String SYSTEMINFO = "systeminfo"; + private static final String TASKLIST = "tasklist"; + private static final String TELNET = "telnet"; + private static final String TIME = "time"; + private static final String TITLE = "title"; + private static final String TOUCH = "touch"; + private static final String TREE = "tree"; + private static final String TYPE = "type"; + private static final String UNALIAS = "unalias"; + private static final String UPTIME = "uptime"; + private static final String VER = "ver"; + private static final String WHOAMI = "whoami"; } private static void registerCommands(Map commands) {