You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

56 lines
1.8 KiB

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
public class Main3 {
public static void main(String[] args) {
StrategyFactory2 factory = new StrategyFactory2();
// 模拟一个博客策略
CrawlStrategy2 blogStrategy = new CrawlStrategy2() {
@Override
public boolean supports(String url) {
return url.contains("blog");
}
@Override
public Article2 crawl(String url) {
return new Article2("测试标题", url, "测试内容", "作者", "2025-01-01");
}
};
factory.registerStrategy(blogStrategy);
AnalyzeCommand2 analyzeCmd = new AnalyzeCommand2(factory);
HistoryCommand2 historyCmd = new HistoryCommand2();
List<Article2> articles = new ArrayList<>();
Scanner sc = new Scanner(System.in);
System.out.println("===== 程序启动 =====");
System.out.println("命令:");
System.out.println("analyze 网址");
System.out.println("history");
System.out.println("exit\n");
while (true) {
System.out.print("请输入命令:");
String input = sc.nextLine().trim();
historyCmd.addCommand(input);
if (input.equalsIgnoreCase("exit")) {
System.out.println("退出");
break;
}
if (input.startsWith("analyze ")) {
String[] arr = input.split(" ", 2);
analyzeCmd.execute(new String[]{arr[1]}, articles);
} else if (input.equalsIgnoreCase("history")) {
historyCmd.execute(new String[0], articles);
} else {
System.out.println("未知命令");
}
}
sc.close();
}
}