-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtodo.java
More file actions
94 lines (76 loc) · 1.99 KB
/
todo.java
File metadata and controls
94 lines (76 loc) · 1.99 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
import java.io.*;
import java.util.ArrayList;
import java.util.Scanner;
class todo {
static void Help() {
System.out.println("$ ./todo help\n" +
"Usage :-\n" +
"$ ./todo add \"todo item\" # Add a new todo\n" +
"$ ./todo ls # Show remaining todos\n" +
"$ ./todo del NUMBER # Delete a todo\n" +
"$ ./todo done NUMBER # Complete a todo\n" +
"$ ./todo help # Show usage\n" +
"$ ./todo report # Statistics\n");
}
static void Ls() {
// String[] todos = new String[0];
// Scanner sc = new Scanner(System.in);
// for (int i = 0; i < todos.length; i++) {
// todos[i] = sc.nextLine();
// System.out.println(todos[i]);
// }
}
static void CreateFile(){
try
{
File myObj = new File("todo.txt");
if (myObj.createNewFile()) {
System.out.println("File created: " + myObj.getName());
}
} catch(
IOException e)
{
System.out.println("An error occurred.");
e.printStackTrace();
}
}
public static void appendStrToFile(String fileName,String str)
{
try{
BufferedWriter out = new BufferedWriter(
new FileWriter(fileName, true));
out.write(str);
System.out.println("Added todo: " +str);
out.close();
}
catch (IOException e){
System.out.println("Exection occured" + e);
}
}
public static ArrayList<String> main(String[] args) throws FileNotFoundException {
// Part 1
if(args.length == 0){
Help();
}
if(args.length == 1 && args[0].equals("help") ){
Help();
}
if(args.length == 2 && args[0].equals("add")){
CreateFile();
appendStrToFile("todo.txt", args[1] + "\n");
}
if(args.length == 1 && args[0].equals("add")){
System.out.println("Error: Missing todo string. Nothing added!");
}
if((args.length == 1) && args[0].equals("ls")){
ArrayList<String> result = new ArrayList<>();
try (Scanner s = new Scanner(new FileReader("todo.txt"))){
while (s.hasNext())
{
result.add(s.nextLine());
}
System.out.println(return);
}
}
}
}