答案:系统包含Note、NoteManager和Main类,实现笔记的增删改查。通过控制台菜单操作,用户可添加、查看、修改、删除笔记,支持按分类查询,结构清晰易扩展。

开发一个简易的笔记分类管理系统,可以用Java实现控制台版本的应用,包含基本的增删改查功能。整个系统由几个核心类组成,结构清晰、易于理解和扩展。
这个简易系统需要支持以下功能:
系统主要包含两个实体类和一个管理类:
Note 类:表示一条笔记
立即学习“Java免费学习笔记(深入)”;
包含标题、内容、分类三个属性,提供构造方法和 getter/setter。
public class Note {
private String title;
private String content;
private String category;
<pre class='brush:java;toolbar:false;'>public Note(String title, String content, String category) {
this.title = title;
this.content = content;
this.category = category;
}
// 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; }
public String getCategory() { return category; }
public void setCategory(String category) { this.category = category; }
@Override
public String toString() {
return "标题: " + title + " | 分类: " + category + " | 内容: " + content;
}}
NoteManager 类:管理所有笔记
使用 ArrayList 存储笔记,提供增删改查方法。
import java.util.*;
<p>public class NoteManager {
private List<Note> notes = new ArrayList<>();</p><pre class='brush:java;toolbar:false;'>public void addNote(Note note) {
notes.add(note);
System.out.println("笔记已添加!");
}
public void viewAllNotes() {
if (notes.isEmpty()) {
System.out.println("暂无笔记。");
return;
}
for (int i = 0; i < notes.size(); i++) {
System.out.println((i + 1) + ". " + notes.get(i));
}
}
public void viewNotesByCategory(String category) {
List<Note> filtered = notes.stream()
.filter(n -> n.getCategory().equalsIgnoreCase(category))
.toList();
if (filtered.isEmpty()) {
System.out.println("该分类下无笔记。");
return;
}
for (Note note : filtered) {
System.out.println("- " + note);
}
}
public boolean updateNote(String title, String newContent) {
for (Note note : notes) {
if (note.getTitle().equals(title)) {
note.setContent(newContent);
System.out.println("笔记已更新!");
return true;
}
}
System.out.println("未找到该标题的笔记。");
return false;
}
public boolean deleteNote(String title) {
Iterator<Note> it = notes.iterator();
while (it.hasNext()) {
Note note = it.next();
if (note.getTitle().equals(title)) {
it.remove();
System.out.println("笔记已删除!");
return true;
}
}
System.out.println("未找到该标题的笔记。");
return false;
}}
使用 Scanner 接收用户输入,提供菜单式交互。
import java.util.Scanner;
<p>public class Main {
public static void main(String[] args) {
NoteManager manager = new NoteManager();
Scanner scanner = new Scanner(System.in);
boolean running = true;</p><pre class='brush:java;toolbar:false;'> System.out.println("=== 简易笔记分类管理系统 ===");
while (running) {
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("输入标题:");
String title = scanner.nextLine();
System.out.print("输入内容:");
String content = scanner.nextLine();
System.out.print("输入分类:");
String category = scanner.nextLine();
manager.addNote(new Note(title, content, category));
break;
case 2:
manager.viewAllNotes();
break;
case 3:
System.out.print("输入分类名:");
String cat = scanner.nextLine();
manager.viewNotesByCategory(cat);
break;
case 4:
System.out.print("输入要修改的笔记标题:");
String oldTitle = scanner.nextLine();
System.out.print("输入新内容:");
String newContent = scanner.nextLine();
manager.updateNote(oldTitle, newContent);
break;
case 5:
System.out.print("输入要删除的笔记标题:");
String delTitle = scanner.nextLine();
manager.deleteNote(delTitle);
break;
case 6:
System.out.println("再见!");
running = false;
break;
default:
System.out.println("无效选项,请重试。");
}
}
scanner.close();
}}
将以上代码保存为对应文件(Note.java、NoteManager.java、Main.java),编译运行即可使用。
后续可扩展方向:
基本上就这些,不复杂但容易忽略细节比如 Scanner 的换行处理。只要理清对象职责,就能快速搭建出可用的小系统。
以上就是Java如何开发一个简易的笔记分类管理系统的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号