-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTask.java
More file actions
165 lines (152 loc) · 4.22 KB
/
Task.java
File metadata and controls
165 lines (152 loc) · 4.22 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
import java.util.*;
import java.io.*;
import java.net.*;
class List implements Comparable<List> {
String title, priority;
public List(String p, String t) {
priority=p;
title=t;
}
public int compareTo(List l)
{
if((this.priority.compareTo(l.priority))>0)
return 1;
else
return -1;
}
}
public class Task {
public static ArrayList<List> tasks = new ArrayList<List>();
public static ArrayList<List> complete = new ArrayList<List>();
public static void main(String args[]) throws FileNotFoundException {
readFile();
writeFile();
String s="";
if(args.length>0)
s=args[0];
switch(s) {
case "ls": display();
break;
case "add":
try {
add(args[1], args[2]);
}
catch(Exception e) {
System.out.println("Please add both priority and title at the end of add");
}
break;
case "del":
try {
delete(Integer.parseInt(args[1]));
}
catch(Exception e) {
System.out.println("Please add index at the end of del");
}
break;
case "done":
try {
done(Integer.parseInt(args[1]));
}
catch(Exception e) {
System.out.println("Please add index at the end of done");
}
break;
case "report": report();
break;
default: System.out.println("Usage :-\n$ ./task add 2 hello world # Add a new item with priority 2 and text \"hello world\" to the list\n$ ./task ls # Show incomplete priority list items sorted by priority in ascending order\n$ ./task del INDEX # Delete the incomplete item with the given index\n$ ./task done INDEX # Mark the incomplete item with the given index as complete\n$ ./task help # Show usage\n$ ./task report # Statistics");
}
}
public static void display() {
int i=0;
for (List list: tasks)
{
System.out.println((++i)+". "+list.title+" ["+list.priority+"]");
}
}
public static void add(String p, String t) throws FileNotFoundException {
tasks.add(new List(p,t));
System.out.println("Added task: \""+t+"\" with priority "+p);
writeFile();
}
public static void delete(int i) throws FileNotFoundException {
if(i>tasks.size())
System.out.println("Error: item with index "+i+" does not exist. Nothing deleted.");
else {
tasks.remove(i-1);
System.out.println("Deleted item with index "+i);
}
writeFile();
}
public static void done(int i) throws FileNotFoundException {
if(i>tasks.size())
System.out.println("Error: no incomplete item with index "+i+" exist.");
else {
complete.add(tasks.get(i-1));
tasks.remove(i-1);
System.out.println("Marked item is done");
}
writeFile();
}
public static void report() {
int i=0,j=0;
System.out.println("Pending : "+tasks.size());
for (List list: tasks)
{
System.out.println((++i)+". "+list.title+" ["+list.priority+"]");
}
System.out.println("Completed : "+complete.size());
for (List list: complete)
{
System.out.println((++j)+". "+list.title);
}
}
public static void readFile() throws FileNotFoundException {
URL url1 = Task.class.getResource("pending.txt");
File pending = new File(url1.getPath());
URL url2 = Task.class.getResource("completed.txt");
File completed = new File(url2.getPath());
Scanner sc = new Scanner(pending);
Scanner sn = new Scanner(completed);
try {
while(sc.hasNextLine()) {
tasks.add(new List(sc.next(), sc.next()));
}
}
catch(NoSuchElementException e) {
}
try {
while(sn.hasNextLine()) {
complete.add(new List(sn.next(), sn.next()));
}
}
catch(NoSuchElementException e) {
}
sc.close();
sn.close();
}
public static void writeFile() throws FileNotFoundException {
URL url1 = Task.class.getResource("pending.txt");
File pending = new File(url1.getPath());
URL url2 = Task.class.getResource("completed.txt");
File completed = new File(url2.getPath());
PrintWriter p1 = new PrintWriter(pending);
PrintWriter p2 = new PrintWriter(completed);
p1.close();
p2.close();
p1 = new PrintWriter(pending);
p2 = new PrintWriter(completed);
Collections.sort(tasks);
for (List list: tasks)
{
p1.write(list.priority+" "+list.title);
p1.println();
}
for (List list: complete)
{
p2.write(list.priority+" "+list.title);
p2.println();
}
p1.close();
p2.close();
}
}