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.
42 lines
1.5 KiB
42 lines
1.5 KiB
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Scanner;
|
|
|
|
public class Main2 {
|
|
public static void main(String[] args) {
|
|
// 1. 初始化:文章集合、历史命令对象、扫描器
|
|
List<Article> articleList = new ArrayList<>();
|
|
HistoryCommand historyCmd = new HistoryCommand();
|
|
Scanner sc = new Scanner(System.in);
|
|
|
|
// 2. 模拟添加两篇测试文章(注意构造方法参数顺序)
|
|
Article art1 = new Article("Java入门", "https://test1.com", "基础语法讲解", "张三", "2026-05-29");
|
|
Article art2 = new Article("面向对象", "https://test2.com", "类与对象", "李四", "2026-05-28");
|
|
articleList.add(art1);
|
|
articleList.add(art2);
|
|
|
|
System.out.println("===== 命令程序启动 =====");
|
|
System.out.println("输入 history 查看历史命令,输入 exit 退出程序");
|
|
|
|
// 3. 循环接收用户输入
|
|
while (true) {
|
|
System.out.print("请输入命令:");
|
|
String input = sc.nextLine().trim();
|
|
|
|
// 输入 exit 退出循环
|
|
if ("exit".equalsIgnoreCase(input)) {
|
|
System.out.println("程序已退出!");
|
|
break;
|
|
}
|
|
|
|
// 把当前输入的命令加入历史记录
|
|
historyCmd.addCommand(input);
|
|
|
|
// 执行 history 查看历史
|
|
if ("history".equalsIgnoreCase(input)) {
|
|
historyCmd.execute(new String[0], articleList);
|
|
}
|
|
}
|
|
sc.close();
|
|
}
|
|
}
|