Skip to content
Snippets Groups Projects
Commit 8293c0f5 authored by W.B.Gravendeel's avatar W.B.Gravendeel
Browse files

Part 6 try 3

parent 56d04380
No related branches found
No related tags found
No related merge requests found
......@@ -8,7 +8,7 @@ public class CmdEcho implements Command{
}
@Override
public String run(String[] args, Context context){
public String run(String[] args){
String tempOutput = "";
for (String arg : args)
tempOutput = tempOutput + arg + " ";
......
......@@ -10,9 +10,9 @@ public class Main {
//CommandLineInterface shell = new SimpleShell();
SimpleShell shell = new SimpleShell();
shell.installCommand(new CmdEcho());
shell.installCommand(new ls());
shell.installCommand(new cd());
shell.installCommand(new pwd());
//shell.installCommand(new ls());
//shell.installCommand(new cd());
//shell.installCommand(new pwd());
// Run the shell in the terminal GUI
......
......@@ -13,11 +13,11 @@ package no.uib.inf101.terminal;
// - kan vises av Terminal.java
// - kan støtte ubegrenset mange kommandoer
//import java.io.File;
import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
//import java.util.Objects;
import java.util.Objects;
// TODO 1: Let SimpleShell implement CommandLineInterface
public class SimpleShell implements CommandLineInterface{
......@@ -44,7 +44,67 @@ public class SimpleShell implements CommandLineInterface{
/** Constructor for SimpleShell */
public SimpleShell() {
// TODO 7-8-9: Install core commands SimpleShell supports (pwd, ls, cd)
this.installCommand(new pwd());
this.installCommand(new ls());
this.installCommand(new cd());
}
public class pwd implements Command{
@Override
public String getName(){
return "pwd";
}
@Override
public String run(String[] args){
return context.getCwd().getAbsolutePath();
}
}
public class cd implements Command{
@Override
public String getName(){
return "cd";
}
@Override
public String run(String[] args){
if (args.length == 0) {
context.goToHome();
return "";
} else if (args.length > 1) {
return "cd: too many arguments";
}
String path = args[0];
if (context.goToPath(path)) {
return "";
} else {
return "cd: no such file or directory: " + path;
}
}
}
public class ls implements Command{
@Override
public String getName(){
return "ls";
}
@Override
public String run(String[] args){
File cwd = context.getCwd();
String s = "";
for (File file : cwd.listFiles()) {
s += file.getName();
s += " ";
}
return s;
}
}
// TODO 2: rename method to fit new interface, annotate with @Override
// Note: methods with @Override generally do not need javadoc comments,
......@@ -134,7 +194,7 @@ public class SimpleShell implements CommandLineInterface{
Command command = this.allCommands.get(commandName);
if (command != null) {
return command.run(args, this.context);
return command.run(args);
}
//else if (/* ... */) {
// ...
......@@ -143,50 +203,5 @@ public class SimpleShell implements CommandLineInterface{
return "Command not found: \"" + commandName + "\"";
}
}
/*
// TODO 7-8-9: Remove if's for cd, ls, pwd once installed as commands
if (Objects.equals(commandName, "pwd")) {
return this.doPwd(args);
} else if (Objects.equals(commandName, "cd")) {
return this.doCd(args);
} else if (Objects.equals(commandName, "ls")) {
return this.doLs(args);
} else {
return "Command not found: \"" + commandName + "\"";
}
}
// TODO 7: remove this method and replace it with Command -type object
private String doPwd(String[] args) {
return this.context.getCwd().getAbsolutePath();
}
// TODO 8: remove this method and replace it with Command -type object
private String doCd(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;
}
}
// TODO 9: remove this method and replace it with Command -type object
private String doLs(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 cd implements Command{
@Override
public String getName(){
......@@ -23,3 +23,4 @@ public class cd implements Command{
}
}
}
*/
\ No newline at end of file
package no.uib.inf101.terminal;
/*
import java.io.File;
public class ls implements Command{
@Override
public String getName(){
......@@ -19,3 +19,4 @@ public class ls implements Command{
return s;
}
}
*/
\ No newline at end of file
package no.uib.inf101.terminal;
/*
public class pwd implements Command{
@Override
public String getName(){
......@@ -10,4 +10,5 @@ public class pwd implements Command{
public String run(String[] args, Context context){
return context.getCwd().getAbsolutePath();
}
}
\ No newline at end of file
}
*/
\ No newline at end of file
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