Repository navigation
Expand file tree
/
Copy pathMain.java
More file actions
327 lines (298 loc) · 9.72 KB
/
Copy pathMain.java
File metadata and controls
327 lines (298 loc) · 9.72 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
import java.util.ArrayList; // Import the ArrayList class for dynamically resizable lists
import java.util.Scanner; // Import the Scanner class for user input
import java.io.File; // Import the File class for file handling
import java.io.FileWriter; // Import the FileWriter class for file writing
import java.io.IOException; // Import the IOException class to handle input/output errors
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
public class Main {
static Scanner inputScanner = new Scanner(System.in); // module for user input
static ArrayList<String> tasks = new ArrayList<String>();
static ArrayList<Boolean> done = new ArrayList<Boolean>();
static ArrayList<Boolean> important = new ArrayList<Boolean>();
static ArrayList<String> lists = new ArrayList<String>();
static String input;
static String[] splitInput;
static String currentFilename = "todo";
// Color constants
static final String RESET = "\u001B[0m";
static final String RED = "\u001B[31m";
static final String GREEN = "\u001B[32m";
static final String YELLOW = "\u001B[33m";
static final String BLUE = "\u001B[34m";
static final String MAGENTA = "\u001B[35m";
static final String CYAN = "\u001B[36m";
static final String WHITE = "\u001B[37m";
static final String BOLD = "\u001B[1m";
static final String CROSSED_OUT = "\u001B[9m";
static final String CLS = "\033[H\033[2J";
// LEVEL 0
public static void main(String[] args) {
splashScreen();
decodeCommandLineArguments(args);
lists = convertStringToStringArrayList(loadData("_lists"));
loadList(currentFilename);
while (true) {
AsciiUi();
input = inputScanner.nextLine(); // Get user imput
processInput(input);
saveList(currentFilename);
}
}
// LEVEL 1
static void splashScreen() {
System.out.print(CLS);
System.out.println("###################\n# #\n# MY TO-DO LIST #\n# #\n###################\n"); // stylized title
System.out.println("Momie_et_Masque first Java project !\nType /help to get help.");
try {
Thread.sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
static void decodeCommandLineArguments(String[] args) {
if (args.length > 0) {
currentFilename = args[0];
}
}
static void AsciiUi() {
System.out.print(CLS);
System.out.println("###################\n# #\n# MY TO-DO LIST #\n# #\n###################\n"); // stylized title
System.out.println("List : " + currentFilename);
for (int i = 0; i < tasks.size(); i++) {
System.out.print(i + 1 + " ");
if (done.get(i)) {
System.out.print("[X] " + CROSSED_OUT);
} else {
System.out.print("[ ] " + BOLD);
}
if (important.get(i)) {
System.out.print(RED);
}
System.out.println(tasks.get(i) + RESET);
}
System.out.print("\n > ");
}
// LEVEL 2
static void processInput(String input) {
if (input.startsWith("/")){
try {
processInputCommand(input);
} catch (Exception e) {
appendTask(input);
}
} else {
appendTask(input);
}
}
// LEVEL 3
static void processInputCommand(String input) {
splitInput = input.split(" ");
switch (splitInput[0]) {
case "/add":
appendTask(lastArg(1));
break;
case "/insert":
insertTask(splitInput[1], lastArg(2));
break;
case "/del":
deleteTask(splitInput[1]);
break;
case "/edit":
editTask(splitInput[1], lastArg(2));
break;
case "/done":
doneTask(splitInput[1]);
break;
case "/!":
importantTask(splitInput[1]);
break;
case "/clear":
clearTasks();
break;
case "/save":
saveList(lastArg(1).replace("_", "-"));
break;
case "/load":
loadList(lastArg(1).replace("_", "-"));
break;
case "/help":
printHelp();
break;
case "/lists":
printLists();
break;
case "/exit":
exitProgram();
break;
default:
appendTask(input);
}
}
// LEVEL 4
// get the last user command argument, which may contain spaces, from its index
static String lastArg(int index) {
int startIndex = 0;
for (int i = 0; i < index; i++) {
startIndex += splitInput[i].length() + 1;
}
return input.substring(startIndex);
}
static void appendTask(String task) {
tasks.add(task);
done.add(false);
important.add(false);
}
static void insertTask(String indexStr, String task) {
int index = Integer.parseInt(indexStr) - 1;
tasks.add(index, task);
done.add(index, false);
important.add(index, false);
}
static void editTask(String indexStr, String newTask) {
int index = Integer.parseInt(indexStr);
tasks.set(index - 1, newTask);
}
static void deleteTask(String indexStr) {
int index = Integer.parseInt(indexStr);
tasks.remove(index - 1);
done.remove(index - 1);
important.remove(index - 1);
}
static void doneTask(String indexStr) {
int index = Integer.parseInt(indexStr) - 1;
if (done.get(index)) {
done.set(index, false);
} else {
done.set(index, true);
}
}
static void importantTask(String indexStr) {
int index = Integer.parseInt(indexStr) - 1;
if (important.get(index)) {
important.set(index, false);
} else {
important.set(index, true);
}
}
static void clearTasks() {
tasks.clear();
done.clear();
important.clear();
}
static void saveList(String filename) {
currentFilename = filename;
String tasksString = convertStringArrayListToString(tasks);
saveData(currentFilename, tasksString);
saveData(currentFilename + "_done", convertBooleanArrayListToString(done));
saveData(currentFilename + "_important", convertBooleanArrayListToString(important));
if (!lists.contains(currentFilename)) {
lists.add(currentFilename);
saveData("_lists", convertStringArrayListToString(lists));
}
if (tasksString == "") {
lists.remove(lists.indexOf(currentFilename));
saveData("_lists", convertStringArrayListToString(lists));
}
}
static void loadList(String filename) {
currentFilename = filename;
tasks = convertStringToStringArrayList(loadData(currentFilename));
done = convertStringToBooleanArrayList(loadData(currentFilename + "_done"));
important = convertStringToBooleanArrayList(loadData(currentFilename + "_important"));
}
static void printHelp() {
System.out.println("\nHELP :\nYou can type a new task to add it to the list, or type a command :");
System.out.println("/add <task> - Add a new task");
System.out.println("/insert <index> <task> - Insert a new task at a given index");
System.out.println("/del <index> - Delete a task at a given index");
System.out.println("/edit <index> <task> - Edit a task at a given index");
System.out.println("/done <index> - Mark a task as done");
System.out.println("/! <index> - Mark a task as important");
System.out.println("/clear - Clear all tasks");
System.out.println("/save <filename> - Save the current list to a file");
System.out.println("/load <filename> - Load a list from a file");
System.out.println("/help - Print this help");
System.out.println("/lists - Print existing to-do lists names");
System.out.println("/exit - Exit the program");
System.out.println("\nPress enter to close this help");
inputScanner.nextLine();
}
static void printLists() {
System.out.println("\nTO-DO LISTS :\n");
for (String list : lists) {
System.out.println(list);
}
System.out.println("\nPress enter to close this listing");
inputScanner.nextLine();
}
static void exitProgram() {
System.out.print(CLS);
System.exit(0);
}
// LEVEL 5
static void saveData(String filename, String data) {
try {
File dataFile = new File(filename + ".txt");
if (data == "") {
dataFile.delete();
} else {
dataFile.createNewFile();
FileWriter myWriter = new FileWriter(filename + ".txt");
myWriter.write(data);
myWriter.close();
}
} catch (IOException e) {}
}
static String loadData(String filename) {
String content = "";
try {
Path path = Paths.get(filename + ".txt");
content = Files.readString(path);
} catch (IOException e) {}
return content;
}
static ArrayList<String> convertStringToStringArrayList(String input) {
String[] lines = input.split("\n");
ArrayList<String> list = new ArrayList<>();
if (lines.length > 1 || lines[0] != "") {
for (String line : lines) {
list.add(line);
}
}
return list;
}
static ArrayList<Boolean> convertStringToBooleanArrayList(String input) {
String[] lines = input.split("\n");
ArrayList<Boolean> list = new ArrayList<>();
if (lines.length > 1 || lines[0] != "") {
for (String line : lines) {
list.add(Boolean.parseBoolean(line));
}
}
return list;
}
static String convertStringArrayListToString(ArrayList<String> list) {
StringBuilder sb = new StringBuilder();
for (String item : list) {
sb.append(item).append("\n");
}
// Remove the last line break if needed
if (!list.isEmpty()) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
static String convertBooleanArrayListToString(ArrayList<Boolean> list) {
StringBuilder sb = new StringBuilder();
for (Boolean item : list) {
sb.append(item).append("\n");
}
// Remove the last line break if needed
if (!list.isEmpty()) {
sb.setLength(sb.length() - 1);
}
return sb.toString();
}
}