Skip to content

Commit

Permalink
Added more tests for erroneous user input
Browse files Browse the repository at this point in the history
  • Loading branch information
chongjunwei committed Sep 30, 2021
1 parent ea90f3b commit 31592bb
Show file tree
Hide file tree
Showing 21 changed files with 243 additions and 18 deletions.
5 changes: 1 addition & 4 deletions data/duke.txt
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
T | 0 | read book
D | 0 | return book | 2013-03-03
E | 1 | project meeting | 2012-01-03
T | 0 | join sports club
D | 0 | return book | 2015-03-03
E | 0 | run | 2013-03-03
T | 0 | sleep
T | 0 | sleep
T | 0 | 1
D | 0 | sleep | 2012-03-03
3 changes: 3 additions & 0 deletions src/main/java/Duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
import Duke.command.Command;
import Duke.exception.DukeException;

/**
* This class encapsulates the Duke app.
*/
public class Duke {

private Storage storage;
Expand Down
11 changes: 10 additions & 1 deletion src/main/java/Duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,23 @@
import Duke.command.*;
import Duke.exception.DukeException;

/**
* Parses the user input and returns the corresponding command.
*/
public class Parser {

/**
* Returns the corresponding command for the user input
* @param input User input
* @return the corresponding command for the user input
* @throws DukeException
*/
public static Command parse(String input) throws DukeException {
switch (input.split(" ")[0]) {
case "bye":
return new ExitCommand();
case "list":
return new ListCommand();
return new ListCommand(input);
case "done":
return new DoneCommand(input);
case "todo":
Expand Down
16 changes: 16 additions & 0 deletions src/main/java/Duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@
import java.util.ArrayList;
import java.util.Scanner;

/**
* This class deals with loading tasks from the file and saving tasks in the file
*/
public class Storage {
private File file;

Expand All @@ -24,6 +27,10 @@ public Storage() {
}
}

/**
* Returns the tasks loaded from the file
* @return List containing the loaded tasks
*/
public ArrayList<Task> load() {
ArrayList<Task> tasks = new ArrayList<>();
try {
Expand All @@ -38,6 +45,11 @@ public ArrayList<Task> load() {
return tasks;
}

/**
* Returns an individual task loaded from the file
* @param task String representation of the task in the file
* @return Task containing the loaded task
*/
public static Task loadTask(String task) {
String[] parsed = task.split(" \\| ");
String type = parsed[0];
Expand All @@ -56,6 +68,10 @@ public static Task loadTask(String task) {
return loadedTask;
}

/**
* Saves the tasks in the file
* @param tasks List containing the current tasks
*/
public void save(TaskList tasks) {
this.file.delete();
try {
Expand Down
27 changes: 27 additions & 0 deletions src/main/java/Duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,61 @@
import Duke.task.Task;
import java.util.ArrayList;

/**
* This class contains the tasks loaded from the file.
*/
public class TaskList {
private ArrayList<Task> tasks;

public TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}

/**
* Returns the current tasks in the list
* @return The current tasks in the list
*/
public ArrayList<Task> getTasks() {
return this.tasks;
}

/**
* Marks a task as done
* @param n Index of the task to mark as done
* @return A message to the user that the task has been marked as done
*/
public String markAsDone(int n) {
Task task = this.tasks.get(n-1);
task.markAsDone();
return Ui.doneTask(task);
}

/**
* Adds a task
* @param task Task to be added to the list
* @return A message to the user that the task has been added to the list
*/
public String addTask(Task task) {
this.tasks.add(task);
return Ui.addTask(task, this.tasks.size());
}

/**
* Deletes a task
* @param n Index of the task to be deleted
* @return A message to the user that the task has been deleted
*/
public String deleteTask(int n) {
Task task = this.tasks.get(n-1);
this.tasks.remove(n-1);
return Ui.deleteTask(task, this.tasks.size());
}

/**
* Finds the tasks matching a given string
* @param search Input string entered by the user
* @return A message to the user containing the tasks that match the given search string
*/
public String findTasks(String search) {
ArrayList<Task> matches = new ArrayList<>();
for (Task task : this.tasks) {
Expand Down
33 changes: 33 additions & 0 deletions src/main/java/Duke/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,37 +3,70 @@
import Duke.task.Task;
import java.util.ArrayList;

/**
* This class deals with interactions with the user.
*/
public class Ui {

/**
* Returns a greeting message
* @return A greeting message
*/
public static String greet() {
return "Hello! I'm Duke.\n" +
"What can I do for you?";
}

/**
* Returns a parting message
* @return A parting message
*/
public static String bye() {
return "Bye. Hope to see you again soon!";
}

/**
* Returns a message to the user that the task has been added to the list
* @param task The task that has been added to the list
* @param size Size of the current list of tasks
* @return A message to the user that the task has been added to the list
*/
public static String addTask(Task task, int size) {
String reply = "Got it. I've added this task:\n " +
task + "\n" +
"Now you have " + size + " tasks" + " in the list.";
return reply;
}

/**
* Returns a message to the user that the task has been marked as done
* @param task The task that has been marked as done
* @return A message to the user that the task has been marked as done
*/
public static String doneTask(Task task) {
String reply = "Nice! I've marked this task as done:\n" +
task;
return reply;
}

/**
* Returns a message to the user that the task has been deleted
* @param task The task that has been deleted
* @param size Size of the current list of tasks
* @return
*/
public static String deleteTask(Task task, int size) {
String reply = "Noted. I've removed this task:\n" +
task + "\n"
+ "Now you have " + size + " tasks" + " in the list.\n";
return reply;
}

/**
* Returns a message to the user containing the tasks that match the given search string
* @param matches The tasks that match the given search string
* @return A message to the user containing the tasks that match the given search string
*/
public static String findTasks(ArrayList<Task> matches) {
String reply = "Here are the matching tasks in your list:\n";
int count = 1;
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Duke/command/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,15 @@

import Duke.TaskList;

/**
* Represents a command.
*/
public abstract class Command {

/**
* Returns the result of the execution of the command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the command.
*/
public abstract String execute(TaskList tasks);
}
18 changes: 9 additions & 9 deletions src/main/java/Duke/command/DeadlineCommand.java
Original file line number Diff line number Diff line change
@@ -1,19 +1,14 @@
package Duke.command;

import Duke.Storage;
import Duke.TaskList;
import Duke.task.Deadline;
import Duke.task.Todo;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.PrintWriter;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;

/**
* Represents a deadline command.
*/
public class DeadlineCommand extends Command {

private Deadline deadline;

public DeadlineCommand(String input) {
Expand All @@ -23,6 +18,11 @@ public DeadlineCommand(String input) {
this.deadline = new Deadline(task, time);
}

/**
* Returns the result of the execution of the deadline command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the deadline command.
*/
@Override
public String execute(TaskList tasks) {
return tasks.addTask(this.deadline);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Duke/command/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import Duke.TaskList;

/**
* Represents a delete command.
*/
public class DeleteCommand extends Command {

private int task;
Expand All @@ -10,6 +13,11 @@ public DeleteCommand(String input) {
this.task = Integer.parseInt(input.split(" ")[1]);
}

/**
* Returns the result of the execution of the delete command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the delete command.
*/
@Override
public String execute(TaskList tasks) {
return tasks.deleteTask(this.task);
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Duke/command/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@

import Duke.TaskList;

/**
* Represents a done command.
*/
public class DoneCommand extends Command {

private int task;
Expand All @@ -10,6 +13,11 @@ public DoneCommand(String input) {
this.task = Integer.parseInt(input.split(" ")[1]);
}

/**
* Returns the result of the execution of the done command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the done command.
*/
@Override
public String execute(TaskList tasks) {
return tasks.markAsDone(this.task);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Duke/command/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,11 @@
import Duke.task.Event;
import java.time.LocalDate;

/**
* Represents an event command.
*/
public class EventCommand extends Command {

private Event event;

public EventCommand(String input) {
Expand All @@ -14,6 +18,11 @@ public EventCommand(String input) {
this.event = new Event(task, time);
}

/**
* Returns the result of the execution of the event command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the event command.
*/
@Override
public String execute(TaskList tasks) {
return tasks.addTask(this.event);
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Duke/command/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,16 @@
import Duke.TaskList;
import Duke.Ui;

/**
* Represents an exit command.
*/
public class ExitCommand extends Command {

/**
* Returns the result of the execution of the exit command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the exit command.
*/
@Override
public String execute(TaskList tasks) {
return Ui.bye();
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/Duke/command/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,22 @@

import Duke.TaskList;

/**
* Represents a find command.
*/
public class FindCommand extends Command {

private String task;

public FindCommand(String input) {
this.task = input.split(" ")[1];
}

/**
* Returns the result of the execution of the find command.
* @param tasks List of tasks the user has added.
* @return Result of the execution of the find command.
*/
@Override
public String execute(TaskList tasks) {
return tasks.findTasks(this.task);
Expand Down
Loading

0 comments on commit 31592bb

Please sign in to comment.