From 1781aded8053b540c5354b45730aba06930f2f95 Mon Sep 17 00:00:00 2001 From: HuangZhikai <386754646@qq.com> Date: Sun, 31 May 2026 14:46:50 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'project/command'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- project/command/ExitCommand.java | 19 ++++++++++++++++++ project/command/HelpCommand.java | 30 ++++++++++++++++++++++++++++ project/command/ListCommand.java | 34 ++++++++++++++++++++++++++++++++ 3 files changed, 83 insertions(+) create mode 100644 project/command/ExitCommand.java create mode 100644 project/command/HelpCommand.java create mode 100644 project/command/ListCommand.java 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