-
Notifications
You must be signed in to change notification settings - Fork 184
[hongyijie06] iP #179
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?
[hongyijie06] iP #179
Changes from 17 commits
f0ecd9c
e0b6eed
35ec609
0c16724
1b6ca15
6ef3078
b540106
c230255
bedc8ec
4793c54
c43aef3
e0bf4a2
7078d98
4de283c
e44d14e
011c695
0bc39f6
b1fd82d
61216db
996ba2b
7162ac7
7a5826c
4a60dbe
422ac12
2152ef7
b611a02
d141099
6752306
2f4b481
4f03af2
fbcc37c
456bcb0
dc9a769
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,16 @@ | ||
| [T][0] hike | ||
| [T][0] run | ||
| [D][0] tut (by: 5) | ||
| [E][0] he (from: 5 to: 6) | ||
| [T][0] jump | ||
| [T][0] go | ||
| [D][0] hop (by: 4) | ||
| [E][0] he (from: 4 to: 5) | ||
| [D][0] b (by: 4) | ||
| [E][0] go (from: 4 to: 454) | ||
| [T][0] ds | ||
| [D][0] kj (by: 4) | ||
| [T][0] he | ||
| [E][0] f (from: 4 to: 5) | ||
| [D][0] sd (by: 5) | ||
| [D][0] hsd (by: 343354535) |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import java.io.File; | ||
| import java.io.IOException; | ||
|
|
||
| public class CreateFile { | ||
| public static void main(String[] args) { | ||
| File f = new File("TaskList.txt"); | ||
| if (!f.exists()) { | ||
| try { | ||
| if (f.createNewFile()) { | ||
| System.out.println("File created: " + f.getAbsolutePath()); | ||
| } else { | ||
| System.out.println("File creation failed."); | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("An error occurred."); | ||
| e.printStackTrace(); | ||
| } | ||
| } else { | ||
| System.out.println("File already exists: " + f.getAbsolutePath()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class Deadline extends Task{ | ||
|
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. Formatting error here! |
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,38 @@ | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.io.IOException; | ||
| import java.sql.Array; | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Duke { | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
|
|
||
| private static void printFileContents(String filePath) throws FileNotFoundException { | ||
| File f = new File(filePath); // create a File for the given file path | ||
| Scanner s = new Scanner(f); // create a Scanner using the File as the source | ||
| while (s.hasNext()) { | ||
| System.out.println(s.nextLine()); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) throws UnexpectedCommandException, EmptyLineException, IOException { | ||
|
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. Do refactor out your magic string values! |
||
| ArrayList<Task> tasks = new ArrayList<Task>(); | ||
|
|
||
| //greeting | ||
|
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. Your comment could be more descriptive! |
||
| System.out.println("Hello! I'm Apple"); | ||
| System.out.println("What can I do for you?"); | ||
| try { | ||
| System.out.println("Here are the items in your task list: "); | ||
| printFileContents("TaskList.txt"); | ||
|
|
||
|
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. Is printing out the tasks on startup necessary? |
||
| } catch (FileNotFoundException e) { | ||
| File f = new File("TaskList.txt"); | ||
| System.out.println("File not found"); | ||
| System.out.println("File created: " + f.getAbsolutePath()); | ||
| } | ||
| int index = 0;//number of items in the list | ||
| String line = " "; | ||
|
|
||
| new ManageInputs(tasks, index, line); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| public class EmptyLineException 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. Formatting error for |
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| public class Event extends Task{ | ||
|
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. Formatting issue with the curly brace |
||
| protected String from; | ||
| protected String to; | ||
| public Event(String description, String from, String to){ | ||
| super(description); | ||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
| @Override | ||
|
Comment on lines
+8
to
+14
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 can consider adding a newline between line 8 & 9 to avoid making it look too squashed! |
||
| public String toString() { | ||
| return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,209 @@ | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
|
|
||
| public class ManageInputs { | ||
| protected static String from; | ||
| protected static String to; | ||
| public static String description; | ||
| protected static String by; | ||
|
|
||
| private static void writeToFile(String filePath, Task textToAdd) throws IOException { | ||
| FileWriter fw = new FileWriter(filePath, true); | ||
| fw.write(textToAdd + "\n"); | ||
| fw.close(); | ||
| } | ||
|
|
||
| private static int fillFileContents(ArrayList<Task> tasks, String filePath, int index) throws IOException, UnexpectedCommandException {//updates index | ||
| File f = new File(filePath); // create a File for the given file path | ||
| Scanner s = new Scanner(f); // create a Scanner using the File as the source | ||
|
|
||
| while (s.hasNext()) { | ||
| String sLine = s.nextLine(); | ||
| if (sLine.contains("[E]")) { | ||
| dealWithEvent(tasks, index, sLine); | ||
| } else if (sLine.contains("[D]")) { | ||
| dealWithDeadline(tasks, index, sLine); | ||
| } else if (sLine.contains("[T]")) { | ||
| dealWithTodo(tasks, index, sLine); | ||
| } | ||
| index++; | ||
|
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 is a good application of SLAP! But perhaps the variable |
||
| } | ||
| return index; | ||
| } | ||
|
|
||
| public static void dealWithEvent(ArrayList<Task> tasks, int index, String line) throws UnexpectedCommandException, IOException { | ||
| int indexTo = line.indexOf("to"); | ||
| int indexFrom = line.indexOf("from"); | ||
|
|
||
| if ((indexTo == -1) || (indexFrom == -1)) { //invalid format | ||
| System.out.println("Invalid format! Enter event in the format: event (description) from (start) to (end)"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| try {//timeline not specified/ both not specified | ||
| String from = line.substring(indexFrom + 5, indexTo - 1); | ||
| String to = line.substring(indexTo + 2); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| try { | ||
| String description = line.substring(6, indexFrom - 1); | ||
| } catch (IndexOutOfBoundsException f) { | ||
| System.out.println("event description and timeline not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| System.out.println("event timeline not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| try {//description not specified | ||
| String description = line.substring(6, indexFrom - 1); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.println("event description not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| String from = line.substring(indexFrom + 5, indexTo - 1); | ||
| String to = line.substring(indexTo + 2); | ||
| String description = line.substring(5, indexFrom - 1); | ||
|
|
||
| tasks.add(index, new Event(description, from, to)); | ||
| } | ||
|
|
||
| public static void dealWithDeadline(ArrayList<Task> tasks, int index, String line) throws UnexpectedCommandException, IOException { | ||
| int indexBy = line.indexOf("by"); | ||
| int space = line.indexOf(" "); | ||
| if (indexBy == -1) {//invalid format | ||
| System.out.println("Invalid format! Enter deadline in the format: deadline (description) by (deadline)"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| try {//deadline / both not specified | ||
| String by = line.substring(indexBy + 2); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| try { | ||
| String description = line.substring(space, indexBy - 1); | ||
| } catch (IndexOutOfBoundsException f) { | ||
| System.out.println("deadline description and deadline not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| System.out.println("deadline not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| try {//deadline not specified | ||
| String description = line.substring(space, indexBy - 1); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.println("deadline description not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| String description = line.substring(space, indexBy - 1); | ||
| String by = line.substring(indexBy + 2); | ||
|
|
||
| tasks.add(index, new Deadline(description, by)); | ||
| } | ||
|
|
||
| public static void dealWithTodo(ArrayList<Task> tasks, int index, String line) throws UnexpectedCommandException, IOException { | ||
| int indexSpace = line.indexOf(" "); | ||
| if (indexSpace == -1) { | ||
| System.out.println("todo description not specified"); | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
|
|
||
| String description = line.substring(indexSpace); | ||
|
|
||
| tasks.add(index, new Todo(description)); | ||
|
|
||
| } | ||
|
|
||
| private void handleUnexpectedCommand(boolean isValidCommand) throws UnexpectedCommandException { | ||
| if (!isValidCommand) { | ||
| throw new UnexpectedCommandException(); | ||
| } | ||
| } | ||
|
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. I'm not sure what this abstraction is supposed to do. Is this necessary? If a command is invalid, is it possible to just |
||
|
|
||
| private void handleEmptyInput(String line) throws EmptyLineException { | ||
| if (line.isEmpty()) { | ||
| throw new EmptyLineException(); | ||
| } | ||
| } | ||
|
|
||
| public ManageInputs(ArrayList<Task> tasks, int index, String line) throws IOException, UnexpectedCommandException { | ||
|
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 function is doing quite a fair bit of heavy lifting. It's very long, which usually indicates that perhaps you could apply SLAP more here. To aid you in achieving SLAP, here are some guiding questions...
|
||
| index = fillFileContents(tasks, "TaskList.txt", index); | ||
| while (!line.equals("bye")) { | ||
| Boolean isValidCommand = false; | ||
| Scanner input = new Scanner(System.in); | ||
| line = input.nextLine(); | ||
|
|
||
| Task t = new Task(line); | ||
| String[] inputs = line.split(" "); | ||
|
|
||
| if (inputs[0].equals("mark")) {//mark as done | ||
| isValidCommand = true; | ||
| int idx = Integer.parseInt(inputs[1]); | ||
|
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.
|
||
| tasks.get(idx - 1).markAsDone(); | ||
| System.out.println("Nice! I've marked this task as done: "); | ||
| System.out.println(tasks.get(idx - 1)); | ||
| } else if (inputs[0].equals("unmark")) {//unmark done | ||
| isValidCommand = true; | ||
| int idx = Integer.parseInt(inputs[1]); | ||
| tasks.get(idx - 1).unmarkDone(); | ||
| System.out.println("OK, I've marked this task as not done yet: "); | ||
| System.out.println(tasks.get(idx - 1)); | ||
| } else if (line.equals("list")) {//lists tasks | ||
| isValidCommand = true; | ||
| System.out.println("Here are the tasks in your list: "); | ||
|
|
||
| for (int i = 0; i < index; i++) { | ||
| System.out.println((i + 1) + ". " + tasks.get(i)); | ||
| } | ||
| } else if (line.equals("bye")) {//exit chat | ||
| isValidCommand = true; | ||
| break; | ||
| } else if (inputs[0].equals("event") || inputs[0].equals("todo") || inputs[0].equals("deadline")) {//add items | ||
| try { | ||
| if (inputs[0].equals("event")) { | ||
| isValidCommand = true; | ||
| dealWithEvent(tasks, index, line); | ||
| writeToFile("TaskList.txt", tasks.get(index)); | ||
| System.out.println("Got it. I've added this task: "); | ||
| System.out.println(tasks.get(index)); | ||
| index++; | ||
| } else if (inputs[0].equals("deadline")) { | ||
| isValidCommand = true; | ||
| dealWithDeadline(tasks, index, line); | ||
| writeToFile("TaskList.txt", tasks.get(index)); | ||
| System.out.println("Got it. I've added this task: "); | ||
| System.out.println(tasks.get(index)); | ||
| index++; | ||
| } else { | ||
| isValidCommand = true; | ||
| dealWithTodo(tasks, index, line); | ||
| writeToFile("TaskList.txt", tasks.get(index)); | ||
| System.out.println("Got it. I've added this task: "); | ||
| System.out.println(tasks.get(index)); | ||
| index++; | ||
| } | ||
| } catch (UnexpectedCommandException e) { | ||
| } catch (IOException e) { | ||
| throw new RuntimeException(e); | ||
| } | ||
| System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
| } else if (inputs[0].equals("delete")) { | ||
| isValidCommand = true; | ||
| int idx = Integer.parseInt(inputs[1]); | ||
| System.out.println("Noted. I've removed this task: "); | ||
| System.out.println(tasks.get(idx - 1)); | ||
| tasks.remove(idx - 1); | ||
| System.out.println("Now you have " + tasks.size() + " tasks in the list."); | ||
| index--; | ||
| } else { | ||
| try { | ||
| handleEmptyInput(line); | ||
| handleUnexpectedCommand(isValidCommand); | ||
| } catch (UnexpectedCommandException e) { | ||
| System.out.println("please enter a valid command"); | ||
| } catch (EmptyLineException e) { | ||
| System.out.println("enter a task"); | ||
| } | ||
| } | ||
| } | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,25 @@ | ||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class ReadFileContents { | ||
|
|
||
| private static void printFileContents(String filePath) throws FileNotFoundException { | ||
| File f = new File(filePath); // create a File for the given file path | ||
| Scanner s = new Scanner(f); // create a Scanner using the File as the source | ||
| while (s.hasNext()) { | ||
| System.out.println(s.nextLine()); | ||
| } | ||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| try { | ||
| printFileContents("TaskList.txt"); | ||
|
|
||
| } catch (FileNotFoundException e) { | ||
| new CreateFile(); | ||
| System.out.println("File not found"); | ||
| } | ||
| } | ||
|
|
||
| } |
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.
Do use more descriptive names for your exceptions and files.
Avoid using magic values (the strings) as well!