diff --git a/project/command/ExitCommand.java b/project/command/ExitCommand.java new file mode 100644 index 0000000..3347845 --- /dev/null +++ b/project/command/ExitCommand.java @@ -0,0 +1,19 @@ +package com.crawler.command; + +public class ExitCommand extends BaseCommand { + @Override + public String getName() { + return "exit"; + } + + @Override + public String getDescription() { + return "退出程序"; + } + + @Override + public void execute() { + System.out.println("退出程序..."); + System.exit(0); + } +} \ No newline at end of file diff --git a/project/command/HelpCommand.java b/project/command/HelpCommand.java new file mode 100644 index 0000000..9ee4826 --- /dev/null +++ b/project/command/HelpCommand.java @@ -0,0 +1,30 @@ +package com.crawler.command; + +import java.util.List; + +public class HelpCommand extends BaseCommand { + private List commands; + + public HelpCommand(List commands) { + this.commands = commands; + } + + @Override + public String getName() { + return "help"; + } + + @Override + public String getDescription() { + return "显示所有可用指令"; + } + + @Override + public void execute() { + System.out.println("可用指令:"); + System.out.println("---------"); + for (Command cmd : commands) { + System.out.printf("%-8s : %s%n", cmd.getName(), cmd.getDescription()); + } + } +} \ No newline at end of file diff --git a/project/command/ListCommand.java b/project/command/ListCommand.java new file mode 100644 index 0000000..0ce2331 --- /dev/null +++ b/project/command/ListCommand.java @@ -0,0 +1,34 @@ +package com.crawler.command; + +import java.util.List; + +public class ListCommand extends BaseCommand { + @Override + public String getName() { + return "list"; + } + + @Override + public String getDescription() { + return "查看使用过的指令历史"; + } + + @Override + public void execute() { + List historyList = history.getHistory(); + + System.out.println("========================================"); + System.out.println("指令历史记录:"); + System.out.println("========================================"); + + if (historyList.isEmpty()) { + System.out.println("暂无指令记录"); + } else { + for (int i = 0; i < historyList.size(); i++) { + System.out.printf("[%d] %s%n", i + 1, historyList.get(i)); + } + } + + System.out.println("========================================"); + } +} \ No newline at end of file