From 36159d93b26bc2a1a61a10a741ac609b4f435e76 Mon Sep 17 00:00:00 2001 From: LiuZihan <1353843969@qq.com> Date: Sun, 24 May 2026 17:43:55 +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/HelpCommand.java | 33 ++++++++++++++++++++++++++++ project/command/ListCommand.java | 29 +++++++++++++++++++++++++ project/command/SearchCommand.java | 35 ++++++++++++++++++++++++++++++ project/command/StatCommand.java | 33 ++++++++++++++++++++++++++++ 4 files changed, 130 insertions(+) create mode 100644 project/command/HelpCommand.java create mode 100644 project/command/ListCommand.java create mode 100644 project/command/SearchCommand.java create mode 100644 project/command/StatCommand.java diff --git a/project/command/HelpCommand.java b/project/command/HelpCommand.java new file mode 100644 index 0000000..f6a638a --- /dev/null +++ b/project/command/HelpCommand.java @@ -0,0 +1,33 @@ +package com.example.moviecli.command; + +import com.example.moviecli.repository.MovieRepository; +import com.example.moviecli.view.ConsoleView; + +public class HelpCommand implements Command { + private final ConsoleView view; + + public HelpCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "help"; + } + + @Override + public void execute(String[] args, MovieRepository repository) { + view.printInfo("可用命令:"); + view.printInfo(" crawl - 爬取指定网站的数据"); + view.printInfo(" list - 列出所有记录"); + view.printInfo(" search <词> - 按标题搜索"); + view.printInfo(" stat - 统计评分分布"); + view.printInfo(" export - 导出为 movies.csv"); + view.printInfo(" help - 显示本帮助"); + view.printInfo(" exit - 退出程序"); + view.printInfo("\n支持的 URL 示例:"); + view.printInfo(" https://movie.douban.com/top250"); + view.printInfo(" https://news.sina.com.cn/"); + view.printInfo(" https://book.douban.com/top250"); + } +} \ No newline at end of file diff --git a/project/command/ListCommand.java b/project/command/ListCommand.java new file mode 100644 index 0000000..2cca394 --- /dev/null +++ b/project/command/ListCommand.java @@ -0,0 +1,29 @@ +package com.example.moviecli.command; + +import com.example.moviecli.repository.MovieRepository; +import com.example.moviecli.view.ConsoleView; +import com.example.moviecli.model.Movie; +public class ListCommand implements Command { + private final ConsoleView view; + + public ListCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "list"; + } + + @Override + public void execute(String[] args, MovieRepository repository) { + var movies = repository.getAll(); + if (movies.isEmpty()) { + view.printInfo("暂无数据,请先执行 crawl 。"); + return; + } + for (Movie m : movies) { + System.out.println(m.getRank() + ". " + m.getTitle() + " - " + m.getScore()); + } + } +} \ No newline at end of file diff --git a/project/command/SearchCommand.java b/project/command/SearchCommand.java new file mode 100644 index 0000000..b67e45b --- /dev/null +++ b/project/command/SearchCommand.java @@ -0,0 +1,35 @@ +package com.example.moviecli.command; + +import com.example.moviecli.model.Movie; +import com.example.moviecli.repository.MovieRepository; +import com.example.moviecli.view.ConsoleView; + +public class SearchCommand implements Command { + private final ConsoleView view; + + public SearchCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "search"; + } + + @Override + public void execute(String[] args, MovieRepository repository) { + if (args.length < 2) { + view.printError("用法: search <关键词>"); + return; + } + String keyword = args[1].toLowerCase(); + var result = repository.getAll().stream() + .filter(m -> m.getTitle().toLowerCase().contains(keyword)) + .toList(); + if (result.isEmpty()) { + view.printInfo("没有找到包含 \"" + keyword + "\" 的记录。"); + } else { + result.forEach(m -> System.out.println(m.getRank() + ". " + m.getTitle() + " - " + m.getScore())); + } + } +} \ No newline at end of file diff --git a/project/command/StatCommand.java b/project/command/StatCommand.java new file mode 100644 index 0000000..71482f0 --- /dev/null +++ b/project/command/StatCommand.java @@ -0,0 +1,33 @@ +package com.example.moviecli.command; + +import com.example.moviecli.repository.MovieRepository; +import com.example.moviecli.view.ConsoleView; +import java.util.Map; +import java.util.stream.Collectors; +import com.example.moviecli.model.Movie; +public class StatCommand implements Command { + private final ConsoleView view; + + public StatCommand(ConsoleView view) { + this.view = view; + } + + @Override + public String getName() { + return "stat"; + } + + @Override + public void execute(String[] args, MovieRepository repository) { + var movies = repository.getAll(); + if (movies.isEmpty()) { + view.printInfo("无数据,请先 crawl。"); + return; + } + Map scoreCount = movies.stream() + .collect(Collectors.groupingBy(Movie::getScore, Collectors.counting())); + view.printInfo("评分分布:"); + scoreCount.forEach((score, count) -> + System.out.println(score + " 分 -> " + count + " 条")); + } +} \ No newline at end of file