Skip to content

Commit

Permalink
Merge pull request #35 from Exprescode/FixedDurationTasks
Browse files Browse the repository at this point in the history
Add Fixed Duration Tasks
  • Loading branch information
Exprescode authored Sep 15, 2019
2 parents 7f3b97b + 6566fc2 commit fa9cfcb
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 5 deletions.
14 changes: 10 additions & 4 deletions src/main/java/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,7 @@
import process.*;

import process.DukeException;
import task.Deadline;
import task.Event;
import task.TaskList;
import task.Todo;
import task.*;

/**
* Represents a command that adds an item to tasks
Expand All @@ -14,6 +11,7 @@ public class AddCommand extends Command {
private String description;
private String tasktype;
private String datetime;
private int durationHour, durationMinute;
/**
* Creates a new AddCommand object with the given type of task and description
* @param tasktype The task type
Expand All @@ -34,6 +32,13 @@ public AddCommand(String tasktype, String description, String datetime) {
this.tasktype = tasktype;
this.datetime = datetime;
}

public AddCommand(String description, int durationHour, int durationMinute ){
this.tasktype = "task";
this.description = description;
this.durationHour = durationHour;
this.durationMinute = durationMinute;
}
/**
* Executes the AddCommand and saves changes to storage
* @param tasks the task list
Expand All @@ -45,6 +50,7 @@ public String execute(TaskList tasks, Ui ui, Storage storage) throws DukeExcepti
if (tasktype.equals("todo")) tasks.add(new Todo(description, false));
else if (tasktype.equals("deadline")) tasks.add(new Deadline(description, datetime, false));
else if (tasktype.equals("event")) tasks.add(new Event(description, datetime, false));
else if (tasktype.equals("task")) tasks.add(new FixedTask(description, false, durationHour, durationMinute));
else throw new DukeException("add error");
storage.save(tasks);
return ui.showTaskAdded(tasks.get(tasks.size()-1).toString(), tasks.size());
Expand Down
13 changes: 12 additions & 1 deletion src/main/java/process/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@

import java.util.ArrayList;
import java.util.Arrays;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
* Represents a parser to make sense of user input and translate it to commands for Duke
*/
Expand All @@ -16,7 +19,7 @@ public class Parser {
* @throws DukeException if Duke cannot make sense of the input
*/
public static Command parse(String input) throws DukeException { //input validation
ArrayList<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule"));
ArrayList<String> command_list = new ArrayList<String>(Arrays.asList("bye", "list", "find", "delete", "done", "todo", "deadline", "event","reschedule","task"));
String operation;
String date;
int index =-1;
Expand Down Expand Up @@ -62,6 +65,14 @@ public static Command parse(String input) throws DukeException { //input validat
date = input.substring(at_index + 5).trim();
if (date.isBlank()) throw new DukeException("datetime");
return new AddCommand("event", arg1, date);
} else if (operation.equals("task")) {
int delimiterIndex = input.indexOf(" /need ");
if (delimiterIndex == -1) throw new DukeException("Missing task delimiter. (/need)");
arg1 = input.substring(4, delimiterIndex).trim();
if (arg1.isBlank()) throw new DukeException("Missing task description.");
Matcher m = Pattern.compile("^(\\d+) (\\d+)$").matcher(input.substring(delimiterIndex + 7).trim());
if (!m.find()) throw new DukeException("Invalid task argument need.\ntask <description> /need <hour(s)> <minute(s)>");
return new AddCommand(arg1, Integer.parseInt(m.group(1)), Integer.parseInt(m.group(2)));
} else if (operation.equals("bye")) {
return new ExitCommand();
} else if (operation.equals("list")) {
Expand Down
58 changes: 58 additions & 0 deletions src/main/java/task/FixedTask.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package task;

import process.DukeException;

public class FixedTask extends Task {
int durationHour, durationMinute;

/**
* Creates FixedTask object that represents a fixed duration task.
* @param desc Task description.
* @param check Mark task as done.
* @param durationHour Duration require to complete task. (Hours)
* @param durationMinute Duration require to complete task. (Minutes)
* @throws DukeException
*/
public FixedTask(String desc, boolean check, int durationHour, int durationMinute) throws DukeException {
super(desc, check);
checkHour(durationHour);
this.durationHour = durationHour;
checkMinute(durationMinute);
this.durationMinute = durationMinute;
}

/**
* Checks for valid hour representation.
* @param hour Integer hour to validate.
* @throws DukeException Hour is not a positive neutral number.
*/
private void checkHour(int hour) throws DukeException {
if (hour < 0) {
throw new DukeException("FixedTask argument hour must be a positive neutral number.");
}
}

/**
* Checks for valid minute representation.
* @param minute Integer minute to validate.
* @throws DukeException Minute is not a positive neutral number less than 60.
*/
private void checkMinute(int minute) throws DukeException {
if (minute >= 60) {
throw new DukeException("FixedTask argument minute can be simplified to hours.");
}
if (minute < 0) {
throw new DukeException("FixedTask argument minute must be a positive neutral number.");
}
}

/**
* Formats object variables to text form.
* @return Printable text representation of object.
*/
public String toString() {
String printHour = durationHour > 0 ? durationHour + " Hour" + (durationHour > 1 ? "s" : "") : "";
String printMinute = durationMinute > 0 ? durationMinute + " Minute" + (durationMinute > 1 ? "s" : "" ) : "";
return "[F]" + super.toString() + " (Requires: " + printHour + (durationHour > 0 && durationMinute > 0 ? " " : "") + printMinute + ")";
}
}

0 comments on commit fa9cfcb

Please sign in to comment.