1+ import java .time .LocalDateTime ;
2+ import java .time .format .DateTimeFormatter ;
3+
4+ /**
5+ * Task class represents a single to-do item with title, category, completion status, and timestamp
6+ */
7+ public class Task {
8+ private String title ;
9+ private String category ;
10+ private boolean completed ;
11+ private String createdAt ;
12+ private String completedAt ;
13+
14+ // Constructor
15+ public Task (String title , String category ) {
16+ this .title = title ;
17+ this .category = category ;
18+ this .completed = false ;
19+ this .createdAt = LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" ));
20+ this .completedAt = null ;
21+ }
22+
23+ // Default constructor for Gson
24+ public Task () {}
25+
26+ // Getters
27+ public String getTitle () {
28+ return title ;
29+ }
30+
31+ public String getCategory () {
32+ return category ;
33+ }
34+
35+ public boolean isCompleted () {
36+ return completed ;
37+ }
38+
39+ public String getCreatedAt () {
40+ return createdAt ;
41+ }
42+
43+ public String getCompletedAt () {
44+ return completedAt ;
45+ }
46+
47+ // Setters
48+ public void setTitle (String title ) {
49+ this .title = title ;
50+ }
51+
52+ public void setCategory (String category ) {
53+ this .category = category ;
54+ }
55+
56+ public void setCompleted (boolean completed ) {
57+ this .completed = completed ;
58+ if (completed && this .completedAt == null ) {
59+ this .completedAt = LocalDateTime .now ().format (DateTimeFormatter .ofPattern ("yyyy-MM-dd HH:mm:ss" ));
60+ } else if (!completed ) {
61+ this .completedAt = null ;
62+ }
63+ }
64+
65+ public void setCreatedAt (String createdAt ) {
66+ this .createdAt = createdAt ;
67+ }
68+
69+ public void setCompletedAt (String completedAt ) {
70+ this .completedAt = completedAt ;
71+ }
72+
73+ // Mark task as complete
74+ public void markComplete () {
75+ setCompleted (true );
76+ }
77+
78+ // Mark task as incomplete
79+ public void markIncomplete () {
80+ setCompleted (false );
81+ }
82+
83+ // Get status emoji
84+ public String getStatusEmoji () {
85+ return completed ? "✅" : "⏳" ;
86+ }
87+
88+ // Get category emoji
89+ public String getCategoryEmoji () {
90+ switch (category .toLowerCase ()) {
91+ case "work" :
92+ return "💼" ;
93+ case "personal" :
94+ return "👤" ;
95+ case "urgent" :
96+ return "🚨" ;
97+ default :
98+ return "📝" ;
99+ }
100+ }
101+
102+ @ Override
103+ public String toString () {
104+ return String .format ("%s %s [%s%s] - %s (Created: %s)%s" ,
105+ getStatusEmoji (),
106+ title ,
107+ getCategoryEmoji (),
108+ category ,
109+ completed ? "COMPLETED" : "PENDING" ,
110+ createdAt ,
111+ completed && completedAt != null ? " (Completed: " + completedAt + ")" : ""
112+ );
113+ }
114+
115+ // Simple string representation for display
116+ public String toSimpleString () {
117+ return String .format ("%s %s [%s]" , getStatusEmoji (), title , category );
118+ }
119+ }
0 commit comments