-
Notifications
You must be signed in to change notification settings - Fork 223
[Sunaga] iP #240
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?
[Sunaga] iP #240
Changes from 23 commits
3b19ba1
a75fcee
a47d697
868d263
40da541
f7a9485
5c1e2d7
277ea31
286f0e6
3c427af
6d87412
217f18d
2da7257
7ce01bc
5ea0390
83e25ad
a92c7bf
bbed8dd
2d4fffb
a97bea5
8edf5a9
83214d0
b1c6137
8060ea0
02d7dde
82e1692
1cb4a2c
d81a8ac
2c0ccfe
e7c433d
15ecb72
357647e
87ee16c
43ee981
c2494cb
622cffe
b4954a5
fe998fc
933c244
0742e3b
004dd6d
b066cc9
b7eae57
c1158be
f839ecc
d0fd76b
6b9e7ba
2506408
1d9c26a
7d6099f
1c411b5
ec1cf9c
1e1920d
f6bdb5f
7221b25
a577fa0
1a5be77
e532b61
8dad726
10f881f
7ece23f
d507f93
4a8e6c8
c1e705b
df8672a
cc86996
25557d1
418f1b7
78d7172
ab50e57
f43eca3
123ad30
6c56882
1cd8274
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,141 @@ | ||
| package duke; | ||
|
|
||
| import duke.exceptions.DukeException; | ||
| import duke.exceptions.DukeIDKException; | ||
| import duke.exceptions.DukeInvalidDesException; | ||
| import duke.exceptions.DukeMissingDesException; | ||
| import duke.handler.Parser; | ||
| import duke.handler.Queries; | ||
| import duke.tasks.*; | ||
| import duke.tasks.Event; | ||
|
|
||
| import java.awt.*; | ||
|
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. Same issue as above |
||
| import java.io.File; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| private boolean exit = false; | ||
|
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. isExit |
||
| private Storage storage; | ||
| private TaskList tasks; | ||
| private Ui ui; | ||
|
|
||
| private Duke(String storagePath) { | ||
| storage = new Storage(storagePath); | ||
| tasks = storage.load(); | ||
| ui = new Ui(); | ||
| } | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| new Duke("data/tasks.txt").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. You may want to use relative path
Author
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. Hi my understanding was that this already is a relative path. |
||
| } | ||
|
|
||
| public void run() { | ||
| ui.displayIntro(); | ||
| while (exit == false) { | ||
| processInput(ui.readCommand()); | ||
| } | ||
| ui.close(); | ||
| } | ||
|
|
||
|
|
||
| private void processInput(String userInput) { | ||
| String keyword_UC = userInput.toUpperCase().split(" ", -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 use a camelCase variable name and a more descriptive name rather than |
||
| Queries query = Queries.ADD; | ||
| String response = ""; | ||
|
|
||
| if (Queries.containsValue(keyword_UC)) { | ||
| query = Queries.valueOf(keyword_UC); | ||
| } | ||
|
|
||
| try { | ||
| switch (query) { | ||
| case BYE: | ||
| response = "Bye. Hope to see you again soon!"; | ||
| ui.respond(response); | ||
| exit = true; | ||
| break; | ||
|
|
||
| case ADD: | ||
| Task toAdd; | ||
|
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. You may want to abstract each case into a separate method |
||
| try { | ||
| if (keyword_UC.equals("TODO")) { | ||
| toAdd = new Todo(userInput.split(" ", 2)[1]); | ||
| } else if (keyword_UC.equals("DEADLINE")) { | ||
| String[] info = userInput.split(" ", 2); | ||
| if (info[1].contains("/by")) { | ||
| info = info[1].split("/by"); | ||
| } else { | ||
| throw new DukeInvalidDesException(keyword_UC); | ||
| } | ||
| toAdd = new Deadline(info[0], info[1]); | ||
| } else if (keyword_UC.equals("EVENT")) { | ||
| String[] info = userInput.split(" ", 2); | ||
| if (info[1].contains("/at")) { | ||
| info = info[1].split(("/at")); | ||
| } else { | ||
| throw new DukeInvalidDesException(keyword_UC); | ||
| } | ||
| toAdd = new Event(info[0], info[1]); | ||
| } else { | ||
| throw new DukeIDKException(); | ||
| } | ||
| tasks.addTask(toAdd); | ||
| response = "Got it. I've added this task:\n" | ||
| + " " + toAdd + "\n" | ||
| + "Now you have " + tasks.getNumOfTasks() + " tasks in the list.\n"; | ||
| ui.respond(response); | ||
| storage.addTask(toAdd); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| throw new DukeMissingDesException(keyword_UC); | ||
| } | ||
| break; | ||
|
|
||
| case DONE: | ||
| try { | ||
| int taskNum = Integer.parseInt(userInput.split(" ")[1]); | ||
| tasks.markDone(taskNum); | ||
| Task updatedTask = tasks.getTask(taskNum); | ||
| response = "Nice! I've marked this task as done: \n" | ||
| + " " + updatedTask+ "\n"; | ||
| ui.respond(response); | ||
| storage.markDoneInStorage(updatedTask, taskNum); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| throw new DukeMissingDesException("DONE"); | ||
| } catch (IndexOutOfBoundsException | NumberFormatException e) { | ||
| throw new DukeInvalidDesException("DONE"); | ||
| } | ||
| break; | ||
|
|
||
| case DELETE: | ||
| try { | ||
| int taskNum = Integer.parseInt(userInput.split(" ")[1]); | ||
| Task task = tasks.getTask(taskNum); | ||
| tasks.removeTask(taskNum); | ||
| response = "Noted. I've removed this task: \n" | ||
| + " " + task + "\n"; | ||
| ui.respond(response); | ||
| storage.delete(taskNum); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| throw new DukeMissingDesException("DELETE"); | ||
| } catch (IndexOutOfBoundsException | NumberFormatException e) { | ||
| throw new DukeInvalidDesException("DELETE"); | ||
| } | ||
| break; | ||
| case LIST: | ||
| response += tasks.toString(); | ||
| ui.respond(response); | ||
| break; | ||
| } | ||
| } catch (DukeException e) { | ||
| String output = e.getMessage(); | ||
| ui.respond(output); | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,106 @@ | ||
| package duke; | ||
|
|
||
| import duke.handler.Parser; | ||
| import duke.tasks.Deadline; | ||
| import duke.tasks.Event; | ||
| import duke.tasks.Task; | ||
| import duke.tasks.TaskList; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.charset.StandardCharsets; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.nio.file.Paths; | ||
| import java.util.List; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Storage { | ||
| private static String STORAGE_PATH; | ||
|
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 this instance variable should be declared final instead? (as your variable name is already in all uppercase letters) |
||
| private static final int MARK_INDEX = 2; | ||
|
|
||
| public Storage(String storagePath) { | ||
| STORAGE_PATH = storagePath; | ||
| } | ||
|
|
||
| public TaskList load() { | ||
| TaskList tasks = new TaskList(); | ||
| Scanner dataReader = null; | ||
| try{ | ||
| File file = new File(STORAGE_PATH); | ||
| file.getParentFile().mkdirs(); | ||
| file.createNewFile(); | ||
|
|
||
| dataReader = new Scanner(file); | ||
| while (dataReader.hasNextLine()) { | ||
| Task task = Parser.parseFromData(dataReader.nextLine()); | ||
| tasks.addTask(task); | ||
| } | ||
| return tasks; | ||
| } catch (IOException e) { | ||
| System.out.println("File did not load"); | ||
| } finally { | ||
| dataReader.close(); | ||
| } | ||
| return tasks; | ||
| } | ||
|
|
||
| public void addTask(Task task){ | ||
| try { | ||
| FileWriter fw = new FileWriter(STORAGE_PATH, true); | ||
| String toWrite =""; | ||
| switch(task.getType()) { | ||
| case "TODO": | ||
| toWrite = String.format("%c|%c|%s", | ||
| 'T', | ||
| task.isDone() ? 'X' : ' ', | ||
| task.getDescription()); | ||
| break; | ||
| case "DEADLINE": | ||
| toWrite = String.format("%c|%c|%s|%s", | ||
| 'D', | ||
| task.isDone() ? 'X' : ' ', | ||
| task.getDescription(), | ||
| ((Deadline) task).getBy()); | ||
| break; | ||
| case "EVENT": | ||
| toWrite = String.format("%c|%c|%s|%s", | ||
| 'E', | ||
| task.isDone() ? 'X' : ' ', | ||
| task.getDescription(), | ||
| ((Event) task).getAt()); | ||
| break; | ||
| } | ||
| fw.write(toWrite + "\n"); | ||
| fw.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("File could not be written to. Could not add task."); | ||
| } | ||
| } | ||
|
|
||
| public void markDoneInStorage(Task task, int taskNum) { | ||
| try { | ||
| Path path = Paths.get(STORAGE_PATH); | ||
| List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
| String line = lines.get(taskNum - 1); | ||
| StringBuilder updatedLine = new StringBuilder(line); | ||
| updatedLine.setCharAt(MARK_INDEX, 'X'); | ||
| lines.set(taskNum - 1, updatedLine.toString()); | ||
| Files.write(path, lines, StandardCharsets.UTF_8); | ||
| } catch (IOException e) { | ||
| System.out.println("Fail to mark done in storage file."); | ||
| } | ||
| } | ||
|
|
||
| public void delete(int taskNum) { | ||
| try { | ||
| Path path = Paths.get(STORAGE_PATH); | ||
| List<String> lines = Files.readAllLines(path, StandardCharsets.UTF_8); | ||
| lines.remove(taskNum - 1); | ||
| Files.write(path, lines, StandardCharsets.UTF_8); | ||
| } catch (IOException e) { | ||
| System.out.println("Fail to delete item in storage file."); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package duke; | ||
|
|
||
| import java.io.PrintStream; | ||
| import java.util.Arrays; | ||
| import java.util.Scanner; | ||
| import java.util.stream.Collectors; | ||
|
|
||
| public class Ui { | ||
| private static final String INDENT = " "; | ||
| private static final String TEXT_INDENT = INDENT + "|" + " "; | ||
| private static final String RES_BOX_TOP = INDENT + "_______________________________________________\n"; | ||
| private static final String RES_BOX_BOTTOM = INDENT + "|____________________________________________\n" | ||
| + INDENT + " \\|\n"; | ||
| private static Scanner sc = new Scanner(System.in); | ||
| private final Scanner in; | ||
| private final PrintStream out; | ||
|
|
||
|
|
||
| public Ui() { | ||
| this.in = new Scanner(System.in); | ||
| this.out = System.out; | ||
| } | ||
|
|
||
| private boolean hasCommand() { | ||
| return in.hasNext(); | ||
| } | ||
|
|
||
| public String readCommand() { | ||
| assert hasCommand(); | ||
|
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. Like the use of assertion here 😄 |
||
| return in.nextLine(); | ||
| } | ||
|
|
||
| public void close() { | ||
| in.close(); | ||
| } | ||
|
|
||
| public void displayIntro() { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| String response = "Hello from\n" | ||
| + logo + "\n" | ||
| + "What can I do for you?\n"; | ||
| respond(response); | ||
| } | ||
|
|
||
| public void respond(String response) { | ||
| String indentedResponse = Arrays.stream(response.split("\n")) | ||
| .map(line -> TEXT_INDENT + line) | ||
| .collect(Collectors.joining("\n")) + "\n"; | ||
| String output = RES_BOX_TOP | ||
| + indentedResponse | ||
| + RES_BOX_BOTTOM; | ||
|
|
||
| System.out.println(output); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package duke.exceptions; | ||
|
|
||
| public class DukeException extends Exception{ | ||
|
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. Should there be a space between |
||
| private String message = "OOPS!"; | ||
|
|
||
| public DukeException(String s) { | ||
| super(s); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| package duke.exceptions; | ||
|
|
||
| public class DukeIDKException extends DukeException{ | ||
|
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. Should there be a space between |
||
| private String message; | ||
|
|
||
| /** | ||
| * Constructor for DukeException for incomprehensible request. | ||
| */ | ||
| public DukeIDKException() { | ||
|
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. DukeIdkException? |
||
| super("This is a Duke IDK exception"); | ||
| message = super.getMessage() + " " | ||
| + "I don't get what you mean."; | ||
| } | ||
|
|
||
| /** | ||
| * Returns message of the incomprehensible request. | ||
| * @return Message of the incomprehensible request. | ||
| */ | ||
| @Override | ||
| public String getMessage() { | ||
| return message; | ||
| } | ||
| } | ||
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.
Should this import statement be listed explicitly instead?