Java如何开发一个简易的笔记分类管理系统

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

开发一个简易的笔记分类管理系统,可以用Java实现控制台版本的应用,包含基本的增删改查功能。整个系统由几个核心类组成,结构清晰、易于理解和扩展。

1. 确定系统功能需求

这个简易系统需要支持以下功能:

  • 添加笔记(标题、内容、分类)
  • 查看所有笔记
  • 按分类查看笔记
  • 修改笔记内容
  • 删除笔记
  • 退出程序

2. 设计核心类结构

系统主要包含两个实体类和一个管理类:

Note 类:表示一条笔记

包含标题、内容、分类三个属性,提供构造方法和 getter/setter。

public class Note {
    private String title;
    private String content;
    private String category;
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.*;

public class NoteManager { private List notes = new ArrayList<>();

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 zuojiankuohaophpcn notes.size(); i++) {
        System.out.println((i + 1) + ". " + notes.get(i));
    }
}

public void viewNotesByCategory(String category) {
    List filtered = notes.stream()
            .filter(n -youjiankuohaophpcn 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 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;
}

}

3. 创建主程序入口

使用 Scanner 接收用户输入,提供菜单式交互。

import java.util.Scanner;

public class Main { public static void main(String[] args) { NoteManager manager = new NoteManager(); Scanner scanner = new Scanner(System.in); boolean running = true;

    S

ystem.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(); }

}

4. 运行与扩展建议

将以上代码保存为对应文件(Note.java、NoteManager.java、Main.java),编译运行即可使用。

后续可扩展方向:

  • 将数据持久化到文件(如 JSON 或 CSV)
  • 增加分类管理功能
  • 支持模糊搜索标题
  • 改为图形界面(Swing 或 JavaFX)

基本上就这些,不复杂但容易忽略细节比如 Scanner 的换行处理。只要理清对象职责,就能快速搭建出可用的小系统。