-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathkaam.java
More file actions
296 lines (246 loc) · 8.91 KB
/
Copy pathkaam.java
File metadata and controls
296 lines (246 loc) · 8.91 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
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Array;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
public class kaam {
public static String fileSelector() {
System.out.println("Choose the file : ");
String filepath;
// getting all the files in the db directory with the count and index
File db = new File("./db");
File[] dbFiles = db.listFiles();
System.out.println("----------------List of Files---------------");
int i = 0;
for (File dbfile : dbFiles) {
System.out.println((i + 1) + ". " + dbfile.getName());
i++;
}
System.out.println("--------------------------------------------");
Scanner sc = new Scanner(System.in);
System.out.print("Choose the Todo file number : ");
int count = sc.nextInt();
filepath = "./db/" + dbFiles[count - 1].getName();
return filepath;
}
public static String getFilePathFromList() {
String todayFilename = java.time.LocalDate.now().toString() + ".tasks";
String filepath = "./db/" + todayFilename;
// checking if file exists
File todayfile = new File(filepath);
if (!(todayfile.exists() && todayfile.isFile())) {
filepath = fileSelector();
}
return filepath;
}
public static String getFilePathFromAdd() {
String todayFilename = java.time.LocalDate.now().toString() + ".tasks";
String filepath = "./db/" + todayFilename;
return filepath;
}
public static String getFilePath() {
String todayFilename = java.time.LocalDate.now().toString() + ".tasks";
String filepath = "./db/" + todayFilename;
// checking if file exists
File todayfile = new File(filepath);
if (!(todayfile.exists() && todayfile.isFile())) {
filepath = fileSelector();
}
return filepath;
}
public static void listTodo(String filepath) {
// fetching the tasks from file
String todayFilename = filepath;
File lfile = new File(todayFilename);
if (!lfile.exists()) {
System.out.println("Koi Kaam nahi hai Aaj.");
return;
}
try {
FileReader reader = new FileReader(todayFilename);
List<String> ltasks = new ArrayList<String>();
try {
ltasks = reader.readAllLines();
// presenting it clearly
System.out.println(
"-----------------------kaam-------------------------"
);
int count = 0;
for (String task : ltasks) {
count++;
System.out.println(count + ". " + task);
}
System.out.println(
"----------------------------------------------------"
);
reader.close();
} catch (IOException e) {
System.out.println(
"Unable to read tasks : \n" + e.getMessage()
);
}
} catch (FileNotFoundException e) {
System.out.println("[ERROR] : " + e.getMessage());
}
}
public static void listTodo() {
// fetching the tasks from file
String todayFilename = getFilePathFromList();
File lfile = new File(todayFilename);
if (!lfile.exists()) {
System.out.println("Koi Kaam nahi hai Aaj.");
return;
}
try {
FileReader reader = new FileReader(todayFilename);
List<String> ltasks = new ArrayList<String>();
try {
ltasks = reader.readAllLines();
// presenting it clearly
System.out.println(
"-----------------------kaam-------------------------"
);
int count = 0;
for (String task : ltasks) {
count++;
if (task.startsWith("X ")) {
System.out.println("[X] " + task.substring(2));
} else {
System.out.println(count + ". " + task);
}
}
System.out.println(
"----------------------------------------------------"
);
reader.close();
} catch (IOException e) {
System.out.println(
"Unable to read tasks : \n" + e.getMessage()
);
}
} catch (FileNotFoundException e) {
System.out.println("[ERROR] : " + e.getMessage());
}
}
public static void addTodo(String[] commandsString) {
// adding the task to the JSON file
String[] tasks = Arrays.copyOfRange(
commandsString,
1,
commandsString.length
);
// checking if the file exists, if not create it
// the filename is <today's date>.tasks
String filename = getFilePathFromAdd();
File file = new File(filename);
if (!file.exists()) {
try {
FileWriter writer = new FileWriter(file);
// writing to it
for (String task : tasks) {
writer.write(task);
writer.write("\r\n");
}
writer.close();
} catch (IOException e) {
System.out.println(
"An error occurred while creating the file."
);
e.printStackTrace();
}
} else {
// if file exist then, append
try {
FileWriter writer = new FileWriter(file, true);
for (String task : tasks) {
writer.write(task);
writer.write("\r\n");
}
writer.close();
} catch (IOException e) {
System.out.println(
"Error occured while writing... : \n" + e.getMessage()
);
}
}
}
public static void completeTodo(String[] commandsString) {
String[] todoNums = Arrays.copyOfRange(
commandsString,
1,
commandsString.length
);
String filepath = getFilePathFromList();
try {
File taskFile = new File(filepath);
FileReader reader = new FileReader(taskFile);
List<String> tasks = new ArrayList<String>(reader.readAllLines());
for(int i = 0; i < todoNums.length; i++) {
int taskNum = Integer.parseInt(todoNums[i]) - 1;
String task = tasks.get(taskNum);
String completedTask = "X " + task;
System.out.println("Modified string : " + completedTask);
tasks.set(taskNum, completedTask);
}
FileWriter writer = new FileWriter(taskFile);
for(String task : tasks) {
writer.write(task);
writer.write("\r\n");
}
reader.close();
writer.close();
} catch(IOException e) {
System.out.println("[Error] : " + e.getMessage());
}
}
public static void executeCommand(String[] commandsString) {
// This is a placeholder for the actual command execution logic
if (commandsString.length == 0) {
System.out.println("No commands to execute.");
return;
}
String first_command = commandsString[0];
switch (first_command) {
case "list":
case "l":
listTodo();
break;
case "add":
case "a":
addTodo(commandsString);
break;
case "s":
case "select":
String filepath = fileSelector();
listTodo(filepath);
break;
case "c":
case "complete":
completeTodo(commandsString);
break;
case "exit":
// exit command: terminate the program
System.out.println("Exiting the program.");
System.exit(0);
break;
default:
System.out.println("Unknown command: " + first_command);
}
}
public static void main(String[] args) {
// checking and limiting the arguments length to 4
if (args.length > 4) {
System.out.println(
"[ERROR] : too long, it should be less than 4 arguments"
);
System.exit(1);
}
//System.out.println("these are the commands: " + Arrays.toString(args));
executeCommand(args);
}
}