Última atividade 1718984355

NotesApp.java Bruto
1import java.io.*;
2import java.util.ArrayList;
3import java.util.Scanner;
4
5class Note {
6 private String title;
7 private String content;
8
9 public Note(String title, String content) {
10 this.title = title;
11 this.content = content;
12 }
13
14 // Getters and setters for note details
15}
16
17public class NotesApp {
18 private static ArrayList<Note> notes = new ArrayList<>();
19 private static final String FILENAME = "notes.txt"; // File to save notes
20 private static Scanner scanner = new Scanner(System.in);
21
22 public static void main(String[] args) {
23 loadNotes(); // Load saved notes from the file
24
25 while (true) {
26 displayMenu();
27 int choice = scanner.nextInt();
28 scanner.nextLine(); // Consume newline character
29
30 switch (choice) {
31 case 1:
32 createNote();
33 break;
34 case 2:
35 viewNotes();
36 break;
37 case 3:
38 editNote();
39 break;
40 case 4:
41 deleteNote();
42 break;
43 case 5:
44 saveNotes(); // Save notes to the file
45 System.out.println("Goodbye!");
46 System.exit(0);
47 default:
48 System.out.println("Invalid choice. Please try again.");
49 }
50 }
51 }
52
53 // Implement methods for creating, viewing, editing, and deleting notes
54
55 private static void displayMenu() {
56 System.out.println("\nNotes Keeping App");
57 System.out.println("1. Create a new note");
58 System.out.println("2. View saved notes");
59 System.out.println("3. Edit a note");
60 System.out.println("4. Delete a note");
61 System.out.println("5. Exit");
62 System.out.print("Enter your choice: ");
63 }
64
65 private static void loadNotes() {
66 try (BufferedReader reader = new BufferedReader(new FileReader(FILENAME))) {
67 String line;
68 while ((line = reader.readLine()) != null) {
69 String[] parts = line.split("\\|");
70 if (parts.length == 2) {
71 notes.add(new Note(parts[0], parts[1]));
72 }
73 }
74 } catch (IOException e) {
75 // Handle file loading errors, or create the file if it doesn't exist
76 }
77 }
78
79 private static void saveNotes() {
80 try (PrintWriter writer = new PrintWriter(new FileWriter(FILENAME))) {
81 for (Note note : notes) {
82 writer.println(note.getTitle() + "|" + note.getContent());
83 }
84 } catch (IOException e) {
85 e.printStackTrace();
86 }
87 }
88}
89