forked from nus-cs2103-AY2223S1/ip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Deadline.java
53 lines (47 loc) · 1.68 KB
/
Deadline.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
package duke;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;
/**
* Represents a deadline, which is a specific type of task.
*
* @author Liu Han
*/
public class Deadline extends Task {
protected LocalDate by;
private static String printFormat = "MMM d yyyy";
private static String saveFormat = "yyyy-MM-dd";
/**
* Deadline Constructor
* @param description Description of the deadline.
* @param by Date of the deadline due time.
* @throws DukeException If date is in an invalid format.
*/
public Deadline(String description, String by) throws DukeException{
super(description);
try {
this.by = LocalDate.parse(by);
} catch (DateTimeParseException err) {
throw new DukeException("The date format should be: yyyy-MM-dd");
}
// this.by = LocalDate.parse(by, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
}
/**
* Prints the deadline when list is called by the user
* @return String in the format <b>[D][isDone] description (by: MMM d yyyy)</b>.
*/
@Override
public String toString() {
return "[D]" + super.toString() + " (by: " +
this.by.format(DateTimeFormatter.ofPattern(printFormat)) + ")";
}
/**
* Saves the deadline into a string and writes into a .txt file.
* @return String in the format <b>D | 1/0 | description | yyyy-MM-dd</b>.
*/
@Override
public String toSave() {
return "D | " + (this.isDone ? "1 | " : "0 | ") + this.getDescription() +
" | " + this.by.format(DateTimeFormatter.ofPattern(saveFormat)) + "\n";
}
}