Skip to content

Commit

Permalink
Merge pull request #43 from Exprescode/master
Browse files Browse the repository at this point in the history
Update FixedTask
  • Loading branch information
Exprescode authored Sep 19, 2019
2 parents 6d96d42 + 6b5774a commit cdb80e5
Show file tree
Hide file tree
Showing 5 changed files with 99 additions and 38 deletions.
9 changes: 4 additions & 5 deletions src/main/java/command/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public class AddCommand extends Command {
private String description;
private String tasktype;
private String datetime;
private int durationHour, durationMinute;
private int duration;
/**
* Creates a new AddCommand object with the given type of task and description
* @param tasktype The task type
Expand All @@ -33,11 +33,10 @@ public AddCommand(String tasktype, String description, String datetime) {
this.datetime = datetime;
}

public AddCommand(String description, int durationHour, int durationMinute ){
public AddCommand(String description, int duration){
this.tasktype = "task";
this.description = description;
this.durationHour = durationHour;
this.durationMinute = durationMinute;
this.duration = duration;
}
/**
* Executes the AddCommand and saves changes to storage
Expand Down Expand Up @@ -69,7 +68,7 @@ else if (tasktype.equals("deadline")) {
}
}
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 if (tasktype.equals("task")) tasks.add(new FixedTask(description, false, duration));
else throw new DukeException("add error");
storage.save(tasks);
return ui.showTaskAdded(tasks.get(tasks.size()-1).toString(), tasks.size());
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/process/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,13 @@ public static Command parse(String input) throws DukeException { //input validat
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)));
int arg2;
try{
arg2 = Integer.parseInt(input.substring(delimiterIndex + 7).trim());
}catch(NumberFormatException e){
throw new DukeException("Invalid task argument need.\ntask <description> /need <minute(s)>");
}
return new AddCommand(arg1, arg2);
} else if (operation.equals("bye")) {
return new ExitCommand();
} else if (operation.equals("list")) {
Expand Down
68 changes: 38 additions & 30 deletions src/main/java/task/FixedTask.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,56 +3,64 @@
import process.DukeException;

public class FixedTask extends Task {
int durationHour, durationMinute;
int duration;

/**
* 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)
*
* @param desc Task description.
* @param check Mark task as done.
* @param duration Duration require to complete task. (Minutes)
* @throws DukeException
*/
public FixedTask(String desc, boolean check, int durationHour, int durationMinute) throws DukeException {
public FixedTask(String desc, boolean check, int duration) throws DukeException {
super(desc, check);
checkHour(durationHour);
this.durationHour = durationHour;
checkMinute(durationMinute);
this.durationMinute = durationMinute;
checkDuration(duration);
this.duration = duration;
super.tt = "F";
super.extra = String.valueOf(duration);
}

/**
* Checks for valid hour representation.
* @param hour Integer hour to validate.
* @throws DukeException Hour is not 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 checkHour(int hour) throws DukeException {
if (hour < 0) {
throw new DukeException("FixedTask argument hour must be a positive neutral number.");
private void checkDuration(int minute) throws DukeException {
if (minute < 1) {
throw new DukeException("FixedTask argument duration 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.
* Converts minutes into hours without decimal.
* @param duration Minutes to be converted.
* @return Minutes in hours.
*/
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.");
}
private int getHour(int duration) {
return duration / 60;
}

/**
* Converts minutes into hours and minutes. Return only remaining minutes.
* @param duration Minutes to be converted.
* @return Remaining minutes after conversion.
*/
private int getMinute(int duration) {
return duration % 60;
}

/**
* Formats object variables to text form.
* @return Printable text representation of object.
*
* @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 + ")";
int hour = getHour(duration);
int minute = getMinute(duration);
String printHour = hour > 0 ? hour + " Hour" + (hour > 1 ? "s" : "") : "";
String printMinute = minute > 0 ? minute + " Minute" + (minute > 1 ? "s" : "") : "";
return "[F]" + super.toString() + " (Requires: " + printHour + (hour > 0 && minute > 0 ? " " : "") + printMinute + ")";
}
}
2 changes: 2 additions & 0 deletions src/main/java/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,8 @@ public TaskList(String file) {
this.add(new Deadline(attributes[2], DatetimeFormatter.check(attributes[3]), attributes[1].equals("true")));
} else if (attributes[0].equals("E")) {
this.add(new Event(attributes[2], DatetimeFormatter.check(attributes[3]), attributes[1].equals("true")));
} else if (attributes[0].equals("F")) {
this.add(new FixedTask(attributes[2], attributes[1].equals("true"), Integer.parseInt(attributes[3])));
} else {
System.out.println("☹ OOPS!!! Line " + Integer.toString(line) + " in duke.txt is corrupted" + ", skipping...");
}
Expand Down
48 changes: 48 additions & 0 deletions src/test/java/FixedTaskTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import org.junit.jupiter.api.Test;
import process.DukeException;
import task.FixedTask;

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;

public class FixedTaskTest {
@Test
public void validArgTest(){
try {
FixedTask ft = new FixedTask("Build tree house.", true, 330);
} catch (DukeException e) {
assert false;
}
assert true;
}

@Test
public void invalidArgTest(){
FixedTask ft;
boolean hasError = false;
try {
ft = new FixedTask("Build tree house.", true, 0);
} catch (DukeException e) {
hasError = true;
}
assertTrue(hasError);
hasError = false;
try {
ft = new FixedTask("Build tree house.", true, -60);
} catch (DukeException e) {
hasError = true;
}
assertTrue(hasError);
}

@Test
public void toStringTest(){
try {
FixedTask ft = new FixedTask("Build tree house.", false, 301);
assertEquals("[F][X] Build tree house. (Requires: 5 Hours 1 Minute)", ft.toString());
} catch (DukeException e) {
assert false;
}
assert true;
}
}

0 comments on commit cdb80e5

Please sign in to comment.