Skip to content

Commit

Permalink
Merge pull request #2 from ChanWeiJie/branch-A-CodeQuality
Browse files Browse the repository at this point in the history
Update code quality
  • Loading branch information
ChanWeiJie authored Feb 6, 2022
2 parents f0eb842 + 78cbca1 commit e8b3278
Show file tree
Hide file tree
Showing 4 changed files with 67 additions and 117 deletions.
68 changes: 32 additions & 36 deletions src/main/java/duke/functionality/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@ public class Parser {
private static final int TODO_OFFSET = 4;
private static final int DEADLINE_OFFSET = 8;
private static final int INPUT_OFFSET = 3;
private static final int FIRST_INPUT = 0;
private static final int SECOND_INPUT = 1;
private static final int THIRD_INPUT = 2;

/**
* Returns the formatted date.
Expand Down Expand Up @@ -75,15 +78,21 @@ public static String timeToString(LocalTime input) {
return time;
}

private static void checkDescriptionLength(String description, String command) throws IncompleteCommandException {
if (description.length() == 0) {
throw new IncompleteCommandException(command);
}
}

/**
* Returns the respective command from user input.
* @param input user input. Eg, "todo run".
* @return the command.
* @throws DukeException when user input for date and time are not of the correct form.
*/
public static Command parse(String input) throws DukeException {
String[] inputSplit = input.split(" "); //split input by space
String command = inputSplit[0];
String[] inputSplit = input.split(" ");
String command = inputSplit[FIRST_INPUT];
if (command.equals("bye")) {
return new ExitCommand();

Expand All @@ -92,67 +101,54 @@ public static Command parse(String input) throws DukeException {

} else if (command.equals("todo")) {
String description = input.substring(TODO_OFFSET).trim();

if (description.length() == 0) {
throw new IncompleteCommandException(command);
}
checkDescriptionLength(description, command);
return new AddCommand(new Todo(description));

} else if (command.equals("deadline")) {
String[] inputSlash = input.split("/");
String description = inputSlash[0].substring(DEADLINE_OFFSET).trim();

if (description.length() == 0) {
throw new IncompleteCommandException(command);
}
String description = inputSlash[FIRST_INPUT].substring(DEADLINE_OFFSET).trim();
checkDescriptionLength(description, command);

String durationInput = inputSlash[1].substring(INPUT_OFFSET);
String durationInput = inputSlash[SECOND_INPUT].substring(INPUT_OFFSET);
String[] splitDuration = durationInput.split(" ");
LocalDate date;
LocalTime time;

try {
date = formatDate(splitDuration[0]);
time = formatTime(splitDuration[1]);
LocalDate date = formatDate(splitDuration[FIRST_INPUT]);
LocalTime time = formatTime(splitDuration[SECOND_INPUT]);
return new AddCommand(new Deadline(description, date, time));

} catch (DateTimeParseException e) {
throw new DukeException("The expected input form is deadline xxx /by yyyy-mm-dd hhmm");
}

return new AddCommand(new Deadline(description, date, time));

} else if (command.equals("event")) {
String[] inputSlash = input.split("/");
String description = inputSlash[0].substring(EVENT_OFFSET).trim();
String description = inputSlash[FIRST_INPUT].substring(EVENT_OFFSET).trim();
checkDescriptionLength(description, command);

if (description.length() == 0) {
throw new IncompleteCommandException(command);
}

String durationInput = inputSlash[1].substring(INPUT_OFFSET);
String durationInput = inputSlash[SECOND_INPUT].substring(INPUT_OFFSET);
String[] splitDuration = durationInput.split(" ");
LocalDate date;
LocalTime startTime;
LocalTime endTime;

try {
date = formatDate(splitDuration[0]);
startTime = formatTime(splitDuration[1]);
endTime = formatTime(splitDuration[2]);
LocalDate date = formatDate(splitDuration[FIRST_INPUT]);
LocalTime startTime = formatTime(splitDuration[SECOND_INPUT]);
LocalTime endTime = formatTime(splitDuration[THIRD_INPUT]);
return new AddCommand(new Event(description, date, startTime, endTime));
} catch (DateTimeParseException e) {
throw new DukeException("The expected input form is event xxx /at yyyy-mm-dd hhmm hhmm");
}

return new AddCommand(new Event(description, date, startTime, endTime));

} else if (command.equals("mark")) {
return new MarkCommand(Integer.parseInt(inputSplit[1]));
return new MarkCommand(Integer.parseInt(inputSplit[SECOND_INPUT]));

} else if (command.equals("unmark")) {
return new UnmarkCommand(Integer.parseInt(inputSplit[1]));
return new UnmarkCommand(Integer.parseInt(inputSplit[SECOND_INPUT]));

} else if (command.equals("delete")) {
return new DeleteCommand(Integer.parseInt(inputSplit[1]));
return new DeleteCommand(Integer.parseInt(inputSplit[SECOND_INPUT]));

} else if (command.equals("find")) {
return new FindCommand(inputSplit[1]);
return new FindCommand(inputSplit[SECOND_INPUT]);

} else if (command.equals("")) {
throw new BlankCommandException();
Expand Down
51 changes: 31 additions & 20 deletions src/main/java/duke/functionality/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,12 @@
public class Storage {
protected static String pwd;
protected static String path;
private static final int FIRST_INPUT = 0;
private static final int SECOND_INPUT = 1;
private static final int THIRD_INPUT = 2;
private static final int FOURTH_INPUT = 3;
private static final int FIFTH_INPUT = 4;
private static final int SIXTH_INPUT = 5;

/**
* Constructor for Storage class.
Expand Down Expand Up @@ -69,34 +75,39 @@ public TaskList readFileDataAndStoreInList(File file) throws FileNotFoundExcepti
TaskList newTaskList = new TaskList();
while ((sc.hasNextLine())) {
String input = sc.nextLine();
String[] inputSplit = input.split("\\|"); //split input by |
String task = inputSplit[0];
int mark = Integer.parseInt(inputSplit[1]);
String[] inputSplit = input.split("\\|");
String task = inputSplit[FIRST_INPUT];
int mark = Integer.parseInt(inputSplit[SECOND_INPUT]);
if (task.equals("T")) {
Todo tempTask = new Todo(inputSplit[2]);
if (mark == 1) {
tempTask.setTaskDone();
}
newTaskList.addToList(tempTask);
Todo tempTask = new Todo(inputSplit[THIRD_INPUT]);
newTaskList.addToList(checkTaskDone(mark, tempTask));

} else if (task.equals("D")) {
Deadline tempTask = new Deadline(inputSplit[2], Parser.formatDate(inputSplit[3]),
Parser.formatTime(inputSplit[4]));
if (mark == 1) {
tempTask.setTaskDone();
}
newTaskList.addToList(tempTask);
Deadline tempTask = new Deadline(inputSplit[THIRD_INPUT], Parser.formatDate(inputSplit[FOURTH_INPUT]),
Parser.formatTime(inputSplit[FIFTH_INPUT]));
newTaskList.addToList(checkTaskDone(mark, tempTask));

} else if (task.equals("E")) {
Event tempTask = new Event(inputSplit[2], Parser.formatDate(inputSplit[3]) ,
Parser.formatTime(inputSplit[4]), Parser.formatTime(inputSplit[5]));
if (mark == 1) {
tempTask.setTaskDone();
}
newTaskList.addToList(tempTask);
Event tempTask = new Event(inputSplit[THIRD_INPUT], Parser.formatDate(inputSplit[FOURTH_INPUT]) ,
Parser.formatTime(inputSplit[FIFTH_INPUT]), Parser.formatTime(inputSplit[SIXTH_INPUT]));
newTaskList.addToList(checkTaskDone(mark, tempTask));
}
}
return newTaskList;
}

/**
* Returns nothing, but used to check if a Task is done or not.
* @param mark indicator, 1 is done 0 is not done.
* @param task the Task object to be checked.
*/
public Task checkTaskDone(int mark, Task task) {
if(mark == 1){
task.setTaskDone();
}
return task;
}

/**
* Returns a crafted output to be stored in the text file.
* @param task the task created in Parser class.
Expand Down
8 changes: 4 additions & 4 deletions src/main/java/duke/functionality/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public Task deleteTask(int taskNum) {
* @return the specified task to be marked.
*/
public Task markTask(int taskNum) {
int actualTaskNum = taskNum - 1; //minus 1 as list index is from 0
Task markedTask = taskList.get(actualTaskNum); // get the task from the array
int actualTaskNum = taskNum - 1;
Task markedTask = taskList.get(actualTaskNum);
markedTask.setTaskDone();
return markedTask;
}
Expand All @@ -44,9 +44,9 @@ public Task markTask(int taskNum) {
* @param taskNum an indicator to the index of taskList.
* @return the specified task to be unmarked.
*/
public Task unMarkTask(int taskNum) {
public Task unmarkTask(int taskNum) {
int actualTaskNum = taskNum - 1;
Task unMarkedtask = taskList.get(actualTaskNum); // get the task from the array
Task unMarkedtask = taskList.get(actualTaskNum);
unMarkedtask.setTaskNotDone();
return unMarkedtask;
}
Expand Down
57 changes: 0 additions & 57 deletions src/main/java/duke/functionality/Ui.java

This file was deleted.

0 comments on commit e8b3278

Please sign in to comment.