NotesApp.java
· 2.7 KiB · Java
Orginalformat
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
class Note {
private String title;
private String content;
public Note(String title, String content) {
this.title = title;
this.content = content;
}
// Getters and setters for note details
}
public class NotesApp {
private static ArrayList<Note> notes = new ArrayList<>();
private static final String FILENAME = "notes.txt"; // File to save notes
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
loadNotes(); // Load saved notes from the file
while (true) {
displayMenu();
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline character
switch (choice) {
case 1:
createNote();
break;
case 2:
viewNotes();
break;
case 3:
editNote();
break;
case 4:
deleteNote();
break;
case 5:
saveNotes(); // Save notes to the file
System.out.println("Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please try again.");
}
}
}
// Implement methods for creating, viewing, editing, and deleting notes
private static void displayMenu() {
System.out.println("\nNotes Keeping App");
System.out.println("1. Create a new note");
System.out.println("2. View saved notes");
System.out.println("3. Edit a note");
System.out.println("4. Delete a note");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
}
private static void loadNotes() {
try (BufferedReader reader = new BufferedReader(new FileReader(FILENAME))) {
String line;
while ((line = reader.readLine()) != null) {
String[] parts = line.split("\\|");
if (parts.length == 2) {
notes.add(new Note(parts[0], parts[1]));
}
}
} catch (IOException e) {
// Handle file loading errors, or create the file if it doesn't exist
}
}
private static void saveNotes() {
try (PrintWriter writer = new PrintWriter(new FileWriter(FILENAME))) {
for (Note note : notes) {
writer.println(note.getTitle() + "|" + note.getContent());
}
} catch (IOException e) {
e.printStackTrace();
}
}
}
1 | import java.io.*; |
2 | import java.util.ArrayList; |
3 | import java.util.Scanner; |
4 | |
5 | class 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 | |
17 | public 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 |