Browse Source

上传文件至 'w9'

main
Zhengjie 1 month ago
parent
commit
fe1213c0b4
  1. 27
      w9/CrawlCommand.java
  2. 24
      w9/ExitCommand.java
  3. 23
      w9/HelpCommand.java
  4. 33
      w9/HistoryCommand.java
  5. 23
      w9/ListCommand.java

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

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);
}
}

23
w9/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, history, exit");
}
}

33
w9/HistoryCommand.java

@ -0,0 +1,33 @@
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;
private final List<String> history;
public HistoryCommand(ConsoleView view, List<String> history) {
this.view = view;
this.history = history;
}
@Override
public String getName() {
return "history";
}
@Override
public void execute(String[] args, List<Article> articles) {
if (history.isEmpty()){
view.printInfo("No command has been executed yet.");
} else {
view.printInfo("History commands:");
for (int i=0; i<history.size(); i++){
String s = history.get(i);
view.printInfo((i+1) + "." + s);
}
}
}
}

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