So I've got this assignment to write a shell simulating app. And it all went well. I've been refactoring it like crazy, after new demands keeps showing up and now I am stuck. I've got ridden of switch statements with the usage of command pattern but now after another demand I just can't find a solution. Below is the code for the ConsoleCommand class which holds all the logic for reading commands and executing them.
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class ConsoleController extends Directory{
private CommandReader cr;
private Prompt p;
private Tree tree;
private Dir dir;
private String order[];
private Map <String,Command> commandsMap;
private Exit exit;
private PromptReset reset;
private PromptCwd cwd;
private ChangeToParent cTP;
private ChangeDir cD;
public ConsoleController() {
cr=new CommandReader();
p=new Prompt();
cD = new ChangeDir();
tree = new Tree();
dir=new Dir();
exit = new Exit();
cwd = new PromptCwd();
reset = new PromptReset();
cTP = new ChangeToParent();
commandsMap = new HashMap<String,Command>(){{
put("dir",dir);
put("tree",tree);
put("exit",exit);
put("reset",reset);
put("$cwd",cwd);
put("..",cTP);
}};
}
public void start() {
while(true){
prompt();
order = cr.commandRead().split(" ");
if (!order[0].equals("prompt")&&!order[0].equals("cd"))
{
if(commandsMap.get(order[0])!=null){
commandsMap.get(order[0]).execute;
}
else
System.out.println(order[0] + " :unknown command");
}
if(order[0].equals("prompt")&&order.length>1){
if(commandsMap.get(order[1])!=null){
commandsMap.get(order[1]).execute;
}
else
promptChange(order[1]);
}
else if(order[0].equals("cd")&&order.length>1){
if(commandsMap.get(order[1])!=null){
commandsMap.get(order[1]).execute;
cwd.execute();}
else{
changeDir(order[1]);
}
}
}
}
public void prompt(){
p.printPrompt();
}
public void promptChange(String change){
p.change(change);
}
public void changeDir(String s){
File folder = new File(getDir());
File[] fileList = folder.listFiles();
List<String> folderList = new ArrayList<String>();
for(File f:fileList){
if(f.isDirectory()){
folderList.add(f.getName().toString());
}
}
if(folderList.contains(s)){
cD.setSt(s);
cD.execute();
cwd.execute();
}
else
System.out.println("No such directory");
}
}
So I've head "It's cool, without switches, but can you get rid of "if" like in "order[0].equals("prompt")". And so I've tried to add more values to the Hashmap and basically it's ok the only problem I have is with "prompt" and "cd" commands. When they are set and act the same like "prompt reset"(resets to default prompt) or "cd .." (move to parent directory) it's all nice, but the issue I have is when I need to pass "prompt SomeString" which will change the default prompt to "SomeString>" or passing "cd SomeDirectory" which will change the current directory to the one mentioned in the command. Don't ask me "Why you want to change it" or "It's stupid" just let me know what are the options to further refactor my code.
OK so I was ask to make clear what is the question. The question is - Can I get rid of the "if" statements like the one used below
if(order[0].equals("prompt")&&order.length>1)
While still having the possibility to use commands with parameters I can't predict like "prompt lalala" or "cd MyStuff"
Thanks!
Aucun commentaire:
Enregistrer un commentaire