系统通过DiaryEntry和DiaryManager类实现日记的增删改查,结合Scanner提供控制台交互界面,支持按标题检索,结构清晰,便于扩展文件存储与日期查询等功能。

在Java中实现一个个人日记管理系统,核心是通过面向对象的方式组织数据与功能。系统需要支持日记的创建、查看、修改和删除,同时可以按日期或标题检索内容。下面是一个简洁实用的实现思路和代码结构。
每个日记条目应包含基本信息:日期、标题、内容。使用Java类来封装这些属性。
public class DiaryEntry {
private String date;
private String title;
private String content;
public DiaryEntry(String date, String title, String content) {
this.date = date;
this.title = title;
this.content = content;
}
// Getter 和 Setter 方法
public String getDate() { return date; }
public void setDate(String date) { this.date = date; }
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; }
@Override
public String toString() {
return "日期: " + date + "\n标题: " + title + "\n内容: " + content + "\n";
}
}
该类负责管理多个日记条目,使用ArrayList存储,并提供增删改查方法。
import java.util.ArrayList;
import java.util.List;
public class DiaryManager {
private List<DiaryEntry> entries;
public DiaryManager() {
entries = new ArrayList<>();
}
// 添加日记
public void addEntry(DiaryEntry entry) {
entries.add(entry);
System.out.println("日记已添加。");
}
// 查看所有日记
public void viewAllEntries() {
if (entries.isEmpty()) {
System.out.println("暂无日记。");
} else {
for (DiaryEntry entry : entries) {
System.out.println(entry);
}
}
}
// 根据标题查找日记
public DiaryEntry findEntryByTitle(String title) {
for (DiaryEntry entry : entries) {
if (entry.getTitle().equalsIgnoreCase(title)) {
return entry;
}
}
return null;
}
// 修改日记内容
public boolean updateEntry(String title, String newContent) {
DiaryEntry entry = findEntryByTitle(title);
if (entry != null) {
entry.setContent(newContent);
System.out.println("日记已更新。");
return true;
} else {
System.out.println("未找到该标题的日记。");
return false;
}
}
// 删除日记
public boolean deleteEntry(String title) {
DiaryEntry entry = findEntryByTitle(title);
if (entry != null) {
entries.remove(entry);
System.out.println("日记已删除。");
return true;
} else {
System.out.println("未找到该日记。");
return false;
}
}
}
通过控制台输入实现简单的交互式操作。
立即学习“Java免费学习笔记(深入)”;
import java.util.Scanner;
public class Main {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
DiaryManager manager = new DiaryManager();
while (true) {
System.out.println("\n=== 个人日记管理系统 ===");
System.out.println("1. 写日记");
System.out.println("2. 查看所有日记");
System.out.println("3. 搜索日记(按标题)");
System.out.println("4. 修改日记");
System.out.println("5. 删除日记");
System.out.println("6. 退出");
System.out.print("请选择操作: ");
int choice = scanner.nextInt();
scanner.nextLine(); // 消费换行符
switch (choice) {
case 1:
System.out.print("请输入日期(如2025-04-05): ");
String date = scanner.nextLine();
System.out.print("请输入标题: ");
String title = scanner.nextLine();
System.out.print("请输入内容: ");
String content = scanner.nextLine();
manager.addEntry(new DiaryEntry(date, title, content));
break;
case 2:
manager.viewAllEntries();
break;
case 3:
System.out.print("请输入要查找的标题: ");
String searchTitle = scanner.nextLine();
DiaryEntry found = manager.findEntryByTitle(searchTitle);
if (found != null) {
System.out.println("找到日记:\n" + found);
} else {
System.out.println("未找到该日记。");
}
break;
case 4:
System.out.print("请输入要修改的日记标题: ");
String updateTitle = scanner.nextLine();
System.out.print("请输入新内容: ");
String newContent = scanner.nextLine();
manager.updateEntry(updateTitle, newContent);
break;
case 5:
System.out.print("请输入要删除的日记标题: ");
String deleteTitle = scanner.nextLine();
manager.deleteEntry(deleteTitle);
break;
case 6:
System.out.println("再见!");
scanner.close();
return;
default:
System.out.println("无效选择,请重试。");
}
}
}
}
基础版本完成后,可逐步增强功能:
以上就是如何在Java中实现个人日记管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号