Skip to content

Commit

Permalink
Merge branch 'branch-C-Update'
Browse files Browse the repository at this point in the history
  • Loading branch information
WinstonJin committed Sep 11, 2024
2 parents 3ffaec5 + 723f858 commit 7129a98
Show file tree
Hide file tree
Showing 7 changed files with 234 additions and 15 deletions.
4 changes: 2 additions & 2 deletions src/main/java/WindeTasks.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
D | O | cs2103 ass1 | 03/04/2026 19:00
E | X | TAC WorldX | 05/07/2022 19:00 - 31/12/2024 18:56
T | O | yes
E | O | TAC WorldX | 04/07/2022 19:00 - 01/01/2025 19:45
T | O | no
96 changes: 96 additions & 0 deletions src/main/java/commands/ChangeCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package commands;

import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

import exceptions.EmptyDescriptionException;
import tasks.Deadline;
import tasks.Event;
import tasks.Task;
import windebot.History;
import windebot.Reminder;
import windebot.Ui;

/**
* The ChangeCommand class represents a command to edit a task to the reminder list.
* This command parses the input to extract the task description and date.
* If the input is invalid or incomplete, an exception is thrown.
*/

public class ChangeCommand extends Command {
/**
* Executes the ChangeCommand command, edits the task in the schedule.
*
* @param input The user input string containing the task description and deadline.
* @param reminder The Reminder object that manages the task list.
* @param ui The Ui object used to interact with the user.
* @param history The History object used to save the data
* @return true if the command was executed successfully.
* @throws EmptyDescriptionException If the input is incomplete or incorrectly formatted.
*/

public boolean execute(String input, Reminder reminder, Ui ui, History history)
throws EmptyDescriptionException {
String[] command = input.split(" ", 2);
assert(command.length == 2);
int toChange = reminder.getSelected();
Task task = reminder.getSchedule().get(toChange);
if (command[0].equalsIgnoreCase("change")) {
toChange = Integer.parseInt(command[1]) - 1;
reminder.changeSelected(toChange);
task = reminder.getSchedule().get(toChange);
ui.print("Got it. You want to edit this task:");
ui.print(" " + task.toString());
ui.print("To change the task description, input: action (task description)");
if (task.getClass() == Deadline.class) {
ui.print("To change the deadline, input: cutoff (new deadline)");
} else if (task.getClass() == Event.class) {
ui.print("To change the start date, input: start (new start)");
ui.print("To change the end date, input: end (new end)");
}
} else if (command[0].equalsIgnoreCase("action")) {
task.changeAction(command[1]);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
} else if (command[0].equalsIgnoreCase("cutoff")
&& task.getClass() == Deadline.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime deadline = LocalDateTime.parse(command[1], formatter);
task.changeDate(deadline);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("start")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime start = LocalDateTime.parse(command[1], formatter);
task.changeStartDate(start);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else if (command[0].equalsIgnoreCase("end")
&& task.getClass() == Event.class) {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HH:mm");
LocalDateTime end = LocalDateTime.parse(command[1], formatter);
task.changeEndDate(end);
ui.print("Got it. The task has been changed to:");
ui.print(" " + task.toString());
ui.print("Now you have " + reminder.size() + " tasks in the list.");
history.save(reminder.getSchedule());
} else {
throw new EmptyDescriptionException("PUT IN PARAMETERS");
}
return true;
}

/**
* Gets the type of command: Add, ChangeMark or Delete
*/

public String whatCommand() {
return "";
}
}
11 changes: 11 additions & 0 deletions src/main/java/tasks/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,17 @@ public Deadline(String action, boolean complete, LocalDateTime date) {
this.date = date;
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new deadline of the task
*/

@Override
public void changeDate(LocalDateTime newDate) {
this.date = newDate;
}

/**
* Returns the date of the deadline task.
*
Expand Down
20 changes: 20 additions & 0 deletions src/main/java/tasks/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ public Event(String action, boolean complete, LocalDateTime start, LocalDateTime
this.end = end;
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new start date of the task
*/

public void changeStartDate(LocalDateTime newDate) {
this.start = newDate;
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new end date of the task
*/

public void changeEndDate(LocalDateTime newDate) {
this.end = newDate;
}

/**
* Returns the date of the event task (the start date).
*
Expand Down
37 changes: 37 additions & 0 deletions src/main/java/tasks/Task.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package tasks;

import java.time.LocalDate;
import java.time.LocalDateTime;

/**
* The Task class represents a general task with a description and a completion status.
Expand Down Expand Up @@ -81,6 +82,42 @@ public String getAction() {
return action;
}


/**
* Changes the description of the task.
*/

public void changeAction(String newAction) {
this.action = newAction;
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new deadline of the task
*/

public void changeDate(LocalDateTime newDate) {
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new start date of the task
*/

public void changeStartDate(LocalDateTime newDate) {
}

/**
* Changes the due date of the deadline.
*
* @param newDate The new end date of the task
*/

public void changeEndDate(LocalDateTime newDate) {
}

@Override
public String toString() {
return (complete ? "X" : "O") + " | " + action;
Expand Down
16 changes: 15 additions & 1 deletion src/main/java/windebot/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import commands.AddTodo;
import commands.AddUnmark;
import commands.ByeCommand;
import commands.ChangeCommand;
import commands.Command;
import commands.DateCommand;
import commands.ErrorCommand;
Expand Down Expand Up @@ -66,6 +67,9 @@ public static Command parse(String input) throws UnsupportedCommandException {
case BYE:
commands = new ByeCommand();
break;
case CHANGE:
commands = new ChangeCommand();
break;
case UNKNOWN:
commands = new ErrorCommand();
break;
Expand Down Expand Up @@ -112,6 +116,16 @@ private static SubCommandType getCommandType(String input) {
return SubCommandType.DATE;
} else if (input.startsWith("find")) {
return SubCommandType.FIND;
} else if (input.startsWith("change")) {
return SubCommandType.CHANGE;
} else if (input.startsWith("cutoff")) {
return SubCommandType.CHANGE;
} else if (input.startsWith("start")) {
return SubCommandType.CHANGE;
} else if (input.startsWith("end")) {
return SubCommandType.CHANGE;
} else if (input.startsWith("action")) {
return SubCommandType.CHANGE;
} else {
return SubCommandType.UNKNOWN;
}
Expand All @@ -122,6 +136,6 @@ private static SubCommandType getCommandType(String input) {
*/

enum SubCommandType {
TODO, DEADLINE, EVENT, LIST, DELETE, BYE, MARK, UNMARK, DATE, FIND, UNKNOWN
TODO, DEADLINE, EVENT, LIST, DELETE, BYE, MARK, UNMARK, DATE, FIND, CHANGE, UNKNOWN
}
}
65 changes: 53 additions & 12 deletions src/main/java/windebot/Reminder.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@ public class Reminder {
private static ArrayList<Task> schedule;
private static Hashtable<LocalDate, ArrayList<Task>> calendar;

private static int selected;

/**
* Constructs a Reminder object with an empty task list and calendar.
*/

Reminder() {
this.schedule = new ArrayList<Task>();
this.calendar = new Hashtable<LocalDate, ArrayList<Task>>();
this.selected = 0;
}

/**
Expand All @@ -37,6 +40,7 @@ public class Reminder {
Reminder(ArrayList<Task> schedule, Hashtable<LocalDate, ArrayList<Task>> calendar) {
this.schedule = schedule;
this.calendar = calendar;
this.selected = 0;
}

/**
Expand All @@ -52,6 +56,7 @@ public class Reminder {
this.schedule.forEach(task -> {
calendar.computeIfAbsent(task.getDate(), k -> new ArrayList<>()).add(task);
});
this.selected = 0;
/*
for (Task task : schedule) {
ArrayList<Task> taskList;
Expand Down Expand Up @@ -128,18 +133,40 @@ public static void addTodo(Todos task) {

public static Task remove(int i) {
Task task = schedule.remove(i);
if (task.getClass() != Todos.class) {
LocalDate date = task.getDate();
calendar.computeIfPresent(date, (key, value) -> {
value.remove(task);
return value.isEmpty() ? null : value;
});
/*
ArrayList<Task> taskList = calendar.get(date);
taskList.remove(task);
calendar.put(date, taskList);
*/
}
LocalDate date = task.getDate();
calendar.computeIfPresent(date, (key, value) -> {
value.remove(task);
return value.isEmpty() ? null : value;
});
/*
ArrayList<Task> taskList = calendar.get(date);
taskList.remove(task);
calendar.put(date, taskList);
*/
return task;
}

/**
* Removes a task from the schedule and calendar.
*
* @param i The index of the task to replace.
* @param newTask The new task to replace the old one.
* @return The removed task.
*/

public static Task replace(int i, Task newTask) {
Task task = schedule.get(i);
schedule.set(i, newTask);
LocalDate date = task.getDate();
calendar.computeIfPresent(date, (key, value) -> {
value.set(value.indexOf(task), newTask);
return value.isEmpty() ? null : value;
});
/*
ArrayList<Task> taskList = calendar.get(date);
taskList.remove(task);
calendar.put(date, taskList);
*/
return task;
}

Expand Down Expand Up @@ -214,6 +241,20 @@ public static ArrayList<Task> getSchedule() {
return schedule;
}

public int getSelected() {
return selected;
}

/**
* Changes the selected task to edit
*
* @param i The int of the new selected task to edit
*/

public static void changeSelected(int i) {
selected = i;
}

/**
* Retrieves a task from the schedule by index.
*
Expand Down

0 comments on commit 7129a98

Please sign in to comment.