Skip to content
Open
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
f0ecd9c
add chatbot name
hongyijie06 Jan 26, 2024
e0b6eed
Add echo
hongyijie06 Feb 7, 2024
35ec609
add Add,list
hongyijie06 Feb 7, 2024
0c16724
add mark as done
hongyijie06 Feb 9, 2024
1b6ca15
add A-CodingStandard
hongyijie06 Feb 9, 2024
6ef3078
add todo, deadline and event
hongyijie06 Feb 16, 2024
b540106
add code quality
hongyijie06 Feb 19, 2024
c230255
add error handling
hongyijie06 Feb 20, 2024
bedc8ec
make error handling more error-specific
hongyijie06 Feb 20, 2024
4793c54
Revert "make error handling more error-specific"
hongyijie06 Feb 20, 2024
c43aef3
Revert "Revert "make error handling more error-specific""
hongyijie06 Feb 20, 2024
e0bf4a2
add delete
hongyijie06 Feb 20, 2024
7078d98
Merge branch 'branch-Level-6'
hongyijie06 Feb 22, 2024
4de283c
settle errors
hongyijie06 Feb 22, 2024
e44d14e
add save
hongyijie06 Feb 22, 2024
011c695
resolve errors
hongyijie06 Feb 22, 2024
0bc39f6
Merge branch 'branch-Level-7'
hongyijie06 Feb 22, 2024
b1fd82d
fix formatting when listing tasks
hongyijie06 Mar 2, 2024
61216db
add saveToFile
hongyijie06 Mar 2, 2024
996ba2b
fix file not found error
hongyijie06 Mar 3, 2024
7162ac7
use more oop
hongyijie06 Mar 6, 2024
7a5826c
fix loading file errors
hongyijie06 Mar 6, 2024
4a60dbe
add find
hongyijie06 Mar 7, 2024
422ac12
Merge branch 'branch-Level-9'
hongyijie06 Mar 7, 2024
2152ef7
add javadoc
hongyijie06 Mar 7, 2024
b611a02
Merge pull request #1 from hongyijie06/branch-A-JavaDoc
hongyijie06 Mar 7, 2024
d141099
Merge branch 'master' of https://github.com/hongyijie06/ip
hongyijie06 Mar 7, 2024
6752306
Add user guide
hongyijie06 Mar 7, 2024
2f4b481
Format headers
hongyijie06 Mar 7, 2024
4f03af2
follow coding standards
hongyijie06 Mar 8, 2024
fbcc37c
add README pictures
hongyijie06 Mar 8, 2024
456bcb0
add pictures
hongyijie06 Mar 8, 2024
dc9a769
improve formatting, no pictures
hongyijie06 Mar 8, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Binary file added production/main/CreateFile.class
Binary file not shown.
Binary file added production/main/Deadline.class
Binary file not shown.
Binary file added production/main/Duke.class
Binary file not shown.
Binary file added production/main/EmptyLineException.class
Binary file not shown.
Binary file added production/main/Event.class
Binary file not shown.
Binary file added production/main/ManageInputs.class
Binary file not shown.
Binary file added production/main/ReadFileContents.class
Binary file not shown.
Binary file added production/main/Task.class
Binary file not shown.
Binary file added production/main/Todo.class
Binary file not shown.
Binary file added production/main/UnexpectedCommandException.class
Binary file not shown.
8 changes: 8 additions & 0 deletions src/.idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/.idea/.name

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/misc.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions src/.idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions src/.idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

16 changes: 16 additions & 0 deletions src/TaskList.txt
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)
22 changes: 22 additions & 0 deletions src/main/java/CreateFile.java
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());
}
}

Copy link
Copy Markdown

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!

}
13 changes: 13 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Deadline extends Task{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 + ")";
}
}
Binary file added src/main/java/Duke.class
Binary file not shown.
42 changes: 35 additions & 7 deletions src/main/java/Duke.java
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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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");

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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);
}
}
2 changes: 2 additions & 0 deletions src/main/java/EmptyLineException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
public class EmptyLineException extends Exception{
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Formatting error for Exception{. Also, I'm not sure why your exception class is empty?

13 changes: 13 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
public class Event extends Task{

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 + ")";
}
}
209 changes: 209 additions & 0 deletions src/main/java/ManageInputs.java
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++;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a good application of SLAP! But perhaps the variable sLine could be renamed to make it clearer what this means?

}
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();
}
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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 throw the exception straight?


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 {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The 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...

  1. What parts of the code is repetitive? i.e. virtually the same for each if/else block you have
  2. What part of the code does a particular function that can be isolated into a function? (like printing tasks, or doing something to the file?)

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]);

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

index and idx are being used in the code but seem to be referring to different things. It's not too clear what they really mean! Do consider how you could make this function more readable!

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!");
}
}
25 changes: 25 additions & 0 deletions src/main/java/ReadFileContents.java
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");
}
}

}
Loading