-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
156 lines (134 loc) · 4.92 KB
/
NotesApp.java
File metadata and controls
156 lines (134 loc) · 4.92 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
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;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
public void setContent(String content) {
this.content = content;
}
@Override
public String toString() {
return "Title: " + title + "\nContent: " + content;
}
}
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.");
}
}
}
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
System.out.println("No previous notes found, starting fresh.");
}
}
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();
}
}
private static void createNote() {
System.out.print("Enter the title of the note: ");
String title = scanner.nextLine();
System.out.print("Enter the content of the note: ");
String content = scanner.nextLine();
notes.add(new Note(title, content));
System.out.println("Note created successfully!");
}
private static void viewNotes() {
if (notes.isEmpty()) {
System.out.println("No notes available.");
} else {
for (int i = 0; i < notes.size(); i++) {
System.out.println("\nNote " + (i + 1) + ":");
System.out.println(notes.get(i));
}
}
}
private static void editNote() {
viewNotes();
System.out.print("Enter the note number to edit: ");
int noteNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline character
if (noteNumber < 1 || noteNumber > notes.size()) {
System.out.println("Invalid note number.");
return;
}
Note note = notes.get(noteNumber - 1);
System.out.print("Enter new content for the note (current content: " + note.getContent() + "): ");
String newContent = scanner.nextLine();
note.setContent(newContent);
System.out.println("Note updated successfully!");
}
private static void deleteNote() {
viewNotes();
System.out.print("Enter the note number to delete: ");
int noteNumber = scanner.nextInt();
scanner.nextLine(); // Consume newline character
if (noteNumber < 1 || noteNumber > notes.size()) {
System.out.println("Invalid note number.");
return;
}
notes.remove(noteNumber - 1);
System.out.println("Note deleted successfully!");
}
}