You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.3 KiB
50 lines
1.3 KiB
import java.util.ArrayList;
|
|
import java.util.List;
|
|
|
|
public class HistoryCommand implements Command {
|
|
// 集合:用来存储所有历史命令
|
|
private final List<String> commandHistory = new ArrayList<>();
|
|
|
|
/**
|
|
* 实现接口方法:返回命令标识
|
|
*/
|
|
@Override
|
|
public String getName() {
|
|
return "history";
|
|
}
|
|
|
|
/**
|
|
* 执行查看历史命令功能
|
|
*/
|
|
@Override
|
|
public void execute(String[] args, List<Article> articles) {
|
|
// 判断历史记录是否为空
|
|
if (commandHistory.isEmpty()) {
|
|
System.out.println("暂无任何命令历史记录!");
|
|
return;
|
|
}
|
|
|
|
// 遍历并打印所有历史命令
|
|
System.out.println("===== 命令历史记录 =====");
|
|
for (int i = 0; i < commandHistory.size(); i++) {
|
|
// 序号 + 命令内容
|
|
System.out.println((i + 1) + ". " + commandHistory.get(i));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* 对外提供方法:添加一条命令到历史记录
|
|
* @param command 用户输入的命令
|
|
*/
|
|
public void addCommand(String command) {
|
|
commandHistory.add(command);
|
|
}
|
|
|
|
/**
|
|
* 拓展方法:清空历史命令(可选)
|
|
*/
|
|
public void clearHistory() {
|
|
commandHistory.clear();
|
|
System.out.println("命令历史已清空!");
|
|
}
|
|
}
|