-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNotesApp.java
More file actions
175 lines (162 loc) · 6.19 KB
/
NotesApp.java
File metadata and controls
175 lines (162 loc) · 6.19 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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
import java.io.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.util.logging.*;
// Simple text-based Notes app using FileWriter and BufferedReader/FileReader.
public class NotesApp {
private static final String FILE_NAME = "notes.txt";
private static final Logger LOGGER = Logger.getLogger(NotesApp.class.getName());
private static final Scanner SC = new Scanner(System.in);
private static final DateTimeFormatter TF = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
public static void main(String[] args) {
setupLogger();
System.out.println("=== Simple Notes App (file: " + FILE_NAME + ") ===");
while (true) {
printMenu();
String choice = SC.nextLine().trim();
switch (choice) {
case "1": addNote(); break;
case "2": listNotes(); break;
case "3": searchNotes(); break;
case "4": deleteNote(); break;
case "5": clearNotes(); break;
case "6": System.out.println("Goodbye!"); return;
default: System.out.println("Invalid choice, try again.");
}
}
}
private static void setupLogger() {
try {
// write logs to errors.log (append)
Handler fh = new FileHandler("errors.log", true);
fh.setFormatter(new SimpleFormatter());
LOGGER.addHandler(fh);
LOGGER.setLevel(Level.ALL);
} catch (IOException e) {
System.err.println("Could not initialize file logger: " + e.getMessage());
}
}
private static void printMenu() {
System.out.println("\nMenu:");
System.out.println("1. Add note");
System.out.println("2. List notes");
System.out.println("3. Search notes");
System.out.println("4. Delete note");
System.out.println("5. Clear all notes");
System.out.println("6. Exit");
System.out.print("Choose (1-6): ");
}
private static void addNote() {
System.out.println("Enter your note (single line). Press Enter to save:");
String text = SC.nextLine();
String line = TF.format(LocalDateTime.now()) + " - " + text;
// append mode -> second parameter true
try (FileWriter fw = new FileWriter(FILE_NAME, true);
BufferedWriter bw = new BufferedWriter(fw)) {
bw.write(line);
bw.newLine();
System.out.println("Note saved.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to write note", e);
System.out.println("Error saving note: " + e.getMessage());
}
}
private static void listNotes() {
List<String> notes = readAllNotes();
if (notes.isEmpty()) {
System.out.println("(No notes found.)");
return;
}
System.out.println("\n--- Notes ---");
for (int i = 0; i < notes.size(); i++) {
System.out.printf("%d. %s%n", i + 1, notes.get(i));
}
}
private static void searchNotes() {
List<String> notes = readAllNotes();
if (notes.isEmpty()) {
System.out.println("(No notes to search.)");
return;
}
System.out.print("Enter search term: ");
String q = SC.nextLine().toLowerCase();
boolean found = false;
for (int i = 0; i < notes.size(); i++) {
if (notes.get(i).toLowerCase().contains(q)) {
if (!found) System.out.println("\nMatches:");
System.out.printf("%d. %s%n", i + 1, notes.get(i));
found = true;
}
}
if (!found) System.out.println("No matches found.");
}
private static void deleteNote() {
List<String> notes = readAllNotes();
if (notes.isEmpty()) {
System.out.println("(No notes to delete.)");
return;
}
listNotes();
System.out.print("Enter number to delete (0 to cancel): ");
String in = SC.nextLine();
int num;
try {
num = Integer.parseInt(in);
} catch (NumberFormatException e) {
System.out.println("Invalid number.");
return;
}
if (num == 0) {
System.out.println("Cancelled.");
return;
}
if (num < 1 || num > notes.size()) {
System.out.println("Number out of range.");
return;
}
notes.remove(num - 1);
// overwrite file (append=false)
try (FileWriter fw = new FileWriter(FILE_NAME, false);
BufferedWriter bw = new BufferedWriter(fw)) {
for (String n : notes) {
bw.write(n);
bw.newLine();
}
System.out.println("Deleted.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to delete note", e);
System.out.println("Error deleting note: " + e.getMessage());
}
}
private static void clearNotes() {
System.out.print("Are you sure? This will delete all notes. (y/N): ");
String ans = SC.nextLine().trim();
if (!ans.equalsIgnoreCase("y")) {
System.out.println("Cancelled.");
return;
}
try (FileWriter fw = new FileWriter(FILE_NAME, false)) {
// opening with append=false truncates file
System.out.println("All notes cleared.");
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to clear notes", e);
System.out.println("Error clearing notes: " + e.getMessage());
}
}
private static List<String> readAllNotes() {
File f = new File(FILE_NAME);
List<String> lines = new ArrayList<>();
if (!f.exists()) return lines;
try (BufferedReader br = new BufferedReader(new FileReader(f))) {
String line;
while ((line = br.readLine()) != null) {
lines.add(line);
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, "Failed to read notes", e);
System.out.println("Error reading notes: " + e.getMessage());
}
return lines;
}
}