Skip to content

Commit

Permalink
Add update functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
teoks0199 committed Sep 15, 2023
1 parent 2d87874 commit 10b4ab7
Show file tree
Hide file tree
Showing 11 changed files with 113 additions and 29 deletions.
6 changes: 3 additions & 3 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
E | N | Finals | 2023-11-11 | 2023-11-12
T | Y | read book
T | N | return book
D | N | write book | 2022-11-11
D | N | write book | 2023-10-10
E | N | book fair | 2000-11-11 | 2000-11-11
T | N | jebfw
T | N | test
D | N | da | 2020-01-01
E | N | da | 2020-10-10 | 2020-10-11
E | N | asdbf | 2020-10-10 | 2020-10-11
1 change: 0 additions & 1 deletion src/main/java/duke/DialogBox.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import javafx.fxml.FXMLLoader;
import javafx.geometry.Pos;
import javafx.scene.Node;
import javafx.scene.control.Label;
import javafx.scene.image.Image;
import javafx.scene.image.ImageView;
import javafx.scene.layout.HBox;
Expand Down
15 changes: 7 additions & 8 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import java.nio.file.Path;
import java.nio.file.Paths;

import java.util.Scanner;

/**
Expand All @@ -15,13 +14,6 @@ public class Duke {
private TaskList tasks;
static final Path FILEPATH = Paths.get(".", "data", "duke.txt"); // Filepath: "./data/duke.txt"

public String getResponse(String input) {
Parser parser = new Parser(tasks);
String response = parser.parse(input);
assert response != null;
return response;
}

/**
* Constructor to start the program.
*/
Expand Down Expand Up @@ -52,6 +44,13 @@ public void updateTaskList() {
storage.updateTaskList();
}

public String getResponse(String input) {
Parser parser = new Parser(tasks);
String response = parser.parse(input);
assert response != null;
return response;
}

/**
* Starts the program and loads in the saved task list.
*
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/duke/Launcher.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ public class Launcher {
public static void main(String[] args) {
Application.launch(Main.class, args);
}
}
}
7 changes: 0 additions & 7 deletions src/main/java/duke/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.ScrollPane;
import javafx.scene.control.TextField;
import javafx.scene.image.Image;
Expand All @@ -18,18 +17,12 @@ public class MainWindow extends AnchorPane {
private VBox dialogContainer;
@FXML
private TextField userInput;
@FXML
private Button sendButton;

private Duke duke;
static final String INITIAL_RESPONSE = "Hello, I'm your task manager :)\nWhat can I do for you?";
static final String FINAL_RESPONSE = "Bye. Hope to see you again soon!";

private Image userImage = new Image(this.getClass().getResourceAsStream("/images/DaUser.png"));
private Image dukeImage = new Image(this.getClass().getResourceAsStream("/images/DaDuke.png"));



@FXML
public void initialize() {
scrollPane.vvalueProperty().bind(dialogContainer.heightProperty());
Expand Down
4 changes: 3 additions & 1 deletion src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,13 @@ public String parse(String s) {
output = tasks.addToDo(input);
} else if (command.equals("find")) {
output = tasks.findTasks(input);
} else if (command.equals("update")) {
output = tasks.updateTask(input);
} else if (command.equals("bye")) {
this.isFinished = true;
output = Ui.bye();
Platform.exit();
} else{
} else {
output = Ui.unknownCommandErrorMessage();
}
Ui.horizontalLine();
Expand Down
79 changes: 71 additions & 8 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ public String deleteTask(String[] input) {
Task task = taskList.get(toDelete - 1);
String deletedTask = task.toString();
taskList.remove(toDelete - 1);
String output = "Noted. I've removed this task:\n" +
deletedTask + "\n" + "Number of tasks: " + taskList.size();
String output = "Noted. I've removed this task:\n"
+ deletedTask + "\n" + "Number of tasks: " + taskList.size();
System.out.println(output);
return output;
} catch (IndexOutOfBoundsException e) {
Expand Down Expand Up @@ -131,8 +131,8 @@ public String addEvent(String[] input) {
}
Event e = new Event(desc, LocalDate.parse(from), LocalDate.parse(to));
taskList.add(e);
String output = "Got it. I've added this task:\n" +
e.toString() + "\n" + "Number of tasks: " + taskList.size();
String output = "Got it. I've added this task:\n"
+ e.toString() + "\n" + "Number of tasks: " + taskList.size();
System.out.println(output);
return output;
} catch (DukeException e) {
Expand Down Expand Up @@ -160,8 +160,8 @@ public String addDeadline(String[] input) {
}
Deadline d = new Deadline(desc, LocalDate.parse(deadline));
taskList.add(d);
String output = "Got it. I've added this task:\n" +
d.toString() + "\n" + "Number of tasks: " + taskList.size();
String output = "Got it. I've added this task:\n"
+ d.toString() + "\n" + "Number of tasks: " + taskList.size();
System.out.println(output);
return output;
} catch (DukeException e) {
Expand All @@ -187,8 +187,8 @@ public String addToDo(String[] input) {
}
ToDo t = new ToDo(desc);
taskList.add(t);
String output = "Got it. I've added this task:\n" +
t.toString() + "\n" + "Number of tasks: " + taskList.size();
String output = "Got it. I've added this task:\n"
+ t.toString() + "\n" + "Number of tasks: " + taskList.size();
System.out.println(output);
return output;
} catch (IndexOutOfBoundsException e) {
Expand Down Expand Up @@ -227,4 +227,67 @@ public String findTasks(String[] input) {
return e.getMessage();
}
}

public String updateTask(String[] input) {
try {
String[] s1 = input[1].split("/", 2);
String[] s2 = s1[1].split(" ", 2);
String taskNumber = s1[0].strip();
String updateType = s2[0].strip();
String newValue = s2[1].strip();
if (Integer.parseInt(taskNumber) > taskList.size()) {
throw new DukeException(Ui.taskNotFoundErrorMessage());
}
if (taskNumber.equals("") || updateType.equals("") || newValue.equals("")) {
throw new DukeException(Ui.updateTaskErrorMessage());
}
Task task = taskList.get(Integer.parseInt(taskNumber) - 1);
updateTaskValue(task, updateType, newValue);
String output = "OK, I've updated this task:\n" + task.toString();
System.out.println(output);
return output;
} catch (IndexOutOfBoundsException e) {
return Ui.updateTaskErrorMessage();
} catch (NumberFormatException e) {
return Ui.updateTaskErrorMessage();
} catch (DukeException e) {
return e.getMessage();
} catch (DateTimeParseException e) {
return Ui.invalidDateErrorMessage();
}
}

private void updateTaskValue(Task task, String toUpdate, String newValue) throws DukeException {
switch (toUpdate) {
case "desc":
task.updateDescription(newValue);
break;
case "by":
if (task instanceof Deadline) {
Deadline d = (Deadline) task;
d.updateByDate(LocalDate.parse(newValue));
} else {
throw new DukeException(Ui.wrongTaskTypeErrorMessage());
}
break;
case "from":
if (task instanceof Event) {
Event e = (Event) task;
e.updateFromDate(LocalDate.parse(newValue));
} else {
throw new DukeException(Ui.wrongTaskTypeErrorMessage());
}
break;
case "to":
if (task instanceof Event) {
Event e = (Event) task;
e.updateToDate(LocalDate.parse(newValue));
} else {
throw new DukeException(Ui.wrongTaskTypeErrorMessage());
}
break;
default:
throw new DukeException(Ui.updateTaskErrorMessage());
}
}
}
12 changes: 12 additions & 0 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,18 @@ public static String markTaskErrorMessage() {
return output;
}

public static String updateTaskErrorMessage() {
String output = "Format: update taskNumber /(desc, by, from, to) value, e.g. update 1 /from 2023-12-30";
System.out.println(output);
return output;
}

public static String wrongTaskTypeErrorMessage() {
String output = "This task type does not have the required field.";
System.out.println(output);
return output;
}

public static String unmarkTaskErrorMessage() {
String output = "Please enter a number e.g., unmark 1";
System.out.println(output);
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,8 @@ public boolean equals(Object o) {
Deadline c = (Deadline) o;
return this.toString().equals(c.toString());
}

public void updateByDate(LocalDate newDate) {
by = newDate;
}
}
8 changes: 8 additions & 0 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -52,4 +52,12 @@ public String toString() {
public String taskToString() {
return "E | " + super.taskToString() + " | " + from + " | " + to;
}

public void updateFromDate(LocalDate newDate) {
from = newDate;
}

public void updateToDate(LocalDate newDate) {
to = newDate;
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,8 @@ public String toString() {
public String taskToString() {
return String.format("%s | " + this.description, (isDone ? "Y" : "N"));
}

public void updateDescription(String newDesc) {
description = newDesc;
}
}

0 comments on commit 10b4ab7

Please sign in to comment.