-
Notifications
You must be signed in to change notification settings - Fork 313
[Lawnce Goh] Duke Increments #347
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from 29 commits
65f72a8
0112efe
8658615
24141dc
31173af
a2c727d
7957c13
d5a61de
183dfe7
37ec8a4
87c298d
91c040e
b0146f9
5dda65f
6c0eaf7
8a58113
d3cccf1
4bb4648
e964e9f
1dba722
b28d390
391cd45
363752c
b9d91a0
c564bb6
26975d7
ec67757
bd7dd1d
4c842cf
c10bc43
32b18fd
9a766b2
68fe08c
26d0001
4aa438f
5ed5e32
c1eb7f2
e8c8738
855e907
c329c33
0ab3dd3
e2515f4
40fb1ea
d3cd4a9
badbd4e
8d5db91
657f9d9
5c1da8d
67eb7c9
21447af
0600f0c
dbe8517
814e460
c613c2e
bab5a4f
10720fd
aadd31b
086722f
d638a11
374317d
cd623e1
2d3c306
86bb0f1
076e3c7
1ffbd0d
7e58400
5551226
dae78f4
dd50750
04a862b
ee20d73
43dd94b
a42a65d
54a6128
f57d77e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| T|1| read book | ||
| D|0| return book | June 6th | ||
| D|1| return book | 2nd of December 0019, 6:00pm | ||
| E|1| return book | 2nd of December 0019, 7:00pm |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,66 @@ | ||
| import duke.command.Command; | ||
| import duke.exception.DukeException; | ||
| import duke.parser.Parser; | ||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| import java.text.ParseException; | ||
|
|
||
| public class Duke { | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
|
|
||
| private Storage storage; | ||
| private TaskList tasks; | ||
| private Ui ui; | ||
|
|
||
| public Duke(String filePath) { | ||
| ui = new Ui(); | ||
| storage = new Storage(filePath); | ||
| try { | ||
| tasks = new TaskList(storage.load()); | ||
| } catch (DukeException e) { | ||
| ui.showLoadingError(); | ||
| tasks = new TaskList(); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs the entire logic of the program. showWelcome() displays the duke logo and greets | ||
| * the user. isExit boolean ensures that the while loop doesn't end until the user input of | ||
| * "bye" is encountered. User's input will be read through the ui and then parsed to give | ||
| * a Command object. The specific command will then be executed. | ||
| * <p> | ||
| * This method has try and catch blocks within to help with catching both DukeException | ||
| * and ParseException | ||
| * | ||
| */ | ||
| public void run() { | ||
| ui.showWelcome(); | ||
| boolean isExit = false; | ||
| while (!isExit) { | ||
| try { | ||
| String fullCommand = ui.readCommand(); | ||
| ui.showLine(); // show the divider line ("_______") | ||
| Command c = Parser.parse(fullCommand); | ||
| c.execute(tasks, ui, storage); | ||
| isExit = c.isExit(); | ||
| } catch (DukeException e) { | ||
| ui.showError(e.getMessage()); | ||
| } catch (ParseException e) { | ||
| System.out.println(e.getMessage()); | ||
| } finally { | ||
| ui.showLine(); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Runs the main method to load the duke.txt file into the program and then calls the run method | ||
| * @param args | ||
| * @throws DukeException | ||
| * @throws ParseException | ||
|
lawncegoh marked this conversation as resolved.
Outdated
|
||
| */ | ||
| public static void main(String[] args) throws DukeException, ParseException{ | ||
| new Duke("/Users/lawnce/Desktop/duke/data/duke.txt").run(); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: Duke | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.exception.DukeException; | ||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
| import java.text.ParseException; | ||
|
|
||
| public abstract class Command { | ||
|
|
||
| public abstract void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, ParseException; | ||
|
|
||
| public abstract boolean isExit(); | ||
|
lawncegoh marked this conversation as resolved.
Outdated
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| package duke.command; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would it be possible to remove the '.class' files? Just curious becuase I didn't package the files. |
||
|
|
||
| import duke.exception.DukeException; | ||
| import duke.initials.Deadline; | ||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| public class DeadlineCommand extends Command { | ||
|
|
||
| public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy HHmm"); | ||
|
lawncegoh marked this conversation as resolved.
|
||
|
|
||
| /** | ||
| * Creates new deadline task using a TaskList, Ui and Storage, it will then be added into the taskArrayList that | ||
| * was loaded into the TaskList as param. | ||
| * @param tasks the TaskList to be used | ||
| * @param ui the Ui to be used | ||
| * @param storage the Storage to be used | ||
| * @throws DukeException | ||
| * @throws ParseException if the date is not able to be parsed | ||
| */ | ||
| public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, ParseException { | ||
|
|
||
| int end2 = ui.getRemainingWords().indexOf('/'); | ||
|
lawncegoh marked this conversation as resolved.
|
||
| if (end2 > 0) { | ||
|
|
||
| String description2 = ui.getRemainingWords().substring(1, ui.getRemainingWords().indexOf('/')); | ||
| String time2 = ui.getRemainingWords().substring(end2 + 4).trim(); | ||
| if (time2.isEmpty()) { | ||
| throw new DukeException("☹OOPS!!! Wrong format'"); | ||
| } else { | ||
| Deadline k = new Deadline(description2, makeDate(time2)); | ||
| tasks.add(k); | ||
| storage.writeData(); | ||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(k); | ||
| System.out.println("Now you have " + tasks.getTaskArrayList().size() + " tasks in the list."); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If there is only one task, it will print "Now you have 1 tasks in the list". It might be better to add an if-else block for singular task? |
||
| ui.showLine(); | ||
| } | ||
| } else { | ||
| throw new DukeException("☹OOPS!!! Wrong format"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns true or false regarding whether this method will end the while loop in the duke method run() | ||
| * @return false or true | ||
| */ | ||
| public boolean isExit() { | ||
| return false; | ||
| } | ||
|
|
||
| public String makeDate(String input) throws ParseException { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good catch for invalid date. I forgot :( |
||
| String ordinalIndicator; | ||
| Date date = DATE_FORMAT.parse(input); | ||
| String day = new SimpleDateFormat("dd").format(date); | ||
| String month = new SimpleDateFormat("MMMMMMMMMMM").format(date); | ||
| String year = new SimpleDateFormat("yyyy").format(date); | ||
| String time = new SimpleDateFormat("h:mma").format(date).toLowerCase(); | ||
|
|
||
| int int_day = Integer.parseInt(day); | ||
| if (int_day >= 11 && int_day <= 13) { | ||
|
lawncegoh marked this conversation as resolved.
|
||
| ordinalIndicator = "th"; | ||
| } else if (int_day % 10 == 1) { | ||
| ordinalIndicator = "st"; | ||
| } else if (int_day % 10 == 2) { | ||
| ordinalIndicator = "nd"; | ||
| } else if (int_day % 10 == 3) { | ||
| ordinalIndicator = "rd"; | ||
| } else { | ||
| ordinalIndicator = "th"; | ||
| } | ||
|
|
||
| String outputDate = int_day + ordinalIndicator + " of " + month + " " + year + ", " + time; | ||
| return outputDate; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.exception.DukeException; | ||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| public class DeleteCommand extends Command { | ||
|
|
||
| /** | ||
| * Creates new deadline task using a TaskList, Ui and Storage, it will then be added into the taskArrayList that | ||
| * was loaded into the TaskList as param. | ||
| * @param tasks the TaskList to be used | ||
| * @param ui the Ui to be used | ||
| * @param storage the Storage to be used | ||
| * @throws DukeException | ||
| */ | ||
| @Override | ||
| public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException { | ||
| if (ui.getRemainingWords().isEmpty()) { | ||
| throw new DukeException("☹OOPS!!! Wrong format"); | ||
| } | ||
| int position = Integer.parseInt(ui.getRemainingWords().trim()); | ||
| System.out.println("Noted. I've removed this task."); | ||
| System.out.println(tasks.getTaskArrayList().get(position-1)); | ||
| tasks.delete(position-1); | ||
| storage.writeData(); | ||
| System.out.println("Now you have " + tasks.getTaskArrayList().size() + " tasks in the list"); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true or false regarding whether this method will end the while loop in the duke method run() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Perhaps you can rephrase the descriptions for isExit() and its return value as they seem a bit repetitive now...? |
||
| * @return false or true | ||
| */ | ||
| @Override | ||
| public boolean isExit() { | ||
| return false; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,35 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| public class DoneCommand extends Command { | ||
|
|
||
| int index; | ||
|
|
||
| /** | ||
| * Creates new deadline task using a TaskList, Ui and Storage, it will then be added into the taskArrayList that | ||
| * was loaded into the TaskList as param. | ||
| * @param tasks the TaskList to be used | ||
| * @param ui the Ui to be used | ||
| * @param storage the Storage to be used | ||
| */ | ||
| public void execute(TaskList tasks, Ui ui, Storage storage) { | ||
| ui.showLine(); | ||
| index = Integer.parseInt(ui.getRemainingWords().trim()); | ||
| tasks.getTaskArrayList().get(index - 1).markAsDone(); | ||
| storage.writeData(); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| System.out.println(tasks.getTaskArrayList().get(index - 1)); | ||
| ui.showLine(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true or false regarding whether this method will end the while loop in the duke method run() | ||
| * @return false or true | ||
| */ | ||
| public boolean isExit() { | ||
| return false; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.exception.DukeException; | ||
| import duke.initials.Event; | ||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| import java.text.ParseException; | ||
| import java.text.SimpleDateFormat; | ||
| import java.util.Date; | ||
|
|
||
| public class EventCommand extends Command { | ||
|
|
||
| public static final SimpleDateFormat DATE_FORMAT = new SimpleDateFormat("dd/MM/yyyy HHmm"); | ||
|
|
||
| /** | ||
| * Creates new deadline task using a TaskList, Ui and Storage, it will then be added into the taskArrayList that | ||
| * was loaded into the TaskList as param. | ||
| * @param tasks the TaskList to be used | ||
| * @param ui the Ui to be used | ||
| * @param storage the Storage to be used | ||
| * @throws DukeException | ||
| * @throws ParseException if the date is not able to be parsed | ||
| */ | ||
| public void execute(TaskList tasks, Ui ui, Storage storage) throws DukeException, ParseException{ | ||
| ui.showLine(); | ||
| int end = ui.getRemainingWords().indexOf('/'); | ||
| if (end > 0) { | ||
| String description = ui.getRemainingWords().substring(1, ui.getRemainingWords().indexOf('/')); | ||
| String time = ui.getRemainingWords().substring(end + 4).trim(); | ||
| if (time.isEmpty()) { | ||
| throw new DukeException("☹OOPS!!! Wrong format'"); | ||
| } else { | ||
| Event m = new Event(description, makeDate(time)); | ||
| tasks.add(m); | ||
| storage.writeData(); | ||
|
|
||
| System.out.println("Got it. I've added this task:"); | ||
| System.out.println(m); | ||
| System.out.println("Now you have " + tasks.getTaskArrayList().size() + " tasks in the list."); | ||
| ui.showLine(); | ||
| } | ||
| } else { | ||
| throw new DukeException("☹OOPS!!! Wrong format'"); | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Returns true or false regarding whether this method will end the while loop in the duke method run() | ||
| * @return false or true | ||
| */ | ||
| public boolean isExit() { | ||
| return false; | ||
| } | ||
|
|
||
| public String makeDate(String input) throws ParseException { | ||
| String ordinalIndicator; | ||
| Date date = DATE_FORMAT.parse(input); | ||
| String day = new SimpleDateFormat("dd").format(date); | ||
| String month = new SimpleDateFormat("MMMMMMMMMMM").format(date); | ||
| String year = new SimpleDateFormat("yyyy").format(date); | ||
| String time = new SimpleDateFormat("h:mma").format(date).toLowerCase(); | ||
|
|
||
| int int_day = Integer.parseInt(day); | ||
|
|
||
| if (int_day >= 11 && int_day <= 13) { | ||
| ordinalIndicator = "th"; | ||
| } else if (int_day % 10 == 1) { | ||
| ordinalIndicator = "st"; | ||
| } else if (int_day % 10 == 2) { | ||
| ordinalIndicator = "nd"; | ||
| } else if (int_day % 10 == 3) { | ||
| ordinalIndicator = "rd"; | ||
| } else { | ||
| ordinalIndicator = "th"; | ||
| } | ||
|
|
||
| String outputDate = int_day + ordinalIndicator + " of " + month + " " + year + ", " + time; | ||
| return outputDate; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,31 @@ | ||
| package duke.command; | ||
|
|
||
| import duke.storage.Storage; | ||
| import duke.tasklist.TaskList; | ||
| import duke.ui.Ui; | ||
|
|
||
| public class ExitCommand extends Command { | ||
|
|
||
| /** | ||
| * Creates new deadline task using a TaskList, Ui and Storage, it will then be added into the taskArrayList that | ||
| * was loaded into the TaskList as param. | ||
| * @param tasks the TaskList to be used | ||
| * @param ui the Ui to be used | ||
| * @param storage the Storage to be used | ||
| */ | ||
| @Override | ||
| public void execute(TaskList tasks, Ui ui, Storage storage) { | ||
| ui.showLine(); | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| ui.showLine(); | ||
| } | ||
|
|
||
| /** | ||
| * Returns true or false regarding whether this method will end the while loop in the duke method run() | ||
| * @return false or true | ||
| */ | ||
| @Override | ||
| public boolean isExit() { | ||
| return true; | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.