Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

Branch a code quality #3

Merged
merged 2 commits into from
Feb 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,17 @@ public Duke() {
}
}

public String getResponse(String input) {
String[] inputArr = input.strip().split(" ",2);
try {
String toPrint = PARSER.readInput(inputArr, taskList);
storage.storeData(this.taskList.getTasks());
return toPrint;
} catch (DukeException e) {
return e.getMessage();
}
}

public void run() throws IOException, DukeException{
UI.start();
String[] input = UI.readLine();
Expand All @@ -44,15 +55,4 @@ public void run() throws IOException, DukeException{
storage.storeData(this.taskList.getTasks());
}

public String getResponse(String input) {
String[] inputArr = input.strip().split(" ",2);
try {
String toPrint = PARSER.readInput(inputArr, taskList);
storage.storeData(this.taskList.getTasks());
return toPrint;
} catch (DukeException e) {
return e.getMessage();
}
}

}
42 changes: 30 additions & 12 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,10 @@ public String readInput(String[] input, TaskList taskList) throws DukeException
}

/**
* Method to handle the mark command.
* Marks a specific task as done and returns a String to notify user.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user enters invalid number or task that has not been created.
*/
public String mark(String[] input, TaskList taskList) throws DukeException {
Expand All @@ -62,17 +63,20 @@ public String mark(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size() || index <= 0) {
boolean isNotWithinTaskListSize = index > taskList.size();
boolean isSmallerThan1 = index <= 0;
if (isNotWithinTaskListSize || isSmallerThan1) {
throw new DukeException("Invalid task.");
}
assert index > 0 : "Number less than equal 0.";
return taskList.markTask(index - 1);
}

/**
* Method to handle the unmark command.
* Marks a specific task as not done and returns a String to notify user.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user enters invalid number or task that has not been created.
*/
public String unmark(String[] input, TaskList taskList) throws DukeException {
Expand All @@ -85,17 +89,20 @@ public String unmark(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size() || index <= 0) {
boolean isNotWithinTaskListSize = index > taskList.size();
boolean isSmallerThan1 = index <= 0;
if (isNotWithinTaskListSize || isSmallerThan1) {
throw new DukeException("Invalid task.");
}
assert index > 0 : "Number less than equal 0.";
return taskList.unmarkTask(index - 1);
}

/**
* Method to handle the todo command.
* Adds a Todo task into the users TaskList.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user did not provide name of Todo.
*/
public String todo(String[] input, TaskList taskList) throws DukeException {
Expand All @@ -108,13 +115,16 @@ public String todo(String[] input, TaskList taskList) throws DukeException {
}

/**
* Method to handle the deadline command.
* Adds a Deadline task into the users TaskList.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user did not enter /by date or invalid date format.
*/
public String deadline(String[] input, TaskList taskList) throws DukeException {
if (input.length == 1 || !input[1].contains("/by")) {
boolean isWithoutInput = input.length == 1;
boolean isWithoutBy = !input[1].contains("/by");
if (isWithoutInput || isWithoutBy) {
throw new DukeException("Deadline needs a /by.");
}
String[] tempInput = input[1].strip().split("/by ");
Expand All @@ -131,13 +141,17 @@ public String deadline(String[] input, TaskList taskList) throws DukeException {
}

/**
* Method to handle the event command.
* Adds an Event task into the users TaskList.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user did not enter /from or /to or invalid date format.
*/
public String event(String[] input, TaskList taskList) throws DukeException {
if (input.length == 1 || !input[1].contains("/from") || !input[1].contains("/to") ) {
boolean isWithoutInput = input.length == 1;
boolean isWithoutFrom = !input[1].contains("/from");
boolean isWithoutTo = !input[1].contains("/to");
if (isWithoutInput || isWithoutFrom || isWithoutTo ) {
throw new DukeException("Event needs a /from and /to.");
}
String[] tempInput = input[1].split("/");
Expand All @@ -156,9 +170,10 @@ public String event(String[] input, TaskList taskList) throws DukeException {
}

/**
* Method to handle the delete command.
* Deletes a task from the user's TaskList.
* @param input User's input
* @param taskList User's TaskList
* @return String Message to update user upon success
* @throws DukeException If user enters invalid number.
*/
public String delete(String[] input, TaskList taskList) throws DukeException {
Expand All @@ -171,17 +186,20 @@ public String delete(String[] input, TaskList taskList) throws DukeException {
} catch (NumberFormatException e) {
throw new DukeException("Invalid number.");
}
if (index > taskList.size() || index <= 0) {
boolean isNotWithinTaskListSize = index > taskList.size();
boolean isSmallerThan1 = index <= 0;
if (isNotWithinTaskListSize || isSmallerThan1) {
throw new DukeException("Invalid task.");
}
assert index > 0 : "Number less than equal 0.";
return taskList.deleteTask(index - 1);
}

/**
* Method to handle the find command.
* Returns a String containing the user's task that matches the given keyword.
* @param input User's input
* @param taskList User's TaskList
* @return String User's task that match given keyword
* @throws DukeException If user did not enter a keyword.
*/
public String find(String[] input, TaskList taskList) throws DukeException {
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public TaskList loadData() throws DukeException {
if (file.createNewFile()){
return taskList;
}
assert file.exists(): "File was not successfully created.";
assert file.exists() : "File was not successfully created.";
} catch (IOException e) {
throw new DukeException("Something went wrong when accessing file");
}
Expand Down Expand Up @@ -85,7 +85,7 @@ public void storeData(ArrayList<Task> t) throws DukeException {
if (file.createNewFile()) {
System.out.println("Created new file taskList.txt");
}
assert file.exists(): "File was not successfully created.";
assert file.exists() : "File was not successfully created.";
FileWriter fw = new FileWriter(file);
String taskString = "";
for (int i = 0; i < t.size(); i++){
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,11 @@ public String[] readLine() throws IOException {
}

/**
* Prints a line to separate user commands and output.
* Returns a line to separate user commands and output.
* @return String Line
*/
public void displayLine() {
System.out.println("_________________________________________\n");
public String displayLine() {
return "_________________________________________\n";
}

/**
Expand Down
9 changes: 2 additions & 7 deletions taskList.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
D|1|2|2023-05-05
T|0|3
T|0|4
T|0|5
T|0|6
D|0||2023-05-25
E|0|3|2023-12-23|2023-12-24
E|0|4|2023-12-23|2023-12-24
E|0|5|2023-03-23|2023-05-06
T|0|1
D|0|3|2023-12-23