Skip to content

Commit 6a0f169

Browse files
Merge branch 'branch-C-Update'
2 parents 54c0c0e + 15a3301 commit 6a0f169

File tree

8 files changed

+212
-18
lines changed

8 files changed

+212
-18
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package helperbot.command;
2+
3+
import java.util.Arrays;
4+
5+
import helperbot.exception.HelperBotArgumentException;
6+
import helperbot.storage.Storage;
7+
import helperbot.task.Task;
8+
import helperbot.task.TaskList;
9+
import helperbot.ui.Response;
10+
11+
/**
12+
* Represents a command that update the content of a <code>Task</code>.
13+
*/
14+
public class UpdateCommand extends Command {
15+
16+
private final String[] splitMessages;
17+
18+
public UpdateCommand(String[] splitMessages) {
19+
this.splitMessages = splitMessages;
20+
}
21+
22+
@Override
23+
public String execute(TaskList tasks, Storage storage, Response response) {
24+
int taskIndex = -1;
25+
try {
26+
taskIndex = Integer.parseInt(splitMessages[1]);
27+
Task task = tasks.get(taskIndex - 1).update(
28+
String.join(" ",
29+
Arrays.copyOfRange(this.splitMessages, 2, this.splitMessages.length)).trim());
30+
return response.getUpdateOutput(taskIndex, task);
31+
} catch (IndexOutOfBoundsException e) {
32+
return response.getErrorMessage("Invalid Argument: " + (this.splitMessages.length == 1
33+
? "Task index is not provided"
34+
: "Task " + taskIndex + " is not found."));
35+
} catch (NumberFormatException e) {
36+
return response.getErrorMessage("Invalid Argument: " + this.splitMessages[1]
37+
+ " cannot be parsed as an integer.");
38+
} catch (HelperBotArgumentException e) {
39+
return response.getErrorMessage(e.toString());
40+
}
41+
}
42+
}

src/main/java/helperbot/parser/Parser.java

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import helperbot.command.FindCommand;
1010
import helperbot.command.ListCommand;
1111
import helperbot.command.MarkCommand;
12+
import helperbot.command.UpdateCommand;
1213
import helperbot.exception.HelperBotCommandException;
1314

1415
/**
@@ -24,20 +25,21 @@ public class Parser {
2425
*/
2526
@SuppressWarnings("checkstyle:Indentation")
2627
public static Command parse(String message) throws HelperBotCommandException {
27-
String[] splitMessage = message.split(" ");
28-
String command = splitMessage[0].toLowerCase();
28+
String[] splitMessages = message.split(" ");
29+
String command = splitMessages[0].toLowerCase();
2930

3031
return switch (command) {
3132
case "bye" -> new ExitCommand();
32-
case "check" -> new CheckCommand(splitMessage);
33-
case "delete" -> new DeleteCommand(splitMessage);
33+
case "check" -> new CheckCommand(splitMessages);
34+
case "delete" -> new DeleteCommand(splitMessages);
3435
case "deadline", "event", "todo" -> new AddCommand(command, message);
3536
case "find" -> new FindCommand(message);
3637
case "list" -> new ListCommand();
37-
case "mark" -> new MarkCommand(splitMessage, true);
38-
case "unmark" -> new MarkCommand(splitMessage, false);
38+
case "mark" -> new MarkCommand(splitMessages, true);
39+
case "unmark" -> new MarkCommand(splitMessages, false);
40+
case "update" -> new UpdateCommand(splitMessages);
3941
// invalid HelperBot Command
40-
default -> throw new HelperBotCommandException(splitMessage[0] + " is not found.");
42+
default -> throw new HelperBotCommandException(splitMessages[0] + " is not found.");
4143
};
4244
}
4345
}

src/main/java/helperbot/storage/data/HelperBot.txt

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
T,0,read book
2-
D,1,return book,2025-08-27,21:30
1+
T,0,read story book
2+
D,1,return book,2028-08-27,21:30
33
D,0,ca1,2025-09-12,
4-
E,0,ca2,2025-09-12,22:10,2025-10-12,22:10
4+
E,0,ca2,2025-09-09,,2025-10-10,
55
E,0,ca2,2025-09-12,,2025-10-12,
66
E,1,ca2,2025-09-12,,2025-10-12,13:10
77
E,1,ca2,2025-08-12,22:10,2025-08-12,

src/main/java/helperbot/task/Deadline.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@
1313
*/
1414
public class Deadline extends Task {
1515

16-
private final LocalDate byDate;
17-
private final LocalTime byTime;
16+
private LocalDate byDate;
17+
private LocalTime byTime;
1818

1919
/**
2020
* Generates a <code>Deadline</code>
@@ -101,6 +101,30 @@ public String toString() {
101101
+ ")";
102102
}
103103

104+
@Override
105+
public Task update(String message) throws HelperBotArgumentException {
106+
int byIndex = message.indexOf("/by ");
107+
if (byIndex == -1) {
108+
this.setDescription(message);
109+
} else {
110+
if (byIndex != 0) {
111+
this.setDescription(message.substring(0, byIndex));
112+
}
113+
try {
114+
String dateTime = message.substring(byIndex + 4).trim();
115+
this.byDate = LocalDate.parse(dateTime.substring(0, 10));
116+
this.byTime = null;
117+
String time = dateTime.substring(10).trim();
118+
if (!time.isEmpty()) {
119+
byTime = LocalTime.parse(time);
120+
}
121+
} catch (DateTimeParseException e) {
122+
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /by");
123+
}
124+
}
125+
return this;
126+
}
127+
104128
private static Deadline getDeadline(String[] message) throws HelperBotFileException {
105129
LocalDate byDate = LocalDate.parse(message[3]);
106130
LocalTime byTime = null;

src/main/java/helperbot/task/Event.java

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,10 @@
1313
*/
1414
public class Event extends Task {
1515

16-
private final LocalDate fromDate;
17-
private final LocalTime fromTime;
18-
private final LocalDate toDate;
19-
private final LocalTime toTime;
16+
private LocalDate fromDate;
17+
private LocalTime fromTime;
18+
private LocalDate toDate;
19+
private LocalTime toTime;
2020

2121
/**
2222
* Generate a <code>Event</code>
@@ -121,6 +121,95 @@ public String toString() {
121121
+ ")";
122122
}
123123

124+
@Override
125+
public Task update(String message) throws HelperBotArgumentException {
126+
int fromIndex = message.indexOf("/from ");
127+
int toIndex = message.indexOf("/to ");
128+
if (fromIndex == -1 && toIndex == -1) {
129+
this.setDescription(message);
130+
} else if (fromIndex == -1) {
131+
updateWithoutFromDateTime(message, toIndex);
132+
} else if (toIndex == -1) {
133+
updateWithoutToDateTime(message, fromIndex);
134+
} else {
135+
updateFromAndToDateTime(message, fromIndex, toIndex);
136+
}
137+
return this;
138+
}
139+
140+
private void updateWithoutToDateTime(String message, int fromIndex) throws HelperBotArgumentException {
141+
if (fromIndex > 0) {
142+
this.setDescription(message.substring(0, fromIndex));
143+
}
144+
try {
145+
/// Ignore "/from ", which are 6 characters.
146+
String fromDateTime = message.substring(fromIndex + 6).trim();
147+
/// Date should be in the format of YYYY-MM-DD, thus the first 10 chars refer date.
148+
/// The rest of the substring should refer to the time (if present).
149+
LocalDate fromDate = LocalDate.parse(fromDateTime.substring(0, 10));
150+
LocalTime fromTime = Event.getTime(fromDateTime.substring(10).trim());
151+
if (Event.isToBeforeFrom(fromDate, fromTime, this.toDate, this.toTime)) {
152+
throw new HelperBotArgumentException("From datetime must be before to datetime");
153+
}
154+
this.fromDate = fromDate;
155+
this.fromTime = fromTime;
156+
} catch (IndexOutOfBoundsException e) {
157+
throw new HelperBotArgumentException("Wrong format for Event");
158+
} catch (DateTimeParseException e) {
159+
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /from and /to");
160+
}
161+
}
162+
163+
private void updateWithoutFromDateTime(String message, int toIndex) throws HelperBotArgumentException {
164+
if (toIndex > 0) {
165+
this.setDescription(message.substring(0, toIndex));
166+
}
167+
try {
168+
/// Ignore "/to ", which are 4 characters.
169+
String toDateTime = message.substring(toIndex + 4).trim();
170+
/// Date should be in the format of YYYY-MM-DD, thus the first 10 chars refer date.
171+
/// The rest of the substring should refer to the time (if present).
172+
LocalDate toDate = LocalDate.parse(toDateTime.substring(0, 10));
173+
LocalTime toTime = Event.getTime(toDateTime.substring(10).trim());
174+
if (Event.isToBeforeFrom(this.fromDate, this.fromTime, toDate, toTime)) {
175+
throw new HelperBotArgumentException("From datetime must be before to datetime");
176+
}
177+
this.toDate = toDate;
178+
this.toTime = toTime;
179+
} catch (IndexOutOfBoundsException e) {
180+
throw new HelperBotArgumentException("Wrong format for Event");
181+
} catch (DateTimeParseException e) {
182+
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /from and /to");
183+
}
184+
}
185+
186+
private void updateFromAndToDateTime(String message, int fromIndex, int toIndex) throws HelperBotArgumentException {
187+
if (fromIndex > toIndex) {
188+
throw new HelperBotArgumentException("Please enter /from before entering /to");
189+
}
190+
if (fromIndex > 0) {
191+
this.setDescription(message.substring(0, fromIndex));
192+
}
193+
try {
194+
/// Ignore "/from ", which are 6 characters.
195+
String fromDateTime = message.substring(fromIndex + 6, toIndex).trim();
196+
/// Date should be in the format of YYYY-MM-DD, thus the first 10 chars refer date.
197+
/// The rest of the substring should refer to the time (if present).
198+
this.fromDate = LocalDate.parse(fromDateTime.substring(0, 10));
199+
this.fromTime = Event.getTime(fromDateTime.substring(10).trim());
200+
String toDateTime = message.substring(toIndex + 4).trim();
201+
this.toDate = LocalDate.parse(toDateTime.substring(0, 10));
202+
this.toTime = Event.getTime(toDateTime.substring(10).trim());
203+
if (Event.isToBeforeFrom(fromDate, fromTime, toDate, toTime)) {
204+
throw new HelperBotArgumentException("From datetime must be before to datetime");
205+
}
206+
} catch (IndexOutOfBoundsException e) {
207+
throw new HelperBotArgumentException("Wrong format for Event");
208+
} catch (DateTimeParseException e) {
209+
throw new HelperBotArgumentException("Please enter date and time in YYYY-MM-DD hh:mm after /from and /to");
210+
}
211+
}
212+
124213
private static Event getEvent(String[] message) throws HelperBotFileException {
125214
LocalDate fromDate = LocalDate.parse(message[3]);
126215
LocalDate toDate = LocalDate.parse(message[5]);

src/main/java/helperbot/task/Task.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,14 @@
22

33
import java.time.LocalDate;
44

5+
import helperbot.exception.HelperBotArgumentException;
56
import helperbot.exception.HelperBotFileException;
67

78
/**
89
* Represents a task in <b>HelperBot</b>.
910
*/
1011
public class Task {
11-
private final String description;
12+
private String description;
1213
private boolean isDone;
1314

1415
/**
@@ -38,12 +39,20 @@ public static Task of(String message) throws HelperBotFileException {
3839
};
3940
}
4041

42+
/**
43+
* Change the description.
44+
* @param description The new description.
45+
*/
46+
public void setDescription(String description) {
47+
this.description = description;
48+
}
49+
4150
/**
4251
* Gets the icon for the status of the <code>Task</code>.
4352
* @return 'X' if the <code>Task</code> is done, else ' '.
4453
*/
4554
public String getStatusIcon() {
46-
return (isDone ? "X" : " "); // mark done helperbot.task with X
55+
return (isDone ? "X" : " ");
4756
}
4857

4958
/**
@@ -86,6 +95,16 @@ public boolean match(String description) {
8695
return this.description.contains(description);
8796
}
8897

98+
/**
99+
* Update the content of the task.
100+
* @param message The new content.
101+
* @return The updated task.
102+
* @throws HelperBotArgumentException If the arguments provided are in wrong format
103+
*/
104+
public Task update(String message) throws HelperBotArgumentException {
105+
return this;
106+
}
107+
89108
@Override
90109
public String toString() {
91110
return "["

src/main/java/helperbot/task/ToDo.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,4 +67,10 @@ public String toString() {
6767
return "[T]"
6868
+ super.toString();
6969
}
70+
71+
@Override
72+
public Task update(String message) throws HelperBotArgumentException {
73+
this.setDescription(message);
74+
return this;
75+
}
7076
}

src/main/java/helperbot/ui/Response.java

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,18 @@ public String getExitErrorMessage(String message) {
102102
return message + "\nBye. Hope to see you again soon!";
103103
}
104104

105+
/**
106+
* Output the result of update command.
107+
* @param index The index of the task.
108+
* @param task The task updated.
109+
* @return The result of the command.
110+
*/
111+
public String getUpdateOutput(int index, Task task) {
112+
return "I have updated HelperBot task "
113+
+ index
114+
+ "!\n\t"
115+
+ task.toString();
116+
}
105117

106118
/**
107119
* Error occurs when exiting the program.

0 commit comments

Comments
 (0)