Skip to content

Commit

Permalink
Refactor code rename variables
Browse files Browse the repository at this point in the history
  • Loading branch information
vanGoghhh committed Sep 15, 2020
1 parent 8071606 commit 1e4fb07
Show file tree
Hide file tree
Showing 10 changed files with 213 additions and 156 deletions.
53 changes: 36 additions & 17 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,57 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* Class to represent a deadline object.
* @author vanGoghhh
*/

public class Deadline extends Task {
public class Deadline extends TimedTask {

private LocalDate by;
/**
* Constructor for a timed task.
*
* @param description details of the task.
* @param dueDate deadline of the task.
*/
public Deadline(String description, LocalDate dueDate) {
super(description, dueDate);
}

/**
* Constructor for deadline class.
* @param description description of the deadline.
* @param by date that the deadline is dued.
* Method to convert deadline to a file friendly format.
* @return string representation of file format.
*/
public Deadline(String description, LocalDate by) {
super(description);
this.by = by;
@Override
protected String inputInFile() {
return "D//" + this.getTaskStatus() + "//"
+ super.getDescription() + "//" + this.getTaskDeadline();
}

/**
* Updates the task with new date.
* @param newDueDate new date to update with.
* @return task with new date.
*/
@Override
protected LocalDate getTaskDeadline() {
return by;
protected Deadline updateTimedTaskDeadline(LocalDate newDueDate) {
Deadline newDeadline = new Deadline(super.getDescription(), newDueDate);
if (this.getStatus()) {
newDeadline.markAsDone();
}
return newDeadline;
}

/**
* Prints the deadline object.
* @return string representation of a deadline.
* Updates the task with new description
* @param newDescription new task description.
* @return task with new description.
*/
@Override
public String toString() {
return "[D]" + super.toString() + " (by: "
+ by.format(DateTimeFormatter.ofPattern("d MMM uuuu"))
+ ")";
protected Deadline updateTaskDescription(String newDescription) {
Deadline newDeadline = new Deadline(newDescription, super.getTaskDeadline());
if (this.getStatus()) {
newDeadline.markAsDone();
}
return newDeadline;
}
}
50 changes: 33 additions & 17 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -1,40 +1,56 @@
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
* Class to represent an event object.
* @author vanGoghhh
*/

public class Event extends Task {

private LocalDate at;
public class Event extends TimedTask {

/**
* Constructor for event class.
* @param description description of the event.
* @param at duedate of the event.
* @param dueDate duedate of the event.
*/
public Event(String description, LocalDate dueDate) {
super(description, dueDate);
}

/**
* Method to convert deadline to a file friendly format.
* @return string representation of file format.
*/
public Event(String description, LocalDate at) {
super(description);
this.at = at;
@Override
protected String inputInFile() {
return "E//" + this.getTaskStatus() + "//"
+ super.getDescription() + "//" + this.getTaskDeadline();
}

/**
* Returns deadline for the event.
* @return deadline of event.
* Updates the task with new date.
* @param newDueDate new date to update with.
* @return task with new date.
*/
@Override
protected LocalDate getTaskDeadline() {
return at;
protected Event updateTimedTaskDeadline(LocalDate newDueDate) {
Event newEvent = new Event(super.getDescription(), newDueDate);
if (this.getStatus()) {
newEvent.markAsDone();
}
return newEvent;
}

/**
* Prints the event object.
* @return string representation of the event object.
* Updates the task with new description
* @param newDescription new task description.
* @return task with new description.
*/
@Override
public String toString() {
return "[E]" + super.toString() + " (at: "
+ at.format(DateTimeFormatter.ofPattern("d MMM uuuu")) + ")";
protected Event updateTaskDescription(String newDescription) {
Event newEvent = new Event(newDescription, super.getTaskDeadline());
if (this.getStatus()) {
newEvent.markAsDone();
}
return newEvent;
}
}
61 changes: 21 additions & 40 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,16 @@
* @author vanGoghhh
*/

public class Task {
public abstract class Task {

protected String description;
protected boolean isDone;
protected boolean isBeingUpdated = false;
private String description;
private boolean isDone;

/**
* Constructor for Task.
* @param description description of the task object.
*/
public Task(String description) {
protected Task(String description) {
this.description = description;
this.isDone = false;
}
Expand All @@ -24,7 +23,7 @@ public Task(String description) {
* Return icon to indicate whether a task is done.
* @return a "check" or a "cross" depending on whether the task is done.
*/
public String getStatusIcon() {
protected String getStatusIcon() {
return (isDone ? "\u2713" : "\u2718");
}

Expand All @@ -35,33 +34,39 @@ public void markAsDone() {
this.isDone = true;
}

/**
* Mark a task as not done.
*/
public void markNotDone() {
this.isDone = false;
}

/**
* Get the status of a task.
* @return boolean
*/
public boolean getStatus() {
protected boolean getStatus() {
return this.isDone;
}

/**
* Get task status in the form of an integer.
* @return 1 or 0 depending on status on task.
*/
private int getTaskStatus() {
protected int getTaskStatus() {
return this.isDone ? 1 : 0;
}

protected void toBeUpdated() {
isBeingUpdated = true;
/**
* Returns description of the task.
* @return description of the task.
*/
protected String getDescription() {
return this.description;
}

abstract protected LocalDate getTaskDeadline();

abstract protected Task updateTaskDescription(String newDescription);

abstract protected Task updateTimedTaskDeadline(LocalDate newDueDate);

abstract protected String inputInFile();

/**
* Prints the task object.
* @return string representation of a task object.
Expand All @@ -71,28 +76,4 @@ public String toString() {
return "[" + this.getStatusIcon() + "] " + this.description;
}

protected LocalDate getTaskDeadline() {
return null;
}

/**
* Transform the task into a file friendly format.
* @return string representation of task in file format.
*/
protected String inputInFile() {
if (this instanceof Todo) {
return "T//" + this.getTaskStatus() + "//"
+ this.description;
}
if (this instanceof Event) {
return "E//" + this.getTaskStatus() + "//"
+ this.description + "//" + ((Event) this).getTaskDeadline();
}
if (this instanceof Deadline) {
return "D//" + this.getTaskStatus() + "//"
+ this.description + "//" + ((Deadline) this).getTaskDeadline();
}
return " ";
}

}
68 changes: 20 additions & 48 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import java.time.LocalDate;
import java.time.format.DateTimeParseException;
import java.util.ArrayList;
import java.util.stream.Collectors;

Expand Down Expand Up @@ -80,7 +81,7 @@ protected long checkTasksLeft() {
*/
protected ArrayList<Task> findTask(String keyWord) {
ArrayList<Task> foundTasks = new ArrayList<>();
foundTasks = this.taskList.stream().filter(task -> task.description.equals(keyWord))
foundTasks = this.taskList.stream().filter(task -> task.getDescription().equals(keyWord))
.collect(Collectors.toCollection(ArrayList::new));
return foundTasks;
}
Expand Down Expand Up @@ -116,63 +117,34 @@ protected ArrayList<Task> sortTask() {
}

/**
* Retrieves task to be updated.
*
* @param taskNumber index of the updating tasks.
* @return the task to be updated.
*/
protected Task getTaskToUpdate(int taskNumber) {
assert (taskNumber >= 1) : "invalid task number";
return this.taskList.get(taskNumber - 1);
}

/**
* Returns the updated version of a task.
*
* @param detailToUpdate string containing what to update.
* Updates existing task with new task.
* @param updatingTask task to be updated.
* @param updateDetails new details to update the task with.
* @param taskIndex index of task in the task list.
* @return new updated task.
* @throws DukeException
*/
protected Task getUpdatedTask(String detailToUpdate) throws DukeException {
protected Task updateTask(Task updatingTask, String updateDetails, int taskIndex) throws DukeException {
try {
Task updatingTask = this.taskList.stream()
.filter(task -> task.isBeingUpdated)
.findFirst()
.get();
String[] updateDetails = detailToUpdate.split(" ", 2);
if (updateDetails[0].equals("date")) {
LocalDate newDate = LocalDate.parse(updateDetails[1]);
if (updatingTask instanceof Deadline) {
return new Deadline(updatingTask.description, newDate);
} else {
return new Event(updatingTask.description, newDate);
}
} else if (updateDetails[0].equals("desc")) {
String newDescription = updateDetails[1];
if (updatingTask instanceof Deadline) {
return new Deadline(newDescription, ((Deadline) updatingTask).getTaskDeadline());
} else if (updatingTask instanceof Event) {
return new Event(newDescription, ((Event) updatingTask).getTaskDeadline());
String[] updatingDetails = updateDetails.split(" ", 2);
String updateType = updatingDetails[0];
Task updatedTask;
if (updateType.equals("desc")) {
updatedTask = updatingTask.updateTaskDescription(updatingDetails[1]);
} else if (updateType.equals("date")) {
if (updatingTask instanceof TimedTask) {
LocalDate newDueDate = LocalDate.parse(updatingDetails[1]);
updatedTask = updatingTask.updateTimedTaskDeadline(newDueDate);
} else {
return new Todo(newDescription);
throw new InvalidCommandException();
}
} else {
throw new InvalidCommandException();
}

} catch (IndexOutOfBoundsException e) {
taskList.set(taskIndex - 1, updatedTask);
return updatedTask;
} catch (IndexOutOfBoundsException | DateTimeParseException e) {
throw new InvalidCommandException();
}
}

/**
* Updates the tasklist with the updated task.
*
* @param updatedTask the new updated task.
*/
protected void updateTask(Task updatedTask) {
this.taskList = this.taskList.stream()
.map(task -> task.isBeingUpdated ? updatedTask : task)
.collect(Collectors.toCollection(ArrayList::new));
}
}

0 comments on commit 1e4fb07

Please sign in to comment.