Skip to content

Commit

Permalink
Merge pull request #1 from Impala36/branch-Level-9
Browse files Browse the repository at this point in the history
Branch level 9
  • Loading branch information
Impala36 authored Sep 28, 2020
2 parents c675d46 + 6d0c227 commit a5c85af
Show file tree
Hide file tree
Showing 10 changed files with 143 additions and 61 deletions.
2 changes: 1 addition & 1 deletion src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ private void repeatedUserInput() {
while (true) {
Command command = parser.parseInput(in.nextLine(), ui, tasks, database);
command.execute();
if (command.equals(new ExitCommand(ui))) return;
if (ExitCommand.class.isAssignableFrom(command.getClass())) return;
}
}
}
12 changes: 9 additions & 3 deletions src/main/java/duke/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,24 @@
* An abstract class that is inherited by every command.
*/
public abstract class Command {
public String taskDescription;
public String line;
public TaskList tasks;
public Ui ui;
public Database database;

protected Command(String taskDescription, TaskList tasks, Ui ui, Database database) {
this.taskDescription = taskDescription;
protected Command(String line, TaskList tasks, Ui ui, Database database) {
this.line = line;
this.database = database;
this.tasks = tasks;
this.ui = ui;
}

protected Command(String line, TaskList tasks, Ui ui) {
this.line = line;
this.tasks = tasks;
this.ui = ui;
}

protected Command(TaskList tasks, Ui ui) {
this.tasks = tasks;
this.ui = ui;
Expand Down
41 changes: 29 additions & 12 deletions src/main/java/duke/commands/DeadlineCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,44 +7,61 @@
import duke.ui.Ui;

import java.io.IOException;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* A command to create a Deadline task that has a deadline.
*/
public class DeadlineCommand extends Command {
public static final String word = "deadline";

public DeadlineCommand(String taskDescription, TaskList tasks, Ui ui, Database database) {
super(taskDescription, tasks, ui, database);
public DeadlineCommand(String line, TaskList tasks, Ui ui, Database database) {
super(line, tasks, ui, database);
}

/**
* Changes the format of the whole line into proper task format.
*/
public String reformatLine(String type, String keyword, String line) {
line = line.replaceFirst("/" + keyword, "(" + keyword + ":").concat(")");
return type + line;
}

public String reformatDateTime(String dateTime) throws DateTimeParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy HHmm");
LocalDateTime date = LocalDateTime.parse(dateTime, formatter);
return date.format(DateTimeFormatter.ofPattern("d MMM yyyy h:mma"));
}

@Override
public void execute() {
try {
if (taskDescription.isEmpty() || !taskDescription.contains("/by")) {
if (line.isEmpty() || !line.contains("/by")) {
throw new DukeException("It seems that you've missed out the task description or the /by <when> segment!\n" +
"Please type in the 'deadline <something> /by <when>' format.");
"Please type in the 'deadline <something> /by <dd/MM/yyyy HHmm>' format.");
}
String[] split = taskDescription.split("/by");
String description = line.split("/by ")[0];
String dateTime = line.split("/by ")[1];

if (split[0].isEmpty() || split[1].isEmpty()) {
if (description.isEmpty() || dateTime.isEmpty()) {
throw new DukeException("It seems that you've missed out the task description or the /by <when> segment!\n" +
"Please type in the 'deadline <something> /by <when>' format.");
"Please type in the 'deadline <something> /by <dd/MM/yyyy HHmm>' format.");
}

taskDescription = reformatLine("[Deadline] ", "by", taskDescription);
tasks.add(new Deadline(taskDescription));
line = description + "/by " + reformatDateTime(dateTime);
line = reformatLine("[Deadline] ", "by", line);
tasks.add(new Deadline(line));
ui.printTaskAdded(tasks);
database.updateDatabase(tasks);

} catch (DukeException | IOException e) {
ui.printRedBorderlines(e.getMessage());
} catch (IndexOutOfBoundsException e) {
ui.printRedBorderlines("It seems that you've missed out the deadline time!\n" +
"Please type in something for <when> after 'deadline <something> /by'.");
"Please type in something for <dd/MM/yyyy HHmm> after 'deadline <something> /by'.");
} catch (DateTimeParseException e) {
ui.printRedBorderlines("It seems that you didn't enter the time in the right format!\n" +
"Please type in the 'deadline <something> /by <dd/MM/yyyy HHmm>' format.");
}
}
}
4 changes: 2 additions & 2 deletions src/main/java/duke/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public void execute() {
"Perhaps you should start creating one?");
return;
}
if (taskDescription.isEmpty()) {
if (line.isEmpty()) {
throw new DukeException("You almost typed a proper delete command, but you missed out the number!\n" +
"Please type in the 'delete <task index number>' format.");
}
int index = Integer.parseInt(taskDescription);
int index = Integer.parseInt(line);
ui.printTaskRemoved(tasks, index);
tasks.remove(index);
database.updateDatabase(tasks);
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/duke/commands/DoneCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@
public class DoneCommand extends Command {
public static final String word = "done";

public DoneCommand(String taskDescription, TaskList tasks, Ui ui, Database database) {
super(taskDescription, tasks, ui, database);
public DoneCommand(String line, TaskList tasks, Ui ui, Database database) {
super(line, tasks, ui, database);
}

@Override
Expand All @@ -24,11 +24,11 @@ public void execute() {
"Perhaps you should start creating one?");
return;
}
if (taskDescription.isEmpty()) {
if (line.isEmpty()) {
throw new DukeException("You almost typed a proper done command, but you missed out the number!\n" +
"Please type in the 'done <task index number>' format.");
}
int index = Integer.parseInt(taskDescription);
int index = Integer.parseInt(line);
tasks.get(index - 1).setDone();
ui.printTaskCompleted(index, tasks);
database.updateDatabase(tasks);
Expand Down
54 changes: 38 additions & 16 deletions src/main/java/duke/commands/EventCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,47 +2,69 @@

import duke.database.Database;
import duke.exception.DukeException;
import duke.task.Deadline;
import duke.task.Event;
import duke.task.TaskList;
import duke.ui.Ui;

import java.io.IOException;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;
import java.time.format.DateTimeParseException;

/**
* A command that is similar to a Deadline task but with a different time keyword '/at'.
* A command that is similar to a Deadline task but with a different time keyword '/at' and has a time duration.
*/
public class EventCommand extends DeadlineCommand {
public static final String word = "event";

public EventCommand(String taskDescription, TaskList tasks, Ui ui, Database database) {
super(taskDescription, tasks, ui, database);
}
/**
* Changes the format of the whole line into proper task format.
*/
public String reformatLine(String type, String keyword, String keyword2, String line) {
line = line.replaceFirst("/" + keyword, "(" + keyword + ":").concat(")");
line = line.replace("/" + keyword2, "-");
return type + line;
}

public String reformatTime(String time) throws DateTimeParseException {
DateTimeFormatter formatter = DateTimeFormatter.ofPattern("HHmm");
LocalTime date = LocalTime.parse(time, formatter);
return date.format(DateTimeFormatter.ofPattern("h:ma"));
}
@Override
public void execute() {
try {
if (taskDescription.isEmpty() || !taskDescription.contains("/at")) {
throw new DukeException("It seems that you've missed out the task description " +
"or the /at <when to when> segment!\n" +
"Please type in the 'event <something> /at <when to when>' format.");
if (line.isEmpty() || !line.contains("/at") || !line.contains("/to")) {
throw new DukeException("It seems that you've missed out the required keyword(s)!\n" +
"Please type in the 'event <something> /at <dd/MM/yyyy HHmm> /to <HHmm>' format.");
}
String[] split = taskDescription.split("/at");
String description = line.split("/at ")[0];
String startDateTime = line.split("/at ")[1].split(" /to ")[0];
String endTime = line.split("/at ")[1].split(" /to ")[1];

if (split[0].isEmpty() || split[1].isEmpty()) {
throw new DukeException("It seems that you've missed out the task description " +
"or the /at <when to when> segment!\n" +
"Please type in the 'event <something> /at <when to when>' format.");
if (description.isEmpty() || startDateTime.isEmpty() || endTime.isEmpty()) {
throw new DukeException("It seems that you've missed out the task description or the start and end duration!\n" +
"Please type in the 'event <something> /at <dd/MM/yyyy HHmm> /to <HHmm>' format.");
}

taskDescription = reformatLine("[Event] ", "at", taskDescription);
tasks.add(new Deadline(taskDescription));
startDateTime = reformatDateTime(startDateTime);
endTime = reformatTime(endTime);
line = description + "/at " + startDateTime + " /to " + endTime;
line = reformatLine("[Event] ", "at", "to", line);
tasks.add(new Event(line));
ui.printTaskAdded(tasks);
database.updateDatabase(tasks);

} catch (DukeException | IOException e) {
ui.printRedBorderlines(e.getMessage());
} catch (IndexOutOfBoundsException e) {
ui.printRedBorderlines("It seems that you've missed out the event time!\n" +
"Please type in something for <when> after 'deadline <something> /by'.");
ui.printRedBorderlines("It seems that you've missed out the event duration!\n" +
"Please type in something for <dd/MM/yyyy HHmm> after 'event <something> /at'.");
} catch (DateTimeParseException e) {
ui.printRedBorderlines("It seems that you didn't enter the time in the right format!\n" +
"Please type in the 'event <something> /at <dd/MM/yyyy HHmm>' format.");
}
}
}
41 changes: 41 additions & 0 deletions src/main/java/duke/commands/FindCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package duke.commands;

import duke.task.TaskList;
import duke.ui.Ui;

/**
* A command to print out all tasks in the list.
*/
public class FindCommand extends Command {
public static final String word = "find";

public FindCommand(String line, TaskList tasks, Ui ui) {
super(line, tasks, ui);
}

@Override
public void execute() {
String searchWord = line.trim();
if (tasks.size() == 0) {
ui.printBorderlines("It appears that you have no tasks! Perhaps you should start creating one?");
return;
}
if (searchWord.isEmpty()) {
ui.printBorderlines("Please type in the 'find <word>' format!");
return;
}
String matches = "Here are the tasks that matches '" + searchWord + "'!\n";
boolean hasMatch = false;
for (int i = 0; i < tasks.size(); i++) {
if (tasks.get(i).getDescription().toLowerCase().contains(searchWord)) {
matches = matches.concat((i + 1) + ". " + tasks.get(i).getDescription()) + "\n";
hasMatch = true;
}
}
if (!hasMatch) {
matches = "It appears that are no matches for '" + searchWord + "'!\n" +
"Perhaps you could try searching another word?";
}
ui.printBorderlines(matches);
}
}
10 changes: 5 additions & 5 deletions src/main/java/duke/commands/TodoCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,19 +13,19 @@
public class TodoCommand extends Command {
public static final String word = "todo";

public TodoCommand(String taskDescription, TaskList tasks, Ui ui, Database database) {
super(taskDescription, tasks, ui, database);
public TodoCommand(String line, TaskList tasks, Ui ui, Database database) {
super(line, tasks, ui, database);
}

@Override
public void execute() {
try {
if (taskDescription.isEmpty()) {
if (line.isEmpty()) {
throw new DukeException("It seems that you missed out the task description!\n" +
"Please type in the 'todo <something>' format.");
}
taskDescription = "[Todo] " + taskDescription;
tasks.add(new Todo(taskDescription));
line = "[Todo] " + line;
tasks.add(new Todo(line));
ui.printTaskAdded(tasks);
database.updateDatabase(tasks);

Expand Down
25 changes: 10 additions & 15 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
@@ -1,14 +1,6 @@
package duke.parser;

import duke.commands.Command;
import duke.commands.DeleteCommand;
import duke.commands.DoneCommand;
import duke.commands.DeadlineCommand;
import duke.commands.EventCommand;
import duke.commands.ExitCommand;
import duke.commands.ListCommand;
import duke.commands.InvalidCommand;
import duke.commands.TodoCommand;
import duke.commands.*; //IntelliJ keeps reducing all the commands to wildcard.
import duke.database.Database;
import duke.task.TaskList;
import duke.ui.Ui;
Expand All @@ -20,26 +12,29 @@ public class Parser {
public Command parseInput(String line, Ui ui, TaskList tasks, Database database) {

String commandWord = line.trim().split(" ")[0];
String taskDescription = line.substring(line.indexOf(" ") + 1).trim();
String lineWithoutCommand = line.substring(line.indexOf(" ") + 1).trim();

switch (commandWord) {
case ListCommand.word -> {
return new ListCommand(tasks, ui);
}
case FindCommand.word -> {
return new FindCommand(lineWithoutCommand, tasks, ui);
}
case DoneCommand.word -> {
return new DoneCommand(taskDescription, tasks, ui, database);
return new DoneCommand(lineWithoutCommand, tasks, ui, database);
}
case DeleteCommand.word -> {
return new DeleteCommand(taskDescription, tasks, ui, database);
return new DeleteCommand(lineWithoutCommand, tasks, ui, database);
}
case TodoCommand.word -> {
return new TodoCommand(taskDescription, tasks, ui, database);
return new TodoCommand(lineWithoutCommand, tasks, ui, database);
}
case DeadlineCommand.word -> {
return new DeadlineCommand(taskDescription, tasks, ui, database);
return new DeadlineCommand(lineWithoutCommand, tasks, ui, database);
}
case EventCommand.word -> {
return new EventCommand(taskDescription, tasks, ui, database);
return new EventCommand(lineWithoutCommand, tasks, ui, database);
}
case "bye", "exit", "" -> { // also exits when user input is empty
return new ExitCommand(ui);
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ public void printGreeting() {
+ "I can help you manage a list of tasks!\n"
+ "What you can tell me to do is listed below:\n"
+ " ⬡ Create a Todo task | " + colorGreen("todo <your task>\n")
+ " ⬢ Create a Deadline task | " + colorGreen("deadline <your task> /by <when>\n")
+ " ⬡ Create an Event task | " + colorGreen("event <your task> /at <when to when>\n")
+ " ⬢ Create a Deadline task | " + colorGreen("deadline <your task> /by <dd/MM/yyyy HHmm>\n")
+ " ⬡ Create an Event task | " + colorGreen("event <your task> /at <dd/MM/yyyy HHmm> /to <HHmm>\n")
+ " ⬢ Complete a task | " + colorGreen("done <task index number>\n")
+ " ⬡ Delete a task | " + colorGreen("delete <task index number>\n")
+ " ⬡ List down all tasks | " + colorGreen("list\n")
+ " ⬢ List down all tasks | " + colorGreen("list\n")
+ " ⬡ Find tasks with word | " + colorGreen("find <word>\n")
+ " ⬢ Exit my program | " + colorGreen("bye or hit Enter");
printBorderlines(greeting);
}
Expand Down

0 comments on commit a5c85af

Please sign in to comment.