-
Notifications
You must be signed in to change notification settings - Fork 452
[Moo Jun Wei] iP #504
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?
[Moo Jun Wei] iP #504
Changes from 12 commits
ad35520
1dc859c
59433e5
081e632
8eb1837
ba8b47a
bd7449c
ebafc17
9ba4bb1
7a0cdad
f95b100
7615def
272b7b3
596be76
a35099a
e2a4b22
a06d4d2
b934cb3
e074e85
c13b21c
2d069b3
d081eed
5f24147
05ec52e
3cfe83c
5ba0b20
06a22e5
688825b
5700e9a
a502a2e
3b13d75
1b7f7b1
3ccc405
56d69f1
646856f
f5995ab
d02ea3a
bf98f6b
f54a9f6
feba356
ca041f0
54e4652
3d11a0c
3332db6
40568ee
7da2071
7fc7be5
76c5628
fb1dd66
13de582
bdae392
c4ccc86
87a48ee
d52ccea
d25fd66
dfb6231
cf5d827
68b4c2f
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 |
|---|---|---|
| @@ -1,10 +1,218 @@ | ||
| import exception.EmptyTaskDescException; | ||
| import exception.EmptyTaskTimeException; | ||
| import exception.NoSuchTaskException; | ||
| import tasks.*; | ||
|
|
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
|
|
||
| public enum TaskTypes { | ||
| TODO, | ||
| DEADLINE, | ||
| EVENT | ||
| } | ||
|
|
||
| private List<Task> taskList; | ||
| private SaveData data; | ||
| private static final File savefile = new File("savedata.txt"); | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
|
|
||
| Duke duke = new Duke(); | ||
| duke.start(); | ||
| } | ||
|
|
||
| public Duke() { | ||
|
|
||
| } | ||
|
|
||
| public void start() { | ||
| Scanner sc = new Scanner(System.in); | ||
| System.out.println( | ||
| "Hello! I'm Duke\n" + | ||
| "What can I do for you?" | ||
| ); | ||
|
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. Could perhaps break up the line before the operator, in accordance with the coding standards? |
||
| data = new SaveData(); | ||
| boolean isSaveFileCreated = savefile.exists(); | ||
| if (!isSaveFileCreated) { | ||
| try { | ||
| isSaveFileCreated = savefile.createNewFile(); | ||
| } catch (IOException e) { | ||
| System.out.println("Error while creating save file: " + e); | ||
| } | ||
| } | ||
|
|
||
| if (!isSaveFileCreated) { | ||
| System.out.println("Unable to find or create save file, exiting..."); | ||
| System.exit(1); | ||
| } | ||
|
|
||
| data.loadFromFile(new File("savedata.txt")); | ||
| taskList = data.getTaskList(); | ||
| loop(sc); | ||
| } | ||
|
|
||
| public void loop(Scanner sc) { | ||
|
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. This method looks long and complex, could think of abstracting out key components into different methods? |
||
| while (sc.hasNext()) { | ||
| String input = sc.nextLine().trim(); | ||
| int spaceIndex = input.indexOf(' '); | ||
| String command, body; | ||
| if (spaceIndex == -1) { | ||
| command = input; | ||
| body = ""; | ||
| } else { | ||
| command = input.substring(0, spaceIndex); | ||
| body = input.substring(spaceIndex + 1); | ||
| } | ||
|
|
||
| boolean isModified = false; | ||
| if ("bye".equals(command)) { | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| return; | ||
| } else if ("list".equals(command)) { | ||
| printTaskList(); | ||
| } else if ("mark".equals(command)) { | ||
| markTask(body); | ||
| isModified = true; | ||
| } else if ("unmark".equals(command)) { | ||
| unmarkTask(body); | ||
| isModified = true; | ||
| } else if ("delete".equals(command)) { | ||
| deleteTask(body); | ||
| isModified = true; | ||
| } else if (isTaskType(command)) { | ||
| addTask(command, body); | ||
| isModified = true; | ||
| } else { | ||
| System.out.println("OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| } | ||
|
|
||
| if (isModified) { | ||
| data.setTaskList(taskList); | ||
| data.saveToFile(savefile); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private void markTask(String body) { | ||
| int index = Integer.parseInt(body) - 1; | ||
| Task taskToMark = taskList.get(index); | ||
| taskToMark.mark(); | ||
| String response = String.format( | ||
| "Nice! I've marked this task as done:\n %s", | ||
| taskToMark); | ||
| System.out.println(response); | ||
| } | ||
|
|
||
| private void unmarkTask(String body) { | ||
| int index = Integer.parseInt(body) - 1; | ||
| Task taskToUnmark = taskList.get(index); | ||
| taskToUnmark.unmark(); | ||
| String response = String.format( | ||
| "OK, I've marked this task as not done yet:\n %s", | ||
| taskToUnmark | ||
| ); | ||
| System.out.println(response); | ||
| } | ||
|
|
||
| private void deleteTask(String body) { | ||
| int index = Integer.parseInt(body) - 1; | ||
| Task deletedTask = taskList.remove(index); | ||
| String response = String.format( | ||
| "Noted. I've removed this task:\n %s\nNow you have %d tasks in the list", | ||
| deletedTask, taskList.size() | ||
| ); | ||
| System.out.println(response); | ||
| } | ||
|
|
||
| private void addTask(String command, String body) { | ||
| try { | ||
| TaskTypes type = getTaskType(command); | ||
| Task newTask = createNewTask(body, type); | ||
| String response = String.format( | ||
| "Got it. I've added this task:\n %s\nNow you have %d tasks in the list.", | ||
| newTask, taskList.size()); | ||
| System.out.println(response); | ||
| } catch (NoSuchTaskException | EmptyTaskDescException | EmptyTaskTimeException e) { | ||
| System.out.println("Error: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private void printTaskList() { | ||
| System.out.println("Here are the tasks in your list:"); | ||
| StringBuilder sb = new StringBuilder(); | ||
| int i = 1; | ||
| for (Task t : taskList) { | ||
| sb.append(i); | ||
| sb.append(". "); | ||
| sb.append(t); | ||
| sb.append("\n"); | ||
| ++i; | ||
| } | ||
| System.out.print(sb); | ||
| } | ||
|
|
||
| private boolean isTaskType(String s) { | ||
| return "todo".equals(s) || | ||
| "event".equals(s) || | ||
| "deadline".equals(s); | ||
| } | ||
|
|
||
| private Task createNewTask(String taskString, TaskTypes type) | ||
| throws EmptyTaskDescException, EmptyTaskTimeException { | ||
| if ("".equals(taskString)) { | ||
| throw new EmptyTaskDescException(); | ||
| } | ||
|
|
||
| String[] taskInfo; | ||
| Task newTask; | ||
|
|
||
| switch (type) { | ||
| case TODO: | ||
| newTask = new Todo(taskString); | ||
| break; | ||
| case EVENT: | ||
| taskInfo = taskString.split("/at"); | ||
| if (taskInfo.length < 2) { | ||
| throw new EmptyTaskTimeException(); | ||
| } | ||
| newTask = new Event(taskInfo[0].trim(), taskInfo[1].trim()); | ||
| break; | ||
| case DEADLINE: | ||
| taskInfo = taskString.split("/by"); | ||
| if (taskInfo.length < 2) { | ||
| throw new EmptyTaskTimeException(); | ||
| } | ||
| newTask = new Deadline(taskInfo[0].trim(), taskInfo[1].trim()); | ||
| break; | ||
| default: | ||
| return null; | ||
| } | ||
|
|
||
| taskList.add(newTask); | ||
| return newTask; | ||
| } | ||
|
|
||
| private TaskTypes getTaskType(String type) throws NoSuchTaskException { | ||
| switch(type) { | ||
| case "todo": | ||
| return TaskTypes.TODO; | ||
| case "event": | ||
| return TaskTypes.EVENT; | ||
| case "deadline": | ||
| return TaskTypes.DEADLINE; | ||
| default: | ||
| throw new NoSuchTaskException(type); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package exception; | ||
|
|
||
| public class EmptyTaskDescException extends Exception { | ||
|
|
||
| public EmptyTaskDescException() { | ||
| super ("OOPS!!! The description of a todo cannot be empty."); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package exception; | ||
|
|
||
| public class EmptyTaskTimeException extends Exception { | ||
|
|
||
| public EmptyTaskTimeException() { | ||
| super ("OOPS!!! You need to specify a time."); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| package exception; | ||
|
|
||
| public class NoSuchTaskException extends Exception { | ||
|
|
||
| public NoSuchTaskException(String type) { | ||
| super("I'm sorry, there is no task of type " + type); | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package tasks; | ||
|
|
||
| import utils.DateParser; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Deadline extends Task { | ||
|
|
||
| private final LocalDate localDate; | ||
|
|
||
| public Deadline(String name, String dateStr) { | ||
| super(name); | ||
| localDate = DateParser.stringToDate(dateStr); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D] " + super.toString() + " (by: " + localDate.toString() + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toDataString() { | ||
| return String.format("[D] | %d | %s | %s", | ||
| isMarked() ? 1 : 0, | ||
|
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 could look into replacing the 1 and 0 in the conditional statement with a named constant? It could be considered a "magic number". |
||
| getName(), | ||
| localDate.toString()); | ||
| } | ||
|
|
||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| package tasks; | ||
|
|
||
| import utils.DateParser; | ||
|
|
||
| import java.time.LocalDate; | ||
|
|
||
| public class Event extends Task { | ||
|
|
||
| private final LocalDate localDate; | ||
|
|
||
| public Event(String name, String dateStr) { | ||
| super(name); | ||
| localDate = DateParser.stringToDate(dateStr); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E] " + super.toString() + " (at: " + localDate.toString() + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toDataString() { | ||
| return String.format("[E] | %d | %s | %s", | ||
| isMarked() ? 1 : 0, | ||
| getName(), | ||
| localDate.toString()); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| package tasks; | ||
|
|
||
| import java.io.*; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class SaveData { | ||
|
|
||
| private List<Task> taskList; | ||
|
|
||
| public SaveData() { | ||
| this.taskList = new ArrayList<>(); | ||
| } | ||
|
|
||
| public List<Task> getTaskList() { | ||
| return taskList; | ||
| } | ||
|
|
||
| public void setTaskList(List<Task> taskList1) { | ||
| taskList = taskList1; | ||
| } | ||
|
|
||
| public void loadFromFile(File f) { | ||
| try { | ||
| BufferedReader reader = new BufferedReader(new FileReader(f)); | ||
| taskList.clear(); | ||
| reader.lines().forEach((s) -> { | ||
| // System.out.println(s); | ||
| taskList.add(Task.fromDataString(s)); | ||
| }); | ||
| } catch (IOException e) { | ||
| System.out.println("Error while loading from file: " + e); | ||
| } | ||
| } | ||
|
|
||
| public void saveToFile(File f) { | ||
| try { | ||
| BufferedWriter writer = new BufferedWriter(new FileWriter(f)); | ||
| for (Task task : taskList) { | ||
| writer.write(task.toDataString()); | ||
| writer.write('\n'); | ||
| } | ||
| writer.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Error while saving file: " + e); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| SaveData data = new SaveData(); | ||
| List<Task> newTaskList = new ArrayList<>(); | ||
| newTaskList.add(new Deadline("Deadline task", "September")); | ||
| newTaskList.add(new Todo("Todo task")); | ||
| newTaskList.add(new Event("Event task", "October")); | ||
| data.setTaskList(newTaskList); | ||
| data.saveToFile(new File("savedata.txt")); | ||
| data.loadFromFile(new File("savedata.txt")); | ||
| } | ||
|
|
||
| /* | ||
| File specification: | ||
| [T] | 1 | Task Name | Time | ||
| */ | ||
|
|
||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be great if javadocs are included :))