Skip to content

Commit

Permalink
Implement B-FixedDurationTasks
Browse files Browse the repository at this point in the history
  • Loading branch information
QianChangru committed Sep 16, 2023
1 parent ed26f34 commit a146f5a
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 0 deletions.
37 changes: 37 additions & 0 deletions src/main/java/duke/FixedDurationTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package duke;

/**
* Encapsulates FixedDurationTask which are Tasks with a fixed duration.
*/
public class FixedDurationTask extends Task {
private String duration;

public FixedDurationTask(String description, String duration) {
super(description);
this.duration = duration;
}

public FixedDurationTask(String description, boolean isDone, String duration) {
super(description, isDone);
this.duration = duration;
}

/**
* Returns a String representation for FixedDurationTask for output.
*/
@Override
public String toString() {
return "[F]" + super.toString() + " (for: "
+ this.duration + ")";
}

/**
* Returns a String representation of FixedDurationTask for storage.
*/
@Override
public String toTxt() {
return "F | " + super.toTxt()
+ " | " + this.duration;
}

}
15 changes: 15 additions & 0 deletions src/main/java/duke/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ public String parse() {
} else if (command.startsWith("event")) {
Event newEvent = createEventFromCommand();
return this.taskList.addTask(newEvent);
} else if (command.startsWith("fixedduration")) {
FixedDurationTask newFixedDurationTask = createFixedDurationTaskFromCommand();
return this.taskList.addTask(newFixedDurationTask);
} else if (command.startsWith("delete")) {
return this.taskList.deleteTask(Integer.valueOf(command.split(" ")[1]) - 1);
} else if (command.startsWith("bye")) {
Expand All @@ -54,6 +57,18 @@ public String parse() {
return "Invalid command.";
}

/**
* Creates the new FixedDurationTask from the command.
*
* @return The new FixedDurationTask.
*/
private FixedDurationTask createFixedDurationTaskFromCommand() {
String name = command.split(" /for ", 2)[0].split(" ", 2)[1];
String duration = command.split(" /for ", 2)[1];
FixedDurationTask newFixedDurationTask = new FixedDurationTask(name, duration);
return newFixedDurationTask;
}

/**
* Creates the new Event from the command.
*
Expand Down
15 changes: 15 additions & 0 deletions src/main/java/duke/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -84,12 +84,27 @@ private Task storageToTask(String input) throws DukeException {
return this.createDeadlineFromStorage(input, description, isComplete);
} else if (taskType.equals("E")) {
return this.createEventFromStorage(input, description, isComplete);
} else if (taskType.equals("F")) {
return this.createFixedDurationTaskFromStorage(input, description, isComplete);
} else {
throw new DukeException("Unknown task type.");
}
}


/**
* Creates the FixedDurationTask from the storage input.
*
* @param input The input line.
* @param description The description of the FixedDurationTask.
* @param isComplete Whether the FixedDurationTask is completed.
* @return The Event.
*/
private FixedDurationTask createFixedDurationTaskFromStorage(String input, String description, boolean isComplete) {
String duration = input.split(" \\| ")[3];
return new FixedDurationTask(description, isComplete, duration);
}

/**
* Creates the Event from the storage input.
*
Expand Down
1 change: 1 addition & 0 deletions src/main/java/duke/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,5 @@ public String findTask(String keyword) {
}
return Ui.listMatchingTasks(result);
}

}

0 comments on commit a146f5a

Please sign in to comment.