Browse Source

feat:w9

main
WangJunyue 1 month ago
parent
commit
59a21a5235
  1. BIN
      w9/AI架构审计.docx
  2. 22
      w9/datacollect/Main.java
  3. 9
      w9/datacollect/command/Command.java
  4. 27
      w9/datacollect/command/CrawlCommand.java
  5. 24
      w9/datacollect/command/ExitCommand.java
  6. 23
      w9/datacollect/command/HelpCommand.java
  7. 34
      w9/datacollect/command/HistoryCommand.java
  8. 23
      w9/datacollect/command/ListCommand.java
  9. 48
      w9/datacollect/controller/CrawlerController.java
  10. 66
      w9/datacollect/model/Article.java
  11. 53
      w9/datacollect/view/ConsoleView.java
  12. BIN
      w9/w9思考题.docx

BIN
w9/AI架构审计.docx

Binary file not shown.

22
w9/datacollect/Main.java

@ -0,0 +1,22 @@
package com.example.datacollect;
import com.example.datacollect.controller.CrawlerController;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.ArrayList;
import java.util.List;
public class Main {
public static void main(String[] args) {
ConsoleView view = new ConsoleView();
List<Article> articles = new ArrayList<>();
CrawlerController controller = new CrawlerController(view, articles);
view.printSuccess("Welcome to CLI Crawler (w9_1)! Type help for commands.");
while (true) {
controller.handle(view.readLine());
}
}
}

9
w9/datacollect/command/Command.java

@ -0,0 +1,9 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import java.util.List;
public interface Command {
String getName();
void execute(String[] args, List<Article> articles);
}

27
w9/datacollect/command/CrawlCommand.java

@ -0,0 +1,27 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.List;
public class CrawlCommand implements Command {
private final ConsoleView view;
public CrawlCommand(ConsoleView view) {
this.view = view;
}
@Override
public String getName() {
return "crawl";
}
@Override
public void execute(String[] args, List<Article> articles) {
if (args.length < 2) {
view.printError("Usage: crawl <url>");
return;
}
view.printInfo("Stub: would crawl " + args[1]);
}
}

24
w9/datacollect/command/ExitCommand.java

@ -0,0 +1,24 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.List;
public class ExitCommand implements Command {
private final ConsoleView view;
public ExitCommand(ConsoleView view) {
this.view = view;
}
@Override
public String getName() {
return "exit";
}
@Override
public void execute(String[] args, List<Article> articles) {
view.printSuccess("Bye!");
System.exit(0);
}
}

23
w9/datacollect/command/HelpCommand.java

@ -0,0 +1,23 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.List;
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, List<Article> articles) {
view.printInfo("Commands: crawl <url>, list, help, exit");
}
}

34
w9/datacollect/command/HistoryCommand.java

@ -0,0 +1,34 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.List;
public class HistoryCommand implements Command {
private final ConsoleView view;
public HistoryCommand(ConsoleView view) {
this.view = view;
}
@Override
public String getName() {
return "history";
}
@Override
public void execute(String[] args, List<Article> articles) {
List<String> history = view.getCommandHistory();
if (history.isEmpty()) {
view.printInfo("No command history.");
return;
}
view.printInfo("Command History ");
for (int i = 0; i < history.size(); i++) {
view.printInfo((i + 1) + ". " + history.get(i));
}
}
}

23
w9/datacollect/command/ListCommand.java

@ -0,0 +1,23 @@
package com.example.datacollect.command;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.List;
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, List<Article> articles) {
view.display(articles);
}
}

48
w9/datacollect/controller/CrawlerController.java

@ -0,0 +1,48 @@
package com.example.datacollect.controller;
import com.example.datacollect.command.Command;
import com.example.datacollect.command.CrawlCommand;
import com.example.datacollect.command.ExitCommand;
import com.example.datacollect.command.HelpCommand;
import com.example.datacollect.command.ListCommand;
import com.example.datacollect.model.Article;
import com.example.datacollect.view.ConsoleView;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class CrawlerController {
private final Map<String, Command> commands = new HashMap<>();
private final ConsoleView view;
private final List<Article> articles;
public CrawlerController(ConsoleView view, List<Article> articles) {
this.view = view;
this.articles = articles;
register(new HelpCommand(view));
register(new ListCommand(view));
register(new CrawlCommand(view));
register(new ExitCommand(view));
register(new HistoryCommand(view));
}
private void register(Command command) {
commands.put(command.getName(), command);
}
public void handle(String input) {
String text = input == null ? "" : input.trim();
if (text.isEmpty()) {
return;
}
String[] args = text.split("\\s+");
String cmdName = args[0].toLowerCase();
Command command = commands.get(cmdName);
if (command == null) {
view.printError("Unknown command: " + cmdName);
return;
}
command.execute(args, articles);
}
}

66
w9/datacollect/model/Article.java

@ -0,0 +1,66 @@
package com.example.datacollect.model;
public class Article {
private String title;
private String url;
private String content;
private String author;
private LocalDate publishDate;
public Article(String title, String url, String contentString author, LocalDate publishDate) {
this.title = title;
this.url = url;
this.content = content;
this.author = author;
this.publishDate = publishDate;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public LocalDate getPublishDate() {
return publishDate;
}
public void setPublishDate(LocalDate publishDate) {
this.publishDate = publishDate;
}
@Override
public String toString() {
return "Article{"
+ "title='" + title + '\''
+ ", url='" + url + '\''
+ '}';
}
}

53
w9/datacollect/view/ConsoleView.java

@ -0,0 +1,53 @@
package com.example.datacollect.view;
import com.example.datacollect.model.Article;
import java.util.List;
import java.util.Scanner;
public class ConsoleView {
private static final String ANSI_RESET = "\u001B[0m";
private static final String ANSI_GREEN = "\u001B[32m";
private static final String ANSI_RED = "\u001B[31m";
private static final String ANSI_BLUE = "\u001B[34m";
private final Scanner scanner = new Scanner(System.in);
private final List<String> commandHistory = new ArrayList<>();
public String readLine() {
System.out.print("> ");
String input = scanner.nextLine().trim();
if (!input.isBlank()) {
commandHistory.add(input);
}
return input;
}
}
public List<String> getCommandHistory() {
return new ArrayList<>(commandHistory);
}
public void printSuccess(String msg) {
System.out.println(ANSI_GREEN + msg + ANSI_RESET);
}
public void printError(String msg) {
System.out.println(ANSI_RED + msg + ANSI_RESET);
}
public void printInfo(String msg) {
System.out.println(ANSI_BLUE + msg + ANSI_RESET);
}
public void display(List<Article> articles) {
if (articles.isEmpty()) {
printInfo("暂无文章,请先执行 crawl。");
return;
}
for (int i = 0; i < articles.size(); i++) {
Article a = articles.get(i);
System.out.println((i + 1) + ". " + a.getTitle() + " | " + a.getUrl());
}
}
}

BIN
w9/w9思考题.docx

Binary file not shown.
Loading…
Cancel
Save