-
Notifications
You must be signed in to change notification settings - Fork 0
/
Note2
139 lines (119 loc) · 4.36 KB
/
Note2
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
import java.io.*;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Note implements Serializable {
private static final long serialVersionUID = 1L;
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 newContent) {
content = newContent;
}
@Override
public String toString() {
return "Title: " + title + "\nContent: " + content;
}
}
class NoteTaker implements Serializable {
private static final long serialVersionUID = 1L;
private List<Note> notes;
public NoteTaker() {
notes = new ArrayList<>();
}
public void addNote(String title, String content) {
Note newNote = new Note(title, content);
notes.add(newNote);
System.out.println("Note added:\n" + newNote);
}
public void editNote(int index, String newContent) {
if (index >= 0 && index < notes.size()) {
Note note = notes.get(index);
note.setContent(newContent);
System.out.println("Note edited:\n" + note);
} else {
System.out.println("Invalid note index.");
}
}
public void displayNotes() {
if (notes.isEmpty()) {
System.out.println("No notes found.");
} else {
System.out.println("Notes:");
for (int i = 0; i < notes.size(); i++) {
System.out.println((i + 1) + ". " + notes.get(i));
}
}
}
public void saveNotesToFile(String filename) {
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filename))) {
oos.writeObject(this);
System.out.println("Note data saved to file: " + filename);
} catch (IOException e) {
e.printStackTrace();
}
}
public static NoteTaker loadNotesFromFile(String filename) {
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filename))) {
return (NoteTaker) ois.readObject();
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
return new NoteTaker(); // If an error occurs, return a new note taker instance
}
}
}
public class NoteTakerApp {
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
NoteTaker noteTaker = new NoteTaker();
// Load note data from file (if the file exists)
noteTaker = NoteTaker.loadNotesFromFile("notes.dat");
while (true) {
System.out.println("\nChoose an action:");
System.out.println("1. Add Note");
System.out.println("2. Edit Note");
System.out.println("3. View Notes");
System.out.println("4. Save Note Data to File");
System.out.println("5. Exit");
int choice = scanner.nextInt();
scanner.nextLine(); // Clear buffer
switch (choice) {
case 1:
System.out.println("Enter note title:");
String title = scanner.nextLine();
System.out.println("Enter note content:");
String content = scanner.nextLine();
noteTaker.addNote(title, content);
break;
case 2:
System.out.println("Enter note index to edit:");
int noteIndex = scanner.nextInt();
scanner.nextLine(); // Clear buffer
System.out.println("Enter new note content:");
String newContent = scanner.nextLine();
noteTaker.editNote(noteIndex - 1, newContent);
break;
case 3:
noteTaker.displayNotes();
break;
case 4:
noteTaker.saveNotesToFile("notes.dat");
break;
case 5:
System.out.println("Exiting the Note Taker. Goodbye!");
System.exit(0);
default:
System.out.println("Invalid choice. Please choose again.");
}
}
}
}