-
Notifications
You must be signed in to change notification settings - Fork 0
/
CommandHistory.java
36 lines (31 loc) · 948 Bytes
/
CommandHistory.java
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
import java.util.ArrayList;
import java.util.List;
public class CommandHistory {
private List<String> history = new ArrayList<>();
private int history_index = -1;
public void add_command(String command) {
history.add(command);
history_index = history.size();
}
public String get_history() {
StringBuilder sb = new StringBuilder();
for (int i = 0; i < history.size(); i++) {
sb.append(i + 1).append(": ").append(history.get(i)).append("\n");
}
return sb.toString();
}
public String get_previous_command() {
if (history_index > 0) {
history_index--;
return history.get(history_index);
}
return null;
}
public String get_next_command() {
if (history_index < history.size() - 1) {
history_index++;
return history.get(history_index);
}
return null;
}
}