From b11b6c70c9c7094dea66667604b4279e2beda32d Mon Sep 17 00:00:00 2001 From: CEGLincoln Date: Sun, 13 Oct 2019 15:58:05 +0800 Subject: [PATCH] added class convert --- data/tasks.txt | 5 +- src/main/java/duke/Duke.java | 5 +- src/main/java/duke/command/RemindCommand.java | 44 +++++----- src/main/java/duke/command/Snooze.java | 10 ++- src/main/java/duke/command/ViewCommand.java | 3 +- src/main/java/duke/parser/Convert.java | 78 ++++++++++++++++++ src/main/java/duke/parser/Parser.java | 82 ++----------------- src/main/java/duke/storage/Storage.java | 9 +- src/main/java/duke/task/Deadline.java | 5 +- src/main/java/duke/task/DoAfter.java | 12 ++- .../java/duke/task/DoWithinPeriodTasks.java | 4 + src/main/java/duke/task/Event.java | 5 +- src/main/java/duke/task/Task.java | 4 - src/main/java/duke/task/TaskList.java | 13 +-- src/main/java/duke/task/Todo.java | 5 +- src/main/java/duke/task/ViewSchedules.java | 13 +-- src/main/java/duke/ui/Ui.java | 18 ++-- 17 files changed, 160 insertions(+), 155 deletions(-) create mode 100644 src/main/java/duke/parser/Convert.java diff --git a/data/tasks.txt b/data/tasks.txt index 62b6b469..522f25df 100644 --- a/data/tasks.txt +++ b/data/tasks.txt @@ -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 diff --git a/src/main/java/duke/Duke.java b/src/main/java/duke/Duke.java index ead6a06c..20593e09 100644 --- a/src/main/java/duke/Duke.java +++ b/src/main/java/duke/Duke.java @@ -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) { @@ -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(); - } } diff --git a/src/main/java/duke/command/RemindCommand.java b/src/main/java/duke/command/RemindCommand.java index f31a36f2..37e611c4 100644 --- a/src/main/java/duke/command/RemindCommand.java +++ b/src/main/java/duke/command/RemindCommand.java @@ -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()); } } diff --git a/src/main/java/duke/command/Snooze.java b/src/main/java/duke/command/Snooze.java index b0fe3333..9fc9e332 100644 --- a/src/main/java/duke/command/Snooze.java +++ b/src/main/java/duke/command/Snooze.java @@ -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; @@ -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 @@ -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()); diff --git a/src/main/java/duke/command/ViewCommand.java b/src/main/java/duke/command/ViewCommand.java index cbf94fc4..e2315f2f 100644 --- a/src/main/java/duke/command/ViewCommand.java +++ b/src/main/java/duke/command/ViewCommand.java @@ -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) { diff --git a/src/main/java/duke/parser/Convert.java b/src/main/java/duke/parser/Convert.java new file mode 100644 index 00000000..d99b3bd9 --- /dev/null +++ b/src/main/java/duke/parser/Convert.java @@ -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); + } +} diff --git a/src/main/java/duke/parser/Parser.java b/src/main/java/duke/parser/Parser.java index 35767c85..c939b1ae 100644 --- a/src/main/java/duke/parser/Parser.java +++ b/src/main/java/duke/parser/Parser.java @@ -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; /** @@ -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. @@ -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); @@ -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) { @@ -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); @@ -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; @@ -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); - } } diff --git a/src/main/java/duke/storage/Storage.java b/src/main/java/duke/storage/Storage.java index b3c4bf61..f749f701 100644 --- a/src/main/java/duke/storage/Storage.java +++ b/src/main/java/duke/storage/Storage.java @@ -2,13 +2,11 @@ import duke.exception.DukeException; import duke.task.Deadline; -import duke.task.DoAfter; import duke.task.DoWithinPeriodTasks; import duke.task.Event; import duke.task.Task; import duke.task.TaskList; import duke.task.Todo; -import duke.task.ViewSchedules; import java.io.File; import java.io.IOException; @@ -31,7 +29,6 @@ public class Storage { /** * The constructor method for Storage. - * * @param fp used to specify the location of the file in the hard disc. */ public Storage(String fp) { @@ -41,7 +38,8 @@ public Storage(String fp) { } /** - * Returns an {@link ArrayList} of {@link Task}s read from the text file indicated by the {@link Path}. + * Load tasks from file. + * @return an {@link ArrayList} of {@link Task}s read from the text file indicated by the {@link Path}. */ public List load() throws DukeException { try { @@ -102,7 +100,6 @@ private List generateTasks() throws DukeException { /** * Returns the {@link Path} that holds the directory used for I/O. - * * @return Path specifies the directory of the text {@link File} used for writing or reading */ public Path getPath() { @@ -115,7 +112,6 @@ public String toString() { /** * Updates the content in the text file, by changing the specific {@link Task} indicated by the taskNb. - * * @param taskNb Positive integer indicating the number of the {@link Task} in the {@link TaskList} to be updated * @throws DukeException if the taskNb is invalid or there was an I/O Exception */ @@ -134,7 +130,6 @@ public void changeContent(int taskNb) throws DukeException { /** * Used to add a {@link Task} by writing to {@link File}. - * * @param task {@link Task} to be written * @throws IOException whatever that is */ diff --git a/src/main/java/duke/task/Deadline.java b/src/main/java/duke/task/Deadline.java index 7d26d4ad..7ed72c66 100644 --- a/src/main/java/duke/task/Deadline.java +++ b/src/main/java/duke/task/Deadline.java @@ -1,6 +1,7 @@ package duke.task; -import duke.parser.Parser; +import duke.parser.Convert; + import java.util.Date; /** @@ -22,7 +23,7 @@ public Deadline(String description, String str) { @Override public void setNewDate(String date) { this.by = date; - this.date = Parser.stringToDate(by); + this.date = Convert.stringToDate(by); } @Override diff --git a/src/main/java/duke/task/DoAfter.java b/src/main/java/duke/task/DoAfter.java index 2bf77a64..b837d13b 100644 --- a/src/main/java/duke/task/DoAfter.java +++ b/src/main/java/duke/task/DoAfter.java @@ -1,6 +1,7 @@ package duke.task; -import duke.parser.Parser; +import duke.parser.Convert; + import java.text.SimpleDateFormat; import java.time.LocalDate; import java.time.ZoneId; @@ -8,6 +9,8 @@ /** * Represents a special task that {@link Task } specified by the due {@link Date}. + * One of the B-Extensions + * @author VirginiaYu */ public class DoAfter extends Task { @@ -34,7 +37,7 @@ public DoAfter(String description, Task todo) { public DoAfter(String description, String after) { super(description); this.after = after; - this.date = Parser.stringToDate(after); + this.date = Convert.stringToDate(after); } @Override @@ -69,9 +72,9 @@ private String getDateString(Date date) { LocalDate localDate = date.toInstant().atZone(ZoneId.systemDefault()).toLocalDate(); String pattern; if (after.length() > 11) { - pattern = "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha "; + pattern = "d'" + Convert.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy, ha "; } else { - pattern = "d'" + Parser.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; + pattern = "d'" + Convert.getDaySuffix(localDate.getDayOfMonth()) + "' 'of' MMMM yyyy"; } SimpleDateFormat formatter = new SimpleDateFormat(pattern); return formatter.format(date); @@ -79,6 +82,7 @@ private String getDateString(Date date) { /** * Returns the String of the {@link Deadline} in format compatible in a text file. + * * @return String used to print the {@link Task } in the text file */ public String printInFile() { diff --git a/src/main/java/duke/task/DoWithinPeriodTasks.java b/src/main/java/duke/task/DoWithinPeriodTasks.java index 15664dfd..011a2351 100644 --- a/src/main/java/duke/task/DoWithinPeriodTasks.java +++ b/src/main/java/duke/task/DoWithinPeriodTasks.java @@ -2,6 +2,10 @@ import java.util.Date; +/** + * One of the B-Extensions. + * @author CEGLincoln + */ public class DoWithinPeriodTasks extends Task { private String from; diff --git a/src/main/java/duke/task/Event.java b/src/main/java/duke/task/Event.java index 1388801a..d67e8a2a 100644 --- a/src/main/java/duke/task/Event.java +++ b/src/main/java/duke/task/Event.java @@ -1,6 +1,7 @@ package duke.task; -import duke.parser.Parser; +import duke.parser.Convert; + import java.util.Date; /** @@ -22,7 +23,7 @@ public Event(String description, String str) { @Override public void setNewDate(String date) { this.at = date; - this.date = Parser.stringToDate(at); + this.date = Convert.stringToDate(at); } @Override diff --git a/src/main/java/duke/task/Task.java b/src/main/java/duke/task/Task.java index 0ead3053..16c403a8 100644 --- a/src/main/java/duke/task/Task.java +++ b/src/main/java/duke/task/Task.java @@ -13,7 +13,6 @@ public abstract class Task { /** * The constructor method for Task. - * * @param description of the task */ public Task(String description) { @@ -29,7 +28,6 @@ public Task(String description) { /** * Returns a String representation of the status icon, indicating whether the {@link Task} was done. - * * @return a tick or a cross */ public String getStatusIcon() { @@ -38,7 +36,6 @@ public String getStatusIcon() { /** * Returns the String description of the {@link Task}. - * * @return String description of the Task */ public String getDescription() { @@ -58,7 +55,6 @@ public String toString() { /** * Returns a boolean indicating whether the {@link Task} was completed. - * * @return boolean true if the task was marked as done, false otherwise */ public boolean isDone() { diff --git a/src/main/java/duke/task/TaskList.java b/src/main/java/duke/task/TaskList.java index 2e87344f..65419f19 100644 --- a/src/main/java/duke/task/TaskList.java +++ b/src/main/java/duke/task/TaskList.java @@ -1,10 +1,7 @@ package duke.task; import duke.Duke; - import java.util.ArrayList; -import java.util.Collections; -import java.util.Date; import java.util.List; /** @@ -30,7 +27,6 @@ public TaskList() { /** * Adds a task to the {@link TaskList}. - * * @param task {@link Task} to be added to the list */ public void addTask(Task task) { @@ -39,7 +35,6 @@ public void addTask(Task task) { /** * Returns the number of {@link Task}s in the {@link TaskList} so far. - * * @return an integer indicating the size of the list of {@link Task}s stored */ public int size() { @@ -48,7 +43,6 @@ public int size() { /** * Marks a task as completed if the user finished it. - * * @param taskNb the number of the {@link Task} in the {@link TaskList} that was completed */ public void markTaskDone(int taskNb) { @@ -57,7 +51,6 @@ public void markTaskDone(int taskNb) { /** * Returns the {@link Task} at the position indicated by the taskNb. - * * @param taskNb the position of the {@link Task} requested in the {@link TaskList} * @return the requested {@link Task} */ @@ -66,9 +59,8 @@ public Task getTask(int taskNb) { } /** - * Returns an immutable list of all the {@link Task}s in the {@link TaskList} so far. - * - * @return unmodifiable {@link ArrayList} of {@link Task} listed so far + * Returns a list of all the {@link Task}s in the {@link TaskList}. + * @return {@link ArrayList} of {@link Task} */ public List getAllTasks() { return taskList; @@ -76,7 +68,6 @@ public List getAllTasks() { /** * Returns the removed {@link Task} from position taskNb in the {@link TaskList}. - * * @param taskNb the position of the {@link Task} to be removed from the {@link TaskList} * @return Task the task that was removed */ diff --git a/src/main/java/duke/task/Todo.java b/src/main/java/duke/task/Todo.java index 7a18b471..d7e3f4a1 100644 --- a/src/main/java/duke/task/Todo.java +++ b/src/main/java/duke/task/Todo.java @@ -1,6 +1,7 @@ package duke.task; -import duke.parser.Parser; +import duke.parser.Convert; + import java.util.Date; /** @@ -20,7 +21,7 @@ public Todo(String description) { @Override public void setNewDate(String date) { - this.date = Parser.stringToDate(date); + this.date = Convert.stringToDate(date); } @Override diff --git a/src/main/java/duke/task/ViewSchedules.java b/src/main/java/duke/task/ViewSchedules.java index 39e5a528..68d18d16 100644 --- a/src/main/java/duke/task/ViewSchedules.java +++ b/src/main/java/duke/task/ViewSchedules.java @@ -1,16 +1,17 @@ package duke.task; -import duke.parser.Parser; - +import duke.parser.Convert; import java.util.Date; -//import static duke.task.Task.getDate; - +/** + * One of the B-Extensions. + * @author x3chillax + */ public class ViewSchedules extends TaskList { private Date when; - public ViewSchedules(String when) { - this.when = Parser.stringToDate(when); + public ViewSchedules(String w) { + when = Convert.stringToDate(w); } public Date getCurrentDate() { diff --git a/src/main/java/duke/ui/Ui.java b/src/main/java/duke/ui/Ui.java index 92435f62..74471040 100644 --- a/src/main/java/duke/ui/Ui.java +++ b/src/main/java/duke/ui/Ui.java @@ -20,13 +20,15 @@ public Ui() { /** * Returns the input entered by the user. - * * @return String the input entered by the user */ public String readCommand() { return scanner.nextLine(); } + /** + * Show a line. + */ public void showLine() { System.out.println("\t " + line); } @@ -49,7 +51,7 @@ public void showLoadingError() { } /** - * javadoc. + * Show the error to user. * @param e an error */ public void showError(String e) { @@ -57,7 +59,7 @@ public void showError(String e) { } /** - * show the task. + * Show the task to user. * @param task string */ public void showTask(String task) { @@ -65,8 +67,8 @@ public void showTask(String task) { } /** - * what is this. - * @param doneTask yes + * Show that this task is marked. + * @param doneTask The task that is marked as done */ public void showMarkDone(String doneTask) { System.out.println("\t Nice! I've marked this task as done:"); @@ -74,7 +76,7 @@ public void showMarkDone(String doneTask) { } /** - * spam. + * Show the task that has been snoozed. * @param date the date * @param changedTask the task that has been changed */ @@ -98,7 +100,7 @@ public void showSize(int size) { } /** - * comment. + * Shows that a task has been added. * @param command ay * @param size ya */ @@ -109,7 +111,7 @@ public void showAddCommand(String command, int size) { } /** - * say the task that has been removed. + * Show the task that has been removed. * @param removed the task * @param size size of list */