Skip to content

Commit

Permalink
Merge pull request #2 from NereusWB922/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
  • Loading branch information
NereusWB922 authored Sep 10, 2023
2 parents 6b83a4d + bb3d81e commit 6d23cde
Show file tree
Hide file tree
Showing 14 changed files with 167 additions and 117 deletions.
11 changes: 2 additions & 9 deletions src/main/java/corgi/commands/AddTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,16 +27,9 @@ public class AddTaskCommand extends Command {
* @param target The task to be added.
* @param type The type of command (CommandType.TODO, CommandType.DEADLINE, or CommandType.EVENT).
*/
public AddTaskCommand(Task target, CommandType type) {
super(false, type);
public AddTaskCommand(Task target) {
super(false);
this.target = target;
if (type == CommandType.TODO) {
this.taskType = "todo";
} else if (type == CommandType.DEADLINE) {
this.taskType = "deadline";
} else {
this.taskType = "event";
}
}

/**
Expand Down
17 changes: 1 addition & 16 deletions src/main/java/corgi/commands/Command.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,20 +14,14 @@ public abstract class Command {
*/
private boolean isExit;

/**
* The type of command.
*/
private CommandType type;

/**
* Initializes a new Command instance with the specified exit flag and command type.
*
* @param isExit The flag indicating whether this command should exit the application.
* @param type The type of command.
*/
public Command(boolean isExit, CommandType type) {
public Command(boolean isExit) {
this.isExit = isExit;
this.type = type;
}

/**
Expand All @@ -50,13 +44,4 @@ public abstract String execute(TaskList list, TextRenderer renderer, Storage<Tas
public boolean isExit() {
return this.isExit;
}

/**
* Gets the type of this command.
*
* @return The type of command.
*/
public CommandType getCommandType() {
return this.type;
}
}
2 changes: 1 addition & 1 deletion src/main/java/corgi/commands/DeleteTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public class DeleteTaskCommand extends Command {
* @param targetIdx The index of the task to be deleted.
*/
public DeleteTaskCommand(int targetIdx) {
super(false, CommandType.DELETE);
super(false);
this.targetIdx = targetIdx;
}

Expand Down
2 changes: 1 addition & 1 deletion src/main/java/corgi/commands/ExitCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class ExitCommand extends Command {
* Initializes a new ExitCommand instance.
*/
public ExitCommand() {
super(true, CommandType.BYE);
super(true);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,10 @@ public class FindTasksContainKeywordCommand extends Command {
* @param target The target keyword.
*/
public FindTasksContainKeywordCommand(String target) {
super(false, CommandType.FIND);
super(false);
this.target = target;

// Define a predicate to filter tasks based on whether they contain the keyword.
this.predicate = t -> t.contains(target);
}

Expand All @@ -47,9 +49,9 @@ public String execute(TaskList list, TextRenderer renderer, Storage<Task> storag
TaskList tasksContainKeyword = list.filter(predicate);

if (tasksContainKeyword.isEmpty()) {
return renderer.showNoTaskContainsKeyword(this.target);
return renderer.showKeywordNotFound(this.target);
} else {
return renderer.showTasksContainKeyword(this.target, tasksContainKeyword.toString());
return renderer.showTasksWithKeyword(this.target, tasksContainKeyword.toString());
}
}
}
3 changes: 2 additions & 1 deletion src/main/java/corgi/commands/FindTasksOnDateCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,10 @@ public class FindTasksOnDateCommand extends Command {
* @param target The target date
*/
public FindTasksOnDateCommand(LocalDate target) {
super(false, CommandType.DATE);
super(false);
this.target = target;

// Define a predicate to filter tasks based on whether they are happening on the target date.
this.predicate = t -> {
if (t instanceof Deadline) {
Deadline d = (Deadline) t;
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/corgi/commands/ListTasksCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class ListTasksCommand extends Command {
* Initializes a new ListTasksCommand instance.
*/
public ListTasksCommand() {
super(false, CommandType.LIST);
super(false);
}

/**
Expand All @@ -28,7 +28,7 @@ public ListTasksCommand() {
@Override
public String execute(TaskList list, TextRenderer renderer, Storage<Task> storage) {
if (list.isEmpty()) {
return renderer.showNoTaskInList();
return renderer.showNoTaskFound();
} else {
return renderer.showTaskList(list.toString());
}
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/corgi/commands/MarkTaskCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public class MarkTaskCommand extends Command {
* @param status The new status of the task (true for done, false for undone).
* @param type The type of command (CommandType.MARK_DONE or CommandType.MARK_UNDONE).
*/
public MarkTaskCommand(int index, boolean status, CommandType type) {
super(false, type);
public MarkTaskCommand(int index, boolean status) {
super(false);
this.index = index;
this.status = status;
}
Expand Down
174 changes: 106 additions & 68 deletions src/main/java/corgi/parsers/CommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,19 @@ public Command parse(String fullCommand) throws InvalidCommandFormatException, I
command = newListCommand(inputs);
break;
case MARK:
command = newMarkCommand(inputs, true);
command = newMarkCommand(inputs);
break;
case UNMARK:
command = newMarkCommand(inputs, false);
command = newUnMarkCommand(inputs);
break;
case TODO:
command = newAddCommand(inputs, cmd);
command = newAddTodoCommand(inputs);
break;
case DEADLINE:
command = newAddCommand(inputs, cmd);
command = newAddDeadlineCommand(inputs);
break;
case EVENT:
command = newAddCommand(inputs, cmd);
command = newAddEventCommand(inputs);
break;
case DELETE:
command = newDeleteCommand(inputs);
Expand Down Expand Up @@ -100,19 +100,39 @@ private Command newListCommand(String[] inputs) throws InvalidCommandFormatExcep
return new ListTasksCommand();
}

private Command newMarkCommand(String[] inputs, boolean status) throws InvalidCommandFormatException {
private Command newMarkCommand(String[] inputs) throws InvalidCommandFormatException {
CommandType commandType = CommandType.MARK;
String commandFormat = commandType.getCommandFormat();

if (inputs.length == 1) {
throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: "
+ commandFormat);
}

try {
int index = Integer.parseInt(inputs[1]) - 1;
return new MarkTaskCommand(index, true);
} catch (NumberFormatException e) {
throw new InvalidCommandFormatException("Please provide a valid task number!" + "\nFormat: "
+ commandFormat);
}
}

private Command newUnMarkCommand(String[] inputs) throws InvalidCommandFormatException {
CommandType commandType = CommandType.UNMARK;
String commandFormat = commandType.getCommandFormat();

if (inputs.length == 1) {
throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: "
+ (status ? CommandType.MARK.getCommandFormat() : CommandType.UNMARK.getCommandFormat()));
+ commandFormat);
}

try {
int index = Integer.parseInt(inputs[1]) - 1;
return new MarkTaskCommand(index, status,
status ? CommandType.MARK : CommandType.UNMARK);
return new MarkTaskCommand(index, true);
} catch (NumberFormatException e) {
throw new InvalidCommandFormatException("Please provide a valid task number!" + "\nFormat: "
+ (status ? CommandType.MARK.getCommandFormat() : CommandType.UNMARK.getCommandFormat()));
+ commandFormat);
}
}

Expand Down Expand Up @@ -163,90 +183,108 @@ private Command newFindCommand(String[] inputs) throws InvalidCommandFormatExcep
return new FindTasksContainKeywordCommand(keyword);
}

private Command newAddCommand(String[] inputs, CommandType type) throws InvalidCommandFormatException {
private Command newAddTodoCommand(String[] inputs) throws InvalidCommandFormatException {
if (inputs.length == 1) {
throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: "
+ type.getCommandFormat());
+ CommandType.TODO.getCommandFormat());
}

String taskInfo = inputs[1];

if (type == CommandType.TODO) {
Task target = new ToDo(taskInfo);
Task target = new ToDo(taskInfo);

assert target != null : "New Todo task cannot be null.";
assert target != null : "New Todo task cannot be null.";

return new AddTaskCommand(target, CommandType.TODO);
return new AddTaskCommand(target);
}

} else if (type == CommandType.DEADLINE) {
// todo: check number of /by
String[] deadlineInfos = taskInfo.split(" /by ");
private Command newAddDeadlineCommand(String[] inputs) throws InvalidCommandFormatException {
String commandFormat = CommandType.DEADLINE.getCommandFormat();

if (inputs.length == 1) {
throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: "
+ commandFormat);
}

if (deadlineInfos.length == 1) {
throw new InvalidCommandFormatException("Invalid format!" + "\nFormat: "
+ type.getCommandFormat());
}
String taskInfo = inputs[1];

String deadlineDesc = deadlineInfos[0];
LocalDate by = null;
// todo: check number of /by
String[] deadlineInfos = taskInfo.split(" /by ");

try {
by = LocalDate.parse(deadlineInfos[1], Task.DATE_INPUT_FORMATTER);
} catch (DateTimeParseException e) {
throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: "
+ type.getCommandFormat());
}
if (deadlineInfos.length == 1) {
throw new InvalidCommandFormatException("Missing deadline!" + "\nFormat: "
+ commandFormat);
}

assert by != null : "Date cannot be null.";
String deadlineDesc = deadlineInfos[0];
LocalDate by = null;

Task target = new Deadline(deadlineDesc, by);
try {
by = LocalDate.parse(deadlineInfos[1], Task.DATE_INPUT_FORMATTER);
} catch (DateTimeParseException e) {
throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: "
+ commandFormat);
}

assert target != null : "New Deadline task cannot be null.";
assert by != null : "Date cannot be null.";

return new AddTaskCommand(target, CommandType.DEADLINE);
Task target = new Deadline(deadlineDesc, by);

} else {
// todo: check number of /from, /to, check order
String[] eventInfos = taskInfo.split(" /from ");
assert target != null : "New Deadline task cannot be null.";

if (eventInfos.length < 2) {
throw new InvalidCommandFormatException("Missing /from argument." + "\nFormat: "
+ type.getCommandFormat());
} else if (eventInfos.length > 2) {
throw new InvalidCommandFormatException("Only one /from argument is needed." + "\nFormat: "
+ type.getCommandFormat());
}
return new AddTaskCommand(target);
}

String eventDesc = eventInfos[0];
String[] eventDuration = eventInfos[1].split(" /to ");
private Command newAddEventCommand(String[] inputs) throws InvalidCommandFormatException {
String commandFormat = CommandType.EVENT.getCommandFormat();

if (inputs.length == 1) {
throw new InvalidCommandFormatException("No argument is provided!" + "\nFormat: "
+ commandFormat);
}

if (eventDuration.length < 2) {
throw new InvalidCommandFormatException("Missing /to argument!" + "\nFormat: "
+ type.getCommandFormat());
} else if (eventDuration.length > 2) {
throw new InvalidCommandFormatException("Only one /to argument is needed." + "\nFormat: "
+ type.getCommandFormat());
}
String taskInfo = inputs[1];

LocalDate from = null;
LocalDate to = null;
// todo: check number of /from, /to, check order
String[] eventInfos = taskInfo.split(" /from ");

try {
from = LocalDate.parse(eventDuration[0], Task.DATE_INPUT_FORMATTER);
to = LocalDate.parse(eventDuration[1], Task.DATE_INPUT_FORMATTER);
} catch (DateTimeParseException e) {
throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: "
+ type.getCommandFormat());
}
if (eventInfos.length < 2) {
throw new InvalidCommandFormatException("Missing /from argument." + "\nFormat: "
+ commandFormat);
} else if (eventInfos.length > 2) {
throw new InvalidCommandFormatException("Only one /from argument is needed." + "\nFormat: "
+ commandFormat);
}

assert from != null : "Date cannot be null.";
assert to != null : "Date cannot be null.";
String eventDesc = eventInfos[0];
String[] eventDuration = eventInfos[1].split(" /to ");

Task target = new Event(eventDesc, from, to);
if (eventDuration.length < 2) {
throw new InvalidCommandFormatException("Missing /to argument!" + "\nFormat: "
+ commandFormat);
} else if (eventDuration.length > 2) {
throw new InvalidCommandFormatException("Only one /to argument is needed." + "\nFormat: "
+ commandFormat);
}

assert target != null : "New Event task cannot be null.";
LocalDate from = null;
LocalDate to = null;

return new AddTaskCommand(target, CommandType.EVENT);
try {
from = LocalDate.parse(eventDuration[0], Task.DATE_INPUT_FORMATTER);
to = LocalDate.parse(eventDuration[1], Task.DATE_INPUT_FORMATTER);
} catch (DateTimeParseException e) {
throw new InvalidCommandFormatException("Invalid date format!" + "\nFormat: "
+ commandFormat);
}

assert from != null : "Date cannot be null.";
assert to != null : "Date cannot be null.";

Task target = new Event(eventDesc, from, to);

assert target != null : "New Event task cannot be null.";

return new AddTaskCommand(target);
}
}
Loading

0 comments on commit 6d23cde

Please sign in to comment.