Skip to content

Commit 0f5fb4f

Browse files
Merge pull request #81 from HossamSaberr/refactor/replace-magic-strings
refactor: replace magic strings with command constants (fixes #4)
2 parents 2b80892 + 72c88e6 commit 0f5fb4f

File tree

1 file changed

+73
-35
lines changed

1 file changed

+73
-35
lines changed

src/main/java/com/mycmd/App.java

Lines changed: 73 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ public static void main(String[] args) {
4242
}
4343
} else {
4444
// Single, clear not-recognized message + optional suggestion
45-
System.out.println("Unknown command: '" + cmd + "'. Enter 'help' to list all available commands.");
45+
System.out.println("Unknown command: '" + cmd + "'. Enter '" + CommandNames.HELP + "' to list all available commands.");
4646

4747
// compute suggestion safely
4848
try {
@@ -73,40 +73,78 @@ private static String resolveAliases(String input, ShellContext context) {
7373
return input;
7474
}
7575

76+
private static final class CommandNames {
77+
private CommandNames() {}
78+
private static final String ALIAS = "alias";
79+
private static final String CD = "cd";
80+
private static final String CLEARHISTORY = "clearhistory";
81+
private static final String CLS = "cls";
82+
private static final String COLOR = "color";
83+
private static final String COPY = "copy";
84+
private static final String DATE = "date";
85+
private static final String DEL = "del";
86+
private static final String DIR = "dir";
87+
private static final String ECHO = "echo";
88+
private static final String EXIT = "exit";
89+
private static final String HELP = "help";
90+
private static final String HISTORY = "history";
91+
private static final String HOSTNAME = "hostname";
92+
private static final String IPCONFIG = "ipconfig";
93+
private static final String MKDIR = "mkdir";
94+
private static final String PAUSE = "pause";
95+
private static final String PING = "ping";
96+
private static final String PWD = "pwd";
97+
private static final String RENAME = "rename";
98+
private static final String RMDIR = "rmdir";
99+
private static final String SET = "set";
100+
private static final String SYSTEMINFO = "systeminfo";
101+
private static final String TASKLIST = "tasklist";
102+
private static final String TELNET = "telnet";
103+
private static final String TIME = "time";
104+
private static final String TITLE = "title";
105+
private static final String TOUCH = "touch";
106+
private static final String TREE = "tree";
107+
private static final String TYPE = "type";
108+
private static final String UNALIAS = "unalias";
109+
private static final String UPTIME = "uptime";
110+
private static final String VER = "ver";
111+
private static final String WHOAMI = "whoami";
112+
}
113+
76114
private static void registerCommands(Map<String, Command> commands) {
77-
commands.put("dir", new DirCommand());
78-
commands.put("cd", new CdCommand());
79-
commands.put("echo", new EchoCommand());
80-
commands.put("mkdir", new MkdirCommand());
81-
commands.put("rmdir", new RmdirCommand());
82-
commands.put("copy", new CopyCommand());
83-
commands.put("del", new DelCommand());
84-
commands.put("type", new TypeCommand());
85-
commands.put("cls", new ClsCommand());
86-
commands.put("help", new HelpCommand(commands));
87-
commands.put("exit", new ExitCommand());
88-
commands.put("ver", new VersionCommand());
89-
commands.put("title", new TitleCommand());
90-
commands.put("color", new ColorCommand());
91-
commands.put("hostname", new HostnameCommand());
92-
commands.put("whoami", new WhoamiCommand());
93-
commands.put("touch", new TouchCommand());
94-
commands.put("time", new TimeCommand());
95-
commands.put("tasklist", new TasklistCommand());
96-
commands.put("tree", new TreeCommand());
97-
commands.put("date", new DateCommand());
98-
commands.put("history", new HistoryCommand());
99-
commands.put("ping", new PingCommand());
100-
commands.put("telnet", new TelnetCommand());
101-
commands.put("pwd", new PwdCommand());
102-
commands.put("uptime", new UptimeCommand());
103-
commands.put("clearhistory", new ClearHistoryCommand());
104-
commands.put("ipconfig", new IpConfig());
105-
commands.put("alias", new AliasCommand());
106-
commands.put("unalias", new UnaliasCommand());
107-
commands.put("rename", new RenameCommand());
108-
commands.put("set", new SetCommand());
109-
commands.put("systeminfo", new SysteminfoCommand());
110-
commands.put("pause", new PauseCommand());
115+
commands.put(CommandNames.ALIAS, new AliasCommand());
116+
commands.put(CommandNames.CD, new CdCommand());
117+
commands.put(CommandNames.CLEARHISTORY, new ClearHistoryCommand());
118+
commands.put(CommandNames.CLS, new ClsCommand());
119+
commands.put(CommandNames.COLOR, new ColorCommand());
120+
commands.put(CommandNames.COPY, new CopyCommand());
121+
commands.put(CommandNames.DATE, new DateCommand());
122+
commands.put(CommandNames.DEL, new DelCommand());
123+
commands.put(CommandNames.DIR, new DirCommand());
124+
commands.put(CommandNames.ECHO, new EchoCommand());
125+
commands.put(CommandNames.EXIT, new ExitCommand());
126+
commands.put(CommandNames.HELP, new HelpCommand(commands));
127+
commands.put(CommandNames.HISTORY, new HistoryCommand());
128+
commands.put(CommandNames.HOSTNAME, new HostnameCommand());
129+
commands.put(CommandNames.IPCONFIG, new IpConfig());
130+
commands.put(CommandNames.MKDIR, new MkdirCommand());
131+
commands.put(CommandNames.PAUSE, new PauseCommand());
132+
commands.put(CommandNames.PING, new PingCommand());
133+
commands.put(CommandNames.PWD, new PwdCommand());
134+
commands.put(CommandNames.RENAME, new RenameCommand());
135+
commands.put(CommandNames.RMDIR, new RmdirCommand());
136+
commands.put(CommandNames.SET, new SetCommand());
137+
commands.put(CommandNames.SYSTEMINFO, new SysteminfoCommand());
138+
commands.put(CommandNames.TASKLIST, new TasklistCommand());
139+
commands.put(CommandNames.TELNET, new TelnetCommand());
140+
commands.put(CommandNames.TIME, new TimeCommand());
141+
commands.put(CommandNames.TITLE, new TitleCommand());
142+
commands.put(CommandNames.TOUCH, new TouchCommand());
143+
commands.put(CommandNames.TREE, new TreeCommand());
144+
commands.put(CommandNames.TYPE, new TypeCommand());
145+
commands.put(CommandNames.UNALIAS, new UnaliasCommand());
146+
commands.put(CommandNames.UPTIME, new UptimeCommand());
147+
commands.put(CommandNames.VER, new VersionCommand());
148+
commands.put(CommandNames.WHOAMI, new WhoamiCommand());
111149
}
112150
}

0 commit comments

Comments
 (0)