-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNote.java
249 lines (238 loc) · 9.65 KB
/
Note.java
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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
/**
* The Note class provides a console-based application for managing notes.
* Users can add, remove, list, export, and view encrypted notes stored in files.
* It includes features like folder management, date tracking, and file encryption.
*/
public class Note {
/**
* Displays the main menu options to the user.
*/
public static void menu() {
System.out.println("1: Add");
System.out.println("2: Romove");
System.out.println("3: List");
System.out.println("4: Export");
System.out.println("5: Exit");
}
/**
* Creates the required directories and a date-tracking file if they don't already exist.
*/
static void makeFolder() {
Path path = Paths.get("Passwords");
Path path1 = Paths.get("Notes");
Path path2 = Paths.get("Export");
try {
Files.createDirectories(path);
Files.createDirectories(path1);
Files.createDirectories(path2);
}
catch (IOException i) {
System.out.println("filed created folder");
i.printStackTrace();
}
try {
File file = new File("date.txt");
file.createNewFile();
} catch (IOException i) {
System.out.println("date file did not created");
i.printStackTrace();
}
}
/**
* Saves the current date and a provided note name into a map and serializes it to a file.
*
* @param map The HashMap storing note names and their creation dates.
* @param name The name of the note to be saved.
*/
static void saveDate(HashMap<String, String> map, String name) {
LocalDate date = LocalDate.now();
map.put(name, "" + date);
try {
File file = new File("date.txt");
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream o = new ObjectOutputStream(f);
o.writeObject(map);
o.close();
} catch (FileNotFoundException i) {
System.out.println("file did not find");
i.printStackTrace();
}
catch (IOException ioException) {
System.out.println("Error");
ioException.printStackTrace();
}
}
/**
* Retrieves a HashMap of note names and their creation dates from the serialized file.
*
* @return A HashMap containing note names and their corresponding creation dates.
*/
static HashMap<String, Object> getDateHashmap() {
HashMap<String, Object> map = new HashMap<>();
try {
File file = new File("date.txt");
FileInputStream fileInputStream = new FileInputStream(file);
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
map = (HashMap<String, Object>) objectInputStream.readObject();
objectInputStream.close();
} catch (IOException ioException) {
ioException.printStackTrace();
} catch (ClassNotFoundException classNotFoundException) {
classNotFoundException.printStackTrace();
}
return map;
}
/**
* Prints the names of the files in a directory along with their creation dates.
*
* @param files An array of files to be printed.
*/
static void printNameOfFile(File[] files) {
HashMap<String, Object> dateFile = getDateHashmap();
File notesFolders = new File("Notes");
int i = 1;
if (files != null) {
for (File f: files) {
String name = f.getName();
int tmp = name.indexOf(".");
System.out.println((i) + "- " + name.substring(0, tmp) + "\t" + dateFile.get(name));
i++;
}
}
}
/**
* The main method that drives the note management application.
* Users can interact with the application through a menu-based interface.
*
* @param args Command-line arguments (not used).
*/
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
ArrayList<String> list = new ArrayList<>();
HashMap<String, String> date = new HashMap<>();
final int changeNumber = 13;
makeFolder();
File notesFolders = new File("Notes/");
int choice = 0;
outter: while (true) {
menu();
choice = scanner.nextInt();
if (choice == 1) { // add
System.out.println("Enter the name of file (enter 0 for leaving)");
String nameOfFile;
out: while (true) {
File[] fileNames = notesFolders.listFiles();
nameOfFile = scanner.next();
if (nameOfFile.equals("0")) {
continue outter;
}
if (fileNames != null) {
for (File f : fileNames) {
if (f.isFile()) {
if (f.getName().equals(nameOfFile + ".txt")) {
System.out.println("File exist please enter again");
continue out;
}
}
}
}
File newFile = new File("Notes/" + nameOfFile + ".txt");
try {
if (newFile.createNewFile()) {
System.out.println("file created");
saveDate(date, nameOfFile + ".txt");
}
break out;
}
catch (IOException e) {
System.out.println("file did not created");
e.printStackTrace();
}
}
try {
System.out.println("Enter your notes (for ending writin enter \'#\')");
FileWriter myWriter = new FileWriter("Notes/" + nameOfFile + ".txt");
scanner.nextLine();
StringBuilder stringBuilder = new StringBuilder();
while (true) {
String content = scanner.nextLine();
if (content.equals("#")) {
break;
}
stringBuilder.append(content);
stringBuilder.append("\n");
}
String str = "" + stringBuilder;
for (int j = 0; j < str.length(); j++) {
int tmp = str.charAt(j) + changeNumber;
myWriter.write(tmp);
}
myWriter.close();
System.out.println("Successfully wrote to the file.");
} catch (IOException e) {
System.out.println("An error occurred.");
e.printStackTrace();
}
} else if (choice == 2) {
File[] files = notesFolders.listFiles();
System.out.println("choose a note to delete (For leaving enter 0)");
printNameOfFile(files);
int t = scanner.nextInt();
if (t == 0) {
continue;
} else {
File file = new File("Notes/" + files[t - 1].getName());
if (file.delete()) {
System.out.println("file delete");
} else {
System.out.println("file did not deleted");
}
}
} else if (choice == 3) {
File[] files = notesFolders.listFiles();
printNameOfFile(files);
System.out.println("---------------------------------------------------");
System.out.println("Please choose the number of Note you want to see (For leaving enter 0)");
int ch = scanner.nextInt();
if (ch == 0) {
continue outter;
}
try {
FileReader fileReader = new FileReader(files[ch - 1]);
int j;
while ((j = fileReader.read()) != -1) {
System.out.print((char) (j - changeNumber));
}
fileReader.close();
}
catch (IOException e) {
e.printStackTrace();
}
} else if (choice == 4) {
File[] files = notesFolders.listFiles();
printNameOfFile(files);
System.out.println("---------------------------------------------------");
System.out.println("Please choose the number of Note you want to export (For leaving enter 0)");
int ch = scanner.nextInt();
if (ch == 0) {
continue outter;
}
try {
File target = new File("Export/" + files[ch - 1].getName());
File src = new File("Notes/" + files[ch - 1].getName());
Files.copy(src.toPath(), target.toPath());
} catch (IOException i) {
System.out.println("Cannot copy file");
i.printStackTrace();
}
System.out.println("The file exported");
System.out.println("You can find it in export folder now");
} else if (choice == 5) {
System.out.println("GOODBYE");
break;
} else {
System.out.println("Invalid Input");
}
}
}
}