Skip to content

Commit

Permalink
Merge pull request nusCS2113-AY1920S1#20 from saradj/B-Snooze
Browse files Browse the repository at this point in the history
[CS2113-T14-2]-saradj-B-Snooze
  • Loading branch information
saradj authored Sep 18, 2019
2 parents fe14568 + 7c20d64 commit 2d81ff3
Show file tree
Hide file tree
Showing 10 changed files with 174 additions and 78 deletions.
36 changes: 36 additions & 0 deletions src/main/java/duke/command/Snooze.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
package duke.command;

import duke.exception.DukeException;
import duke.parser.Parser;
import duke.storage.Storage;
import duke.task.Task;
import duke.task.TaskList;
import duke.ui.Ui;

import java.util.Date;

public class Snooze extends Command {

private int taskNb;
private String until;
private Date date;

public Snooze(String taskNb, String until){
this.taskNb = Integer.parseInt(taskNb.trim())-1;
this.until=until;
this.date= Parser.getDate(until);
}

@Override
public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
if (taskNb < taskList.size() && taskNb >= 0) {
if(taskList.getTask(taskNb).isDone())
throw new DukeException("Seems like you've already finished that task, no need to snooze it now");
taskList.changeTaskDate(taskNb, until);
ui.showChangedDate(Parser.getDateString(date, until),taskList.getTask(taskNb).toString() );
storage.changeContent(taskNb);
} else
throw new DukeException("Enter a valid task number after snooze, between 1 and " + taskList.size());
}
}

68 changes: 68 additions & 0 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,13 @@
import duke.task.Event;
import duke.task.Todo;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
import java.util.Date;

/**
* Represents a parser used to parse the input String from the user into a Duke understandable {@link Command}
*/
Expand Down Expand Up @@ -64,6 +71,16 @@ 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 "snooze":
if ((splitted.length == 1) || splitted[1].isBlank())
throw new DukeException("The description of a snooze cannot be empty.");
String[] getUntil = splitted[1].split("/until ", 2);
if (getUntil.length < 2)
throw new DukeException("The description of a snooze must contain /until date!");
return new Snooze(getUntil[0], getUntil[1]);


case "period":
if ((splitted.length == 1) || splitted[1].isEmpty())
throw new DukeException("The description of a period cannot be empty.");
Expand All @@ -79,8 +96,59 @@ public static Command parse(String fullCommand) throws DukeException {
catch(Exception e){
throw new DukeException("NO");
}

default:
throw new DukeException("I'm sorry, but I don't know what that means :-(");
}
}

/**
* Returns the suffix to be used after the days in the Date, usefull for printing the Date in the desired format
* @param n indication the Day of the month
* @return the suffix accordingly to the day of the month needed
*/
public static String getDaySuffix(int n) {
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}

/**
* Returns a {@link Date} instance representation of a String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy"
* @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
*/
public static Date getDate(String date) {
DateFormat dateFormat = (date.length() > 11) ? new SimpleDateFormat("dd/MM/yyyy hhmm") : new SimpleDateFormat("dd/MM/yyyy");
try {
dateFormat.parse(date);
return dateFormat.parse(date);
} catch (ParseException e) {
//case the date was not valid!
}
return null;
}
/**
* Returns the {@link Date } instance as a String to be printed in the file
* @param date deadline {@link Date} for finishing the task
* @return String the date for the deadline
*/
public static String getDateString(Date date, String dateString) {
if (date == null)
return dateString;
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
String pattern = dateString.length() > 11 ? "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
}
35 changes: 17 additions & 18 deletions src/main/java/duke/task/Deadline.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.parser.Parser;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
Expand All @@ -15,43 +17,40 @@ public class Deadline extends Task {
public Deadline(String description, String by) {
super(description);
this.by = by;
this.date = super.getDate(by);
this.date = Parser.getDate(by);
}

@Override
public void setNewDate(String date) {
by=date;
this.date=Parser.getDate(by);
}

@Override
public Date getCurrentDate() {
return date;
}


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

/**
* 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
* @param date deadline {@link Date} for finishing the task
* @return String the date for the deadline
*/
private String getDateString(Date date) {
if (date == null)
return by;
LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate();
String pattern = by.length() > 11 ? "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha " : "d'" + getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}

/**
* Returns the String representation of the {@link Deadline} in format compatible to be easily read and written in a text file on the hard disc
* @return String used to print the {@link Task } in the text file
*/
public String printInFile() {
return this.isDone() ? "D|1|" + this.getDescription() + "|" + this.getDateString(date) : "D|0|" + this.getDescription() + "|" + this.getDateString(date);
return this.isDone() ? "D|1|" + this.getDescription() + "|" + Parser.getDateString(date, by) : "D|0|" + this.getDescription() + "|" + Parser.getDateString(date, by);
}
}
39 changes: 16 additions & 23 deletions src/main/java/duke/task/Event.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
package duke.task;

import duke.parser.Parser;

import java.text.SimpleDateFormat;
import java.time.LocalDate;
import java.time.ZoneId;
Expand All @@ -15,39 +17,30 @@ public class Event extends Task {
public Event(String description, String at) {
super(description);
this.at = at;
this.date = super.getDate(at);
this.date = Parser.getDate(at);
}

@Override
public void setNewDate(String date) {
at=date;
this.date=Parser.getDate(at);
}

@Override
public Date getCurrentDate() {
return date;
}

@Override
public String toString() {
return "[E]" + super.toString() + "(at: " + getDateString(date) + ")";
return "[E]" + super.toString() + "(at: " + Parser.getDateString(date, at) + ")";
}
/**
* Returns the String representation of the {@link Event} in format compatible to be easily read and written in a text file on the hard disc
* @return String used to print the {@link Task } in the text file
*/
public String printInFile() {
return this.isDone() ? "E|1|" + getDescription() + "|" + this.getDateString(date) : "E|0|" + this.getDescription() + "|" + getDateString(date);
return this.isDone() ? "E|1|" + getDescription() + "|" + Parser.getDateString(date, at) : "E|0|" + this.getDescription() + "|" + Parser.getDateString(date, at);
}

/**
* Returns the {@link Date } instance as a String to be printed in the file
* @param date the {@link Date} at which the event is happening
* @return String the date for the event
*/
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;
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);
}
}
40 changes: 3 additions & 37 deletions src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@
public abstract class Task {
private String description;
private boolean isDone;

private Date date;
public Task(String description) {
this.description = description;
this.isDone = false;
}

public abstract void setNewDate(String date);
public abstract Date getCurrentDate();
/**
* Returns a String representation of the status icon, tick or cross, indicating whether the {@link Task} was done
* @return a String representation of a tick or a cross, representing a done, respectively not yet finished {@link Task}
Expand Down Expand Up @@ -57,41 +59,5 @@ public boolean isDone() {
return isDone;
}

/**
* Returns the suffix to be used after the days in the Date, usefull for printing the Date in the desired format
* @param n indication the Day of the month
* @return the suffix accordingly to the day of the month needed
*/
static String getDaySuffix(int n) {
if (n >= 11 && n <= 13) {
return "th";
}
switch (n % 10) {
case 1:
return "st";
case 2:
return "nd";
case 3:
return "rd";
default:
return "th";
}
}

/**
* Returns a {@link Date} instance representation of a String in the format "dd/MM/yyyy hhmm" or "dd/MM/yyyy"
* @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
*/
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);
return dateFormat.parse(date);
} catch (ParseException e) {
//case the date was not valid!
return new Date();
}
//return null;
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/task/TaskList.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

import java.util.ArrayList;
import java.util.Collections;
import java.util.Date;
import java.util.List;

/**
Expand Down Expand Up @@ -72,5 +73,8 @@ public Task removeTask(int taskNb) {
return taskList.remove(taskNb);
}

public void changeTaskDate(int taskNb, String date){
taskList.get(taskNb).setNewDate(date);
}

}
17 changes: 17 additions & 0 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
@@ -1,18 +1,35 @@
package duke.task;

import duke.parser.Parser;

import java.util.Date;

/**
* Represents a specific {@link Task} todo, not necessarily indicating a deadline or a specific date
*/
public class Todo extends Task {

private Date date;
public Todo(String description) {
super(description);
date=null;
}

@Override
public void setNewDate(String date) {
this.date= Parser.getDate(date);
}

@Override
public Date getCurrentDate() {
return date;
}

@Override
public String toString() {
return "[T]" + super.toString();
}

@Override
public String printInFile() {
return this.isDone() ? "T|1|" + this.getDescription() : "T|0|" + this.getDescription();
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/duke/ui/Ui.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,11 @@ public void showTask(String task) {
public void showMarkDone(String doneTask) {
System.out.println("\t Nice! I've marked this task as done:");
System.out.println("\t " + doneTask);
}

public void showChangedDate(String date, String changedTask){
System.out.println("\t Nice! I've snoozed this task as until "+ date+":");
System.out.println("\t " + changedTask);
}

public void showAddCommand(String command, int size) {
Expand Down
9 changes: 9 additions & 0 deletions src/module-info.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
/**
*
*/
/**
* @author Sara
*
*/
module duke {
}
Empty file added src/test/data/tasks.txt
Empty file.

0 comments on commit 2d81ff3

Please sign in to comment.