|
13 | 13 | */ |
14 | 14 | public class Event extends Task { |
15 | 15 |
|
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; |
20 | 20 |
|
21 | 21 | /** |
22 | 22 | * Generate a <code>Event</code> |
@@ -121,6 +121,95 @@ public String toString() { |
121 | 121 | + ")"; |
122 | 122 | } |
123 | 123 |
|
| 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 | + |
124 | 213 | private static Event getEvent(String[] message) throws HelperBotFileException { |
125 | 214 | LocalDate fromDate = LocalDate.parse(message[3]); |
126 | 215 | LocalDate toDate = LocalDate.parse(message[5]); |
|
0 commit comments