Skip to content
Snippets Groups Projects
Commit 21aeb64c authored by Havard Kolve's avatar Havard Kolve
Browse files

oblig ferdi

parent cf59980f
No related branches found
No related tags found
No related merge requests found
package no.uib.inf101.terminal;
public class CmdCd implements Command {
Context context = new Context();
@Override
public void setContext(Context context){
}
@Override
public String getName(){
return "cd";
}
@Override
public String run(String[] args){
if (args.length == 0) {
this.context.goToHome();
return "";
} else if (args.length > 1) {
return "cd: too many arguments";
}
String path = args[0];
if (this.context.goToPath(path)) {
return "";
} else {
return "cd: no such file or directory: " + path;
}
}
}
package no.uib.inf101.terminal;
import java.io.File;
public class CmdLs implements Command {
Context context = new Context();
@Override
public void setContext(Context context){
}
@Override
public String getName(){
return "ls";
}
@Override
public String run(String[] args){
File cwd = this.context.getCwd();
String s = "";
for (File file : cwd.listFiles()) {
s += file.getName();
s += " ";
}
return s;
}
}
package no.uib.inf101.terminal;
public class CmdPwd implements Command{
Context context = new Context();
@Override
public void setContext(Context context){
}
@Override
public String getName(){
return "pwd";
}
@Override
public String run(String[] args){
return this.context.getCwd().getAbsolutePath();
}
}
......@@ -4,4 +4,6 @@ public interface Command {
String run(String[] args);
String getName();
default void setContext(Context context) { /* do nothing */ };
}
......@@ -33,7 +33,6 @@ public class SimpleShell implements CommandLineInterface {
private final List<String> history = new ArrayList<>();
/** The command currently being typed */
private String currentCommand = "";
// TODO 4: Create instance variable storing Command objects
private final HashMap<String, Command> allCommands = new HashMap<>();
//////////////////////////////////////////////////////////////////////
......@@ -43,7 +42,6 @@ public class SimpleShell implements CommandLineInterface {
/** Constructor for SimpleShell */
public SimpleShell() {
// TODO 7-8-9: Install core commands SimpleShell supports (pwd, ls, cd)
}
/**
......@@ -77,9 +75,9 @@ public class SimpleShell implements CommandLineInterface {
return s;
}
// TODO 5: Add method to install a command
public void installCommand(Command command){
this.allCommands.put(command.getName(), command);
command.setContext(this.context);
}
//////////////////////////////////////////////////////////////////////
......@@ -119,7 +117,10 @@ public class SimpleShell implements CommandLineInterface {
* @return The output of the command
*/
private String executeCommand(String commandName, String[] args) {
// TODO 6: Call run on Command object for given commandName if present
Command command = this.allCommands.get(commandName);
if (command != null) {
return command.run(args);
}
// TODO 7-8-9: Remove if's for cd, ls, pwd once installed as commands
if (Objects.equals(commandName, "pwd")) {
return this.doPwd(args);
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment