From edad47f474dced2326ee941e687b924f19579862 Mon Sep 17 00:00:00 2001 From: Jiayuheng Date: Thu, 7 May 2026 14:31:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=B8=8A=E4=BC=A0=E6=96=87=E4=BB=B6=E8=87=B3?= =?UTF-8?q?=20'w9'?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- w9/Article.java | 69 ++++++++++++++++++++++++++++++ w9/HistoryCommand.java | 96 ++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 165 insertions(+) create mode 100644 w9/Article.java create mode 100644 w9/HistoryCommand.java diff --git a/w9/Article.java b/w9/Article.java new file mode 100644 index 0000000..ca83ab5 --- /dev/null +++ b/w9/Article.java @@ -0,0 +1,69 @@ +import java.util.Date; + +public class Article { + private String title; + private String content; + private String author; // 新增字段:作者 + private Date publishDate; // 新增字段:发布日期 + + public Article() { + this.publishDate = new Date(); // 默认使用当前时间 + } + + public Article(String title, String content, String author) { + this.title = title; + this.content = content; + this.author = author; + this.publishDate = new Date(); + } + + public Article(String title, String content, String author, Date publishDate) { + this.title = title; + this.content = content; + this.author = author; + this.publishDate = publishDate; + } + + // Getters and Setters + 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; + } + + 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; + } + + @Override + public String toString() { + return "Article{" + + "title='" + title + '\'' + + ", content='" + content + '\'' + + ", author='" + author + '\'' + + ", publishDate=" + publishDate + + '}'; + } +} \ No newline at end of file diff --git a/w9/HistoryCommand.java b/w9/HistoryCommand.java new file mode 100644 index 0000000..2d39582 --- /dev/null +++ b/w9/HistoryCommand.java @@ -0,0 +1,96 @@ +import java.util.ArrayList; +import java.util.List; +import java.util.Scanner; + +public class HistoryCommand { + // 使用List存储所有输入的命令 + private List commandHistory; + + public HistoryCommand() { + this.commandHistory = new ArrayList<>(); + } + + // 添加命令到历史记录 + public void addCommand(String command) { + if (command != null && !command.trim().isEmpty()) { + commandHistory.add(command.trim()); + } + } + + // 获取所有命令历史 + public List getCommandHistory() { + return new ArrayList<>(commandHistory); // 返回副本,防止外部修改 + } + + // 获取命令数量 + public int getCommandCount() { + return commandHistory.size(); + } + + // 查看指定索引的命令 + public String getCommand(int index) { + if (index >= 0 && index < commandHistory.size()) { + return commandHistory.get(index); + } + return null; + } + + // 清空命令历史 + public void clearHistory() { + commandHistory.clear(); + } + + // 显示最近的n条命令 + public void showRecentCommands(int n) { + int start = Math.max(0, commandHistory.size() - n); + System.out.println("最近 " + (commandHistory.size() - start) + " 条命令:"); + for (int i = start; i < commandHistory.size(); i++) { + System.out.println((i + 1) + ". " + commandHistory.get(i)); + } + } + + // 显示所有命令历史 + public void showAllCommands() { + System.out.println("所有命令历史(共 " + commandHistory.size() + " 条):"); + for (int i = 0; i < commandHistory.size(); i++) { + System.out.println((i + 1) + ". " + commandHistory.get(i)); + } + } + + // 命令行交互测试 + public static void main(String[] args) { + HistoryCommand history = new HistoryCommand(); + Scanner scanner = new Scanner(System.in); + + System.out.println("命令历史记录器 - 输入 'exit' 退出,输入 'history' 查看历史记录"); + + while (true) { + System.out.print("> "); + String input = scanner.nextLine(); + + // 添加到历史记录 + history.addCommand(input); + + if ("exit".equalsIgnoreCase(input)) { + System.out.println("退出程序..."); + break; + } else if ("history".equalsIgnoreCase(input)) { + history.showAllCommands(); + } else if (input.startsWith("history ")) { + try { + int n = Integer.parseInt(input.substring(8).trim()); + history.showRecentCommands(n); + } catch (NumberFormatException e) { + System.out.println("无效的数字格式"); + } + } else if ("clear".equalsIgnoreCase(input)) { + history.clearHistory(); + System.out.println("历史记录已清空"); + } else { + System.out.println("执行命令: " + input); + } + } + + scanner.close(); + } +} \ No newline at end of file