Browse Source

上传文件至 'w9'

main
YuWeixia 1 month ago
parent
commit
ecda900e8a
  1. 67
      w9/Article.java
  2. 9
      w9/Command.java
  3. 27
      w9/CrawlCommand.java
  4. 54
      w9/CrawlerController.java
  5. 24
      w9/ExitCommand.java

67
w9/Article.java

@ -0,0 +1,67 @@
package com.example.datacollect.model;
public class Article {
private String title;
private String url;
private String content;
private String author;
private String publishdate;
public Article(String title, String url, String content, String author, String 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 String getPublishdate() {
return publishdate;
}
public void setPublishdate(String publishdate) {
this.publishdate = publishdate;
}
@Override
public String toString() {
return "Article{"
+ "title='" + title + '\''
+ ", url='" + url + '\''
+ ", author='" + author + '\''
+ ", publishdate='" + publishdate + '\''
+ '}';
}
}

9
w9/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/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]);
}
}

54
w9/CrawlerController.java

@ -0,0 +1,54 @@
package com.example.datacollect.controller;
import com.example.datacollect.command.Command;
import com.example.datacollect.command.ClearCommand;
import com.example.datacollect.command.CrawlCommand;
import com.example.datacollect.command.ExitCommand;
import com.example.datacollect.command.HelpCommand;
import com.example.datacollect.command.HistoryCommand;
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;
import java.util.ArrayList;
public class CrawlerController {
private final Map<String, Command> commands = new HashMap<>();
private final ConsoleView view;
private final List<Article> articles;
private final List<String> commandHistory = new ArrayList<>();
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 ClearCommand(view));
register(new HistoryCommand(view, commandHistory));
}
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;
}
commandHistory.add(text);
command.execute(args, articles);
}
}

24
w9/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);
}
}
Loading…
Cancel
Save