-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCdCommand.java
More file actions
38 lines (31 loc) · 951 Bytes
/
CdCommand.java
File metadata and controls
38 lines (31 loc) · 951 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
import java.io.File;
import java.util.Arrays;
public class CdCommand implements Command {
private File curDirectory;
private String path;
CdCommand(File directory, String path) {
curDirectory = directory;
this.path = path;
}
@Override
public File execute() throws WrongCommand {
if (path.equals("..")) {
return new File(curDirectory.getParent());
} else {
for (String name : Arrays.asList(curDirectory.list())) {
if (path.equals(name) && new File(curDirectory + File.separator + name).isDirectory()) {
return new File(curDirectory.toString() + File.separator + name);
}
}
throw new WrongCommand("No directory named " + path);
}
}
@Override
public File cancel() {
return curDirectory;
}
@Override
public String name() {
return "cd";
}
}