5 changed files with 167 additions and 0 deletions
@ -0,0 +1,60 @@ |
|||
package com.example.datacollect; // 声明包名,与项目结构匹配
|
|||
|
|||
import java.util.Date; // 导入日期类,因为要用到Date类型
|
|||
|
|||
// Model层:只封装数据,无业务逻辑
|
|||
public class Article { |
|||
// 原有字段
|
|||
private String title; // 文章标题
|
|||
private String content; // 文章内容
|
|||
// 新增字段
|
|||
private String author; // 文章作者
|
|||
private Date publishDate; // 发布日期
|
|||
|
|||
// 1. 无参构造器(方便创建空对象)
|
|||
public Article() { |
|||
} |
|||
|
|||
// 2. 全参构造器(方便一次性赋值创建对象)
|
|||
public Article(String title, String content, String author, Date publishDate) { |
|||
this.title = title; |
|||
this.content = content; |
|||
this.author = author; |
|||
this.publishDate = publishDate; |
|||
} |
|||
|
|||
// 3. Getter/Setter方法(私有字段只能通过这些方法读写)
|
|||
public String getTitle() { |
|||
return title; |
|||
} |
|||
|
|||
public void setTitle(String title) { |
|||
this.title = title; |
|||
} |
|||
|
|||
public String getContent() { |
|||
return content; |
|||
} |
|||
|
|||
public void setContent(String content) { |
|||
this.content = content; |
|||
} |
|||
|
|||
// 新增字段的get/set
|
|||
public String getAuthor() { |
|||
return author; |
|||
} |
|||
|
|||
public void setAuthor(String author) { |
|||
this.author = author; |
|||
} |
|||
|
|||
public Date getPublishDate() { |
|||
return publishDate; |
|||
} |
|||
|
|||
public void setPublishDate(Date publishDate) { |
|||
this.publishDate = publishDate; |
|||
} |
|||
} |
|||
|
|||
@ -0,0 +1,52 @@ |
|||
package com.example.datacollect; |
|||
|
|||
import java.util.Scanner; |
|||
|
|||
// Controller层:处理用户输入、调用工具类/模型,是程序的入口
|
|||
public class CommandController { |
|||
public static void main(String[] args) { |
|||
// 在CommandController的main方法开头
|
|||
System.out.println("===== 暗色主题模式 ====="); |
|||
System.out.println("背景色:" + ThemeConfig.BACKGROUND_COLOR); |
|||
System.out.println("文字色:" + ThemeConfig.TEXT_COLOR); |
|||
// 1. 创建扫描器,接收用户输入
|
|||
Scanner scanner = new Scanner(System.in); |
|||
System.out.println("===== 命令行工具 ====="); |
|||
System.out.println("指令说明:c/crawl=执行爬虫 | exit=退出 | 其他=未知命令"); |
|||
|
|||
// 2. 循环接收命令(直到输入exit)
|
|||
while (true) { |
|||
System.out.print("\n请输入命令:"); |
|||
String inputCommand = scanner.nextLine().trim(); // 去除首尾空格
|
|||
|
|||
// 3. 把命令记录到历史(调用HistoryCommand的方法)
|
|||
HistoryCommand.addCommand(inputCommand); |
|||
// 在CommandController的while循环中,新增else if分支
|
|||
if (inputCommand.startsWith("url ")) { // 比如输入:url https://www.baidu.com
|
|||
// 截取URL部分(去掉"url "前缀)
|
|||
String url = inputCommand.substring(4).trim(); |
|||
if (UrlValidator.isValidUrl(url)) { |
|||
System.out.println("✅ URL格式合法:" + url); |
|||
} else { |
|||
System.out.println("❌ URL格式非法:" + url); |
|||
} |
|||
} |
|||
|
|||
// 4. 命令别名:c 等价于 crawl
|
|||
if (inputCommand.equals("c") || inputCommand.equals("crawl")) { |
|||
System.out.println("✅ 执行爬虫命令..."); |
|||
// 这里可以后续扩展:调用爬虫逻辑,比如爬取文章并封装到Article
|
|||
} else if (inputCommand.equals("exit")) { |
|||
System.out.println("❌ 退出程序,历史命令如下:"); |
|||
// 打印所有历史命令
|
|||
System.out.println(HistoryCommand.getCommandHistory()); |
|||
break; // 退出循环,结束程序
|
|||
} else { |
|||
System.out.println("❓ 未知命令,请重新输入!"); |
|||
} |
|||
} |
|||
|
|||
// 5. 关闭扫描器(释放资源)
|
|||
scanner.close(); |
|||
} |
|||
} |
|||
@ -0,0 +1,26 @@ |
|||
package com.example.datacollect; |
|||
|
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
|
|||
// 工具类:专门记录命令历史,属于MVC的辅助层
|
|||
public class HistoryCommand { |
|||
// 1. 私有化List,避免外部直接修改(安全)
|
|||
// static:整个程序只有一份,所有地方共用这个历史列表
|
|||
private static List<String> commandHistory = new ArrayList<>(); |
|||
|
|||
// 2. 添加命令到历史(外部调用这个方法记录命令)
|
|||
public static void addCommand(String command) { |
|||
commandHistory.add(command); |
|||
} |
|||
|
|||
// 3. 获取所有历史命令(返回副本,避免原列表被外部篡改)
|
|||
public static List<String> getCommandHistory() { |
|||
return new ArrayList<>(commandHistory); // 返回副本,原列表不会被改
|
|||
} |
|||
|
|||
// 4. 清空历史命令(可选,方便测试)
|
|||
public static void clearHistory() { |
|||
commandHistory.clear(); |
|||
} |
|||
} |
|||
@ -0,0 +1,8 @@ |
|||
package com.example.datacollect; |
|||
|
|||
// 常量类:存储主题配置(只改这里的常量,整个程序的主题就变了)
|
|||
public class ThemeConfig { |
|||
// 暗色主题:修改BACKGROUND_COLOR常量(这就是“修改一处常量”)
|
|||
public static final String BACKGROUND_COLOR = "#1E1E1E"; // 暗色背景(原亮色是#FFFFFF)
|
|||
public static final String TEXT_COLOR = "#FFFFFF"; // 文字白色(适配暗色背景)
|
|||
} |
|||
@ -0,0 +1,21 @@ |
|||
package com.example.datacollect; |
|||
|
|||
import java.util.regex.Pattern; |
|||
|
|||
// 工具类:专门验证URL格式
|
|||
public class UrlValidator { |
|||
// 1. 定义URL正则表达式(简化版,能匹配http/https开头的网址)
|
|||
private static final String URL_REGEX = "^(https?://)?([a-zA-Z0-9_-]+\\.)+[a-zA-Z]{2,6}(/.*)?$"; |
|||
// 2. 编译正则表达式(提升匹配效率)
|
|||
private static final Pattern URL_PATTERN = Pattern.compile(URL_REGEX); |
|||
|
|||
// 3. 公开方法:验证URL是否合法,返回true/false
|
|||
public static boolean isValidUrl(String url) { |
|||
// 先判断URL是否为空
|
|||
if (url == null || url.trim().isEmpty()) { |
|||
return false; |
|||
} |
|||
// 用正则匹配URL
|
|||
return URL_PATTERN.matcher(url.trim()).matches(); |
|||
} |
|||
} |
|||
Loading…
Reference in new issue