forked from rysmith18/TaskManager
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Task.java
381 lines (334 loc) · 8.57 KB
/
Task.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
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
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
import java.util.Date;
import java.io.*;
import java.text.DateFormat;
import java.text.ParseException;
/**
*
* Task object containing information on a single task.
* @author Isabelle Schroeder
*
*/
public class Task implements Comparable<Task> {
static String input;
/**
* Set the description of the task.
* @param p
*/
public void setPriority( short p ){
if( p < 0 || p > 3){
throw new IllegalArgumentException( "Invalid priority" ); // priority can only be 0-3
}
priority = p; // setting priority
}
/**
*
* @return returns a string for the priority
*/
public String getPriorityName() {
String pName = "NAME";
switch( priority ){
case 0:
pName = "Undefined"; // so important you broke it
break;
case 1:
pName = "High"; // super important
break;
case 2:
pName = "Medium"; // meh importance
break;
case 3:
pName = "Low"; // practically nothing to worry about
break;
default:
pName = "ILLEGAL VALUE"; // WHAT HAVE YOU DONE?
break;
}
return pName; // returning importance string
}
/**
* write a task to the provided PrintWriter object
* @param writer
*/
public void write( PrintWriter writer ){
// write data separated by tabs
StringBuilder s = new StringBuilder(); // building a string to hold everything for a task
s.append( priority ); // writing the parts of our new little string
s.append( "\t" ); // tab
if( dueDate != null ){
s.append( dueDate.toString() ); // date is the black sheep of this family
}
s.append( "\t" );
s.append( category);
s.append( "\t" );
s.append( description );
s.append( "\t" );
s.append( location );
s.append( "\t" );
s.append( completed );
writer.println( s ); // printing the string of the task that we just filled out
}
/**
* Read a task from disk using the provided BufferedReader
* @param reader BufferedReadrer to read from disk
* @return read task or null if not read
*/
public void read( BufferedReader reader ){
String line = null;
String [] results;
try{
line = reader.readLine(); // reading the line to disk
} catch( IOException e ){
System.out.println( " Cannot read file: " + e.getMessage() ); // sadness
}
results = line.split( "\t" ); // splitting the String line everywhere there is a tab
// prints out the results until the length of results ends
for( int ctr = 0; ctr < results.length; ctr++ ){
System.out.println( "DBG: results[ " + ctr + "]: \"" + results[ctr] + "\"" );
}
// priority set from data stored in position 0 of results
setPriority( Short.parseShort( results[0] ) );
try{ // sets date with data stored in position 1 of results
setDate( DateFormat.getDateInstance().parse( results[1] ) );
} catch( ParseException e ) { // nothing to do here, move along...
System.out.println( "Could not parse the date. Setting to null." );
}
// sets category with results position 2 data
setCategory( Short.parseShort( results[2] ) );
// setting description with..data from position 3 from the result String array list
setDescription( results[3] );
// sets location with data from result position 4
setLocation( results[4] );
// finally sets completed status with results data from position 5
setCompleted( Boolean.parseBoolean( results[5] ) );
}
/**
* Saving user's information in one String and then returning that String
* @return returns a string
*/
public String toString() {
StringBuilder s = new StringBuilder();
// appending everything into the String s
s.append( "Description: " + description + "\n" );
s.append( "Priority: " + getPriorityName() + "\n" );
s.append( "Category: " + getCategoryName() + "\n" );
// appending date when it is not null
if( dueDate != null ) {
s.append( "Due Date: " + dueDate.toString() + "\n" );
}
s.append( "Location: " + location + "\n" );
s.append( "Completed? " + (completed?"Y":"N") + "\n" );
return s.toString();
}
/**
* Finds the desired category name and returns it.
* @return returns the string name for the category chosen by the user
*/
public String getCategoryName() {
String cName = "NAME";
switch( category ){
case 0:
cName = "Undefined";
break;
case 1:
cName = "Other";
break;
case 2:
cName = "School";
break;
case 3:
cName = "Personal";
break;
case 4:
cName = "Chore";
break;
case 5:
cName = "Work";
break;
default:
cName = "ILLEGAL VALUE";
}
return cName;
}
/**
* Setting the date!
* @param date takes a date parameter
*/
public void setDate( Date date ){
dueDate = date;
}
/**
* Setting the category!
* @param cat takes a short parameter
*/
public void setCategory( short cat ){
if( cat < 0 || cat > 5){
throw new IllegalArgumentException( "Invalid category" );
}
category = cat;
}
/**
* Setting the description!
* @param d takes a string
*/
public void setDescription( String d ){
description = d;
}
/**
* Setting location!
* @param l takes a string
*/
public void setLocation( String l ){
location = l;
}
/**
* Setting completed status!
* @param c takes a boolean
*/
public void setCompleted( boolean c){
completed = c;
}
// now for the getters
/**
* Getting the priority!
* @return returns a short for priority
*/
public short getPriority(){
return priority;
}
/**
* Getting the date!
* @return returns a date
*/
public Date getDate(){
return dueDate;
}
/**
* Getting the category!
* @return returns a short
*/
public short getCategory(){
return category;
}
/**
* Getting description!
* @return returns a string
*/
public String getDescription( ){
return description;
}
/**
* Getting location!
* @return returns a string
*/
public String getLocation( ){
return location;
}
/**
* Getting the completed status!
* @return returns a boolean
*/
public boolean getCompleted( ){
return completed;
}
// these are for the getting and setting of all those parts of a task
private short priority;
private Date dueDate;
private short category;
private String description;
private String location;
private boolean completed;
// priorities
public static final short PRIO_HIGH = 1;
public static final short PRIO_MED = 2;
public static final short PRIO_LOW = 3;
public static final short PRIO_UNDEF = 0;
/**
* @return returns a short
*/
public static short PrioH( ){
return PRIO_HIGH;
}
/**
*
* @return returns a short also
*/
public static short PrioM( ){
return PRIO_MED;
}
/**
*
* @return all these return a short (since they are all returning priority)
*/
public static short PrioL( ){
return PRIO_LOW;
}
/**
*
* @return returns a short
*/
public static short PrioU( ){
return PRIO_UNDEF;
}
// categories
public static final short CAT_UNDEF = 0;
public static final short CAT_OTHER = 1;
public static final short CAT_SCHOOL = 2;
public static final short CAT_PERSONAL = 3;
public static final short CAT_CHORE = 4;
public static final short CAT_WORK = 5;
// Categories now!!
/**
*
* @return returns a short
*/
public static short CatU( ){
return CAT_UNDEF;
}
/**
* I'm seeing a pattern here...
* @return returns a short
*/
public static short CatO( ){
return CAT_OTHER;
}
/**
*
* @return returns a short
*/
public static short CatS( ){
return CAT_SCHOOL;
}
/**
*
* @return returns a short
*/
public static short CatP( ){
return CAT_PERSONAL;
}
/**
* @return returns a short
*/
public static short CatC( ){
return CAT_CHORE;
}
/**
*
* @return returns a string..just kidding, of course it's a short
*/
public static short CatW( ){
return CAT_WORK;
}
/**
* compareTo returns a positive, negative, or zero value.
* zero -- priorities of the tasks are the same
* positive int -- priority of my task is greater than priority of your task
* negative int -- priority of my task is less than priority of your task
*/
@Override // implementing custom version for this class as opposed to the method from the parent
public int compareTo(Task arg0) {
short myPriority = this.getPriority(); // using this because calling task from task
short yourPriority = arg0.getPriority(); // other task for comparing my task's priority
int result = myPriority - yourPriority; // coolness B)
return result; // returning the positive, negative, or zero int result
}
}