Skip to content

Commit

Permalink
Merge pull request nusCS2113-AY1920S1#19 from CEGLincoln/master
Browse files Browse the repository at this point in the history
B-DoWithinPeriodTasks
  • Loading branch information
CEGLincoln authored Sep 18, 2019
2 parents 263656b + e017dc3 commit fe14568
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 10 deletions.
4 changes: 4 additions & 0 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
T|0|read book
D|0|return book |tmr
E|0|meeting |2019-09-09
[P][✘] pain (from: TODO to: TODO
22 changes: 19 additions & 3 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import duke.command.*;
import duke.exception.DukeException;
import duke.task.Deadline;
import duke.task.DoWithinPeriodTasks;
import duke.task.Event;
import duke.task.Todo;

Expand Down Expand Up @@ -37,18 +38,18 @@ public static Command parse(String fullCommand) throws DukeException {
return new DoneCommand(taskNb - 1);
} else throw new DukeException("Need a task number after done!");
case "todo":
if ((splitted.length == 1) || splitted[1].isBlank())
if ((splitted.length == 1) || splitted[1].isEmpty())
throw new DukeException("The description of a todo cannot be empty.");
return new AddCommand(new Todo(splitted[1]));
case "deadline":
if ((splitted.length == 1) || splitted[1].isBlank())
if ((splitted.length == 1) || splitted[1].isEmpty())
throw new DukeException("The description of a deadline cannot be empty.");
String[] getBy = splitted[1].split("/by ", 2);
if (getBy.length < 2)
throw new DukeException("The description of a deadline must contain /by date!");
return new AddCommand(new Deadline(getBy[0], getBy[1]));
case "event":
if ((splitted.length == 1) || splitted[1].isBlank())
if ((splitted.length == 1) || splitted[1].isEmpty())
throw new DukeException("The description of an event cannot be empty, and it must contain /at");
String[] getAt = splitted[1].split("/at ", 2);
if (getAt.length < 2)
Expand All @@ -63,6 +64,21 @@ public static Command parse(String fullCommand) throws DukeException {
int taskNb = Integer.parseInt(splitted[1]);
return new DeleteCommand(taskNb - 1);
} else throw new DukeException("Need a task number after done!");
case "period":
if ((splitted.length == 1) || splitted[1].isEmpty())
throw new DukeException("The description of a period cannot be empty.");
String[] getPart = splitted[1].split("/from ", 2);
if (getPart.length < 2)
throw new DukeException("The description of a period must contain /from date!");
String[] part = getPart[1].split("/to ", 2);
if (part.length < 2)
throw new DukeException("NO");
try {
return new AddCommand(new DoWithinPeriodTasks(getPart[0], part[0], part[1]));
}
catch(Exception e){
throw new DukeException("NO");
}
default:
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
Expand Down
6 changes: 4 additions & 2 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ public class Deadline extends Task {
public Deadline(String description, String by) {
super(description);
this.by = by;
this.date = getDate(by);
this.date = super.getDate(by);
}

@Override
public String toString() {
return (getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + getDateString(date) + ")";
return (super.getDate(by) == null) ? "[D]" + super.toString() + "(by: " + by + ")" : "[D]" + super.toString() + "(by: " + getDateString(date) + ")";
}

/**
* Returns the data by which the deadline must be met
* @return date the final {@link Date } for finishing the Deadline
*/
/*
public Date getDate() {
return date;
}
*/

/**
* Returns the {@link Date } instance as a String to be printed in the file
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/duke/task/DoWithinPeriodTasks.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package duke.task;

import java.util.Date;

public class DoWithinPeriodTasks extends Task{

private Date from;
private Date to;

public DoWithinPeriodTasks(String d, String f, String t) {
//CONSTRUCTOR
super(d);
from = super.getDate(f);
to = super.getDate(t);
}

public String printInFile(){
return this.toString();
}

@Override
public String toString(){
return "[P]" + super.toString() + "(from: " + "TODO" + " to: " + "TODO";
}
}
11 changes: 9 additions & 2 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ public class Event extends Task {
public Event(String description, String at) {
super(description);
this.at = at;
this.date = getDate(at);
this.date = super.getDate(at);
}

@Override
Expand All @@ -39,7 +39,14 @@ private String getDateString(Date date) {
if (date == null)
return at;
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
String pattern = at.length() > 11 ? "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
//String pattern = at.length() > 11 ? "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
String pattern;
if(at.length() > 11){
pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha ";
}
else{
pattern = "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
}
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
Expand Down
7 changes: 4 additions & 3 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -83,14 +83,15 @@ static String getDaySuffix(int n) {
* @param date String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy", used to extract a {@link Date} instance from
* @return the {@link Date} instance created from the argument string
*/
static Date getDate(String date) {
protected Date getDate(String date) {
DateFormat dateFormat = (date.length() > 11) ? new SimpleDateFormat("dd/MM/yyyy hhmm") : new SimpleDateFormat("dd/MM/yyyy");
try {
dateFormat.parse(date);
//dateFormat.parse(date);
return dateFormat.parse(date);
} catch (ParseException e) {
//case the date was not valid!
return new Date();
}
return null;
//return null;
}
}

0 comments on commit fe14568

Please sign in to comment.