Skip to content

Commit

Permalink
Merge pull request #58 from CEGLincoln/master
Browse files Browse the repository at this point in the history
added class convert
  • Loading branch information
CEGLincoln authored Oct 13, 2019
2 parents 905b30e + b11b6c7 commit 879cbf7
Show file tree
Hide file tree
Showing 17 changed files with 160 additions and 155 deletions.
5 changes: 2 additions & 3 deletions data/tasks.txt
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
T|1|read book
D|0|return book|Monday
E|0|meeting|U-Town
P|0|lecture|1600|1800
D|1|return book|09/09/09 2359
E|0|meeting|here
5 changes: 2 additions & 3 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public Duke(String filePath) {
/**
* The execution core of the Duke class.
*/
public void run() throws DukeException {
public void run() {
ui.showWelcome();
boolean isExit = false;
while (!isExit) {
Expand All @@ -55,8 +55,7 @@ public void run() throws DukeException {
/**
* =============== MAIN FUNCTION ===============.
*/
public static void main(String[] args) throws DukeException {
public static void main(String[] args) {
new Duke("data/tasks.txt").run();

}
}
44 changes: 22 additions & 22 deletions src/main/java/duke/command/RemindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,37 +9,37 @@
import java.time.ZoneId;
import java.util.Date;

/**
* One of the B-Extensions.
* @author 9hafidz6
*/
public class RemindCommand extends Command {

public RemindCommand() {

//An empty constructor method
}

@Override
public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeException {
try {
int num = 1;
LocalDate date = LocalDate.now();
LocalDate date1 = date.plusDays(5);

Date startDate = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
Date endDate = Date.from(date1.atStartOfDay(ZoneId.systemDefault()).toInstant());

if (taskList.size() == 0) {
System.out.println("You have no upcoming events/deadlines/todos");
} else {
System.out.println("These are your Tasks in the next 5 days");
for (int a = 0; a < taskList.size(); a++) {
Date taskDate = taskList.getTask(a).getCurrentDate();

if ((isWithinRange(taskDate, endDate, startDate))) {
System.out.println(num + ": " + taskList.getTask(a).toString());
num++;
}
int num = 1;
LocalDate date = LocalDate.now();
LocalDate date1 = date.plusDays(5);

Date startDate = Date.from(date.atStartOfDay(ZoneId.systemDefault()).toInstant());
Date endDate = Date.from(date1.atStartOfDay(ZoneId.systemDefault()).toInstant());

if (taskList.size() == 0) {
System.out.println("You have no upcoming events/deadlines/todos");
} else {
System.out.println("These are your Tasks in the next 5 days");
for (int a = 0; a < taskList.size(); a++) {
Date taskDate = taskList.getTask(a).getCurrentDate();

if ((isWithinRange(taskDate, endDate, startDate))) {
System.out.println(num + ": " + taskList.getTask(a).toString());
num++;
}
}
} catch (Exception e) {
System.out.println(e.getMessage());
}
}

Expand Down
10 changes: 7 additions & 3 deletions src/main/java/duke/command/Snooze.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package duke.command;

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

import java.util.Date;

/**
* One of the B-Extensions.
* @author saradj
*/
public class Snooze extends Command {

private int taskNb;
Expand All @@ -23,7 +27,7 @@ public class Snooze extends Command {
public Snooze(int taskNb, String until) {
this.taskNb = taskNb;
this.until = until;
this.date = Parser.stringToDate(until);
this.date = Convert.stringToDate(until);
}

@Override
Expand All @@ -33,7 +37,7 @@ public void execute(TaskList taskList, Ui ui, Storage storage) throws DukeExcept
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());
ui.showChangedDate(Convert.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());
Expand Down
3 changes: 2 additions & 1 deletion src/main/java/duke/command/ViewCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,11 @@

/**
* Represents a specific {@link Command} used to find a String occurring in the {@link TaskList}.
* One of the B-Extensions.
* @author x3chillax
*/
public class ViewCommand extends Command {

//private final Date Date = new Date();
private Date toView;

public ViewCommand(Date toView) {
Expand Down
78 changes: 78 additions & 0 deletions src/main/java/duke/parser/Convert.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
package duke.parser;

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

/**
* Convert between String and Date.
*/
public class Convert {

/**
* Returns the suffix to be used after the days in the Date, useful 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} 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 or null
*/
public static Date stringToDate(String date) {
DateFormat formatter;
if (date.length() > 11) {
formatter = new SimpleDateFormat("dd/MM/yyyy hhmm");
} else {
formatter = new SimpleDateFormat("dd/MM/yyyy");
}
try {
return formatter.parse(date);
} catch (ParseException e) {
System.out.println("Warning: Unable to convert \"" + date + "\" to a Date.");
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;
if (dateString.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);
}
}
82 changes: 5 additions & 77 deletions src/main/java/duke/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,6 @@
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;

/**
Expand All @@ -30,12 +25,7 @@
*/
public class Parser {

/**
* The constructor method for Parser.
*/
public Parser(int x){
//do nothing, everything here is static
}
//There is no constructor method for all others are static.

/**
* Returns a {@link Command} that can be understood by {@link Duke} and executed after.
Expand Down Expand Up @@ -84,7 +74,7 @@ public static Command parse(String fullCommand, int size) throws DukeException {
return new Snooze(checkNumber(getUntil[0], size), getUntil[1]);
case "view":
checkLength(splitted);
Date splittedDate = stringToDate(splitted[1]);
Date splittedDate = Convert.stringToDate(splitted[1]);
return new ViewCommand(splittedDate);
case "period":
checkLength(splitted);
Expand All @@ -98,6 +88,7 @@ public static Command parse(String fullCommand, int size) throws DukeException {

/**
* Checks the length of a String array is of size 2.
* @throws DukeException when array is not of size 2.
*/
public static void checkLength(String[] str) throws DukeException {
if (str.length != 2) {
Expand All @@ -106,7 +97,7 @@ public static void checkLength(String[] str) throws DukeException {
}

/**
* JAVADOC COMMENT.
* Split a string and check its length.
*/
public static String[] splitAndCheck(String str, String regex) throws DukeException {
String[] part = str.split(regex, 2);
Expand All @@ -116,8 +107,8 @@ public static String[] splitAndCheck(String str, String regex) throws DukeExcept

/**
* Converts a string into a number, and checks if it is out of bounds.
*
* @return Returns a valid integer
* @throws DukeException when it is invalid
*/
public static int checkNumber(String str, int size) throws DukeException {
int x;
Expand All @@ -134,67 +125,4 @@ public static int checkNumber(String str, int size) throws DukeException {
}
return x;
}

/**
* Returns the suffix to be used after the days in the Date, useful 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} 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 stringToDate(String date) {
DateFormat formatter;
if (date.length() > 11) {
formatter = new SimpleDateFormat("dd/MM/yyyy hhmm");
} else {
formatter = new SimpleDateFormat("dd/MM/yyyy");
}
try {
return formatter.parse(date);
} catch (ParseException e) {
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;
if (dateString.length() > 11) {
pattern = "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha ";
} else {
pattern = "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy";
}
SimpleDateFormat formatter = new SimpleDateFormat(pattern);
return formatter.format(date);
}
}
Loading

0 comments on commit 879cbf7

Please sign in to comment.