-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCommandManager.java
More file actions
83 lines (75 loc) · 3.1 KB
/
CommandManager.java
File metadata and controls
83 lines (75 loc) · 3.1 KB
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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
import java.io.File;
import java.util.*;
import java.util.stream.Collectors;
public class CommandManager {
private LinkedList<Command> HISTORY;
private LinkedList<Command> TRASH;
private File curDirectory;
private CommandManager(String home) throws WrongCommand {
HISTORY = new LinkedList<>();
TRASH = new LinkedList<>();
File homeDirectory = new File(home);
if ((!homeDirectory.exists()) || (homeDirectory.isFile())) {
throw new WrongCommand("Incorrect home directory");
}
curDirectory = homeDirectory;
}
private Command getCommand(String command) throws WrongCommand {
List<String> parseCommand = Arrays.stream(command.split(" "))
.filter((w) -> w.length() > 0)
.collect(Collectors.toList());
switch (parseCommand.get(0)) {
case "cd":
return new CdCommand(curDirectory, parseCommand.get(1));
case "cp":
return new CpCommand(curDirectory, parseCommand.get(1), parseCommand.get(2));
case "mv":
return new MvCommand(curDirectory, parseCommand.get(1), parseCommand.get(2));
case "mkdir":
return new MkdirCommand(curDirectory, parseCommand.get(1));
case "redo":
return new RedoCommand(curDirectory, TRASH.removeLast(), HISTORY);
case "undo":
return new UndoCommand(curDirectory, HISTORY.removeLast(), TRASH);
case "history":
return new HistoryCommand(curDirectory, HISTORY);
case "exit":
return new ExitCommand(curDirectory);
default:
throw new WrongCommand("Wrong command");
}
}
private void doSession() throws SessionClosed, WrongCommand {
Scanner scanner = new Scanner(System.in);
System.out.println(curDirectory + "$");
while (true) {
String curCommandName = scanner.nextLine();
try {
Command curCommand = getCommand(curCommandName);
curDirectory = curCommand.execute();
if (!curCommand.getClass().equals(RedoCommand.class) &&
!curCommand.getClass().equals(UndoCommand.class) &&
!curCommand.getClass().equals(HistoryCommand.class) &&
!curCommand.getClass().equals(ExitCommand.class)) {
HISTORY.add(curCommand);
}
System.out.println(curDirectory);
} catch (WrongCommand e) {
System.out.println(e.getMessage());
System.out.println(curDirectory);
} catch (SessionClosed e) {
System.out.println(e.getMessage());
break;
}
}
}
public static void main(String... args) {
CommandManager manager;
try {
manager = new CommandManager(System.getProperty("user.dir"));
manager.doSession();
} catch (WrongCommand | SessionClosed e) {
System.out.println(e.getMessage());
}
}
}