package Article; import java.util.ArrayList; import java.util.List; public class HistoryCommand { private List commandHistory; public HistoryCommand() { this.commandHistory = new ArrayList<>(); } public void addCommand(String command) { if (command != null && !command.trim().isEmpty()) { commandHistory.add(command); } } public List getAllCommands() { return new ArrayList<>(commandHistory); // 返回副本,保护内部数据 } public String getLastCommand() { return commandHistory.isEmpty() ? null : commandHistory.get(commandHistory.size() - 1); } public void printHistory() { for (int i = 0; i < commandHistory.size(); i++) { System.out.println((i + 1) + ". " + commandHistory.get(i)); } } public void clear() { commandHistory.clear(); } public int size() { return commandHistory.size(); } }