Skip to content
Open
Show file tree
Hide file tree
Changes from 23 commits
Commits
Show all changes
36 commits
Select commit Hold shift + click to select a range
bcf20ee
Week 2 Task
yzhedwin Aug 22, 2021
2c2a310
Level-1
yzhedwin Aug 26, 2021
da7c42b
Level-2
yzhedwin Aug 26, 2021
7e2f13a
Level-3
yzhedwin Aug 26, 2021
0b9ce39
Completed A-CodingStandard
yzhedwin Aug 26, 2021
074899d
Level-4 Completed
yzhedwin Sep 3, 2021
4ce8ac0
Level-4, A-CodeQuality
yzhedwin Sep 3, 2021
c2a8cf3
Completed Level-5
yzhedwin Sep 7, 2021
e61f1e7
Completed Level-5,A-Exceptions
yzhedwin Sep 7, 2021
daef42c
Updated Ui Testing
yzhedwin Sep 7, 2021
3fc7b81
Created new branch as part of increments
yzhedwin Sep 7, 2021
ed20d64
Merge branch-Level-5 with master
yzhedwin Sep 7, 2021
e8ce66f
Completed A-Packages and improve coding standards
yzhedwin Sep 7, 2021
acfd97c
Merge branch-A-Packages with master
yzhedwin Sep 7, 2021
88ad15e
Updated runtest.bat
yzhedwin Sep 7, 2021
d3cbc29
Formatted some codes
yzhedwin Sep 7, 2021
b573d4c
Code Fixes
yzhedwin Sep 7, 2021
3974bd3
Improved A-Exception
yzhedwin Sep 7, 2021
22af079
Added delete feature
yzhedwin Sep 15, 2021
3c647fc
Added save feature and some personality for Level-7 requirement
yzhedwin Sep 16, 2021
a294af8
Minor changes to save.txt
yzhedwin Sep 16, 2021
86a9032
Merge branch Level-6 with master
yzhedwin Sep 16, 2021
c3af9c5
Merge branch Level-7 with master
yzhedwin Sep 16, 2021
92cbe7d
Added more OOP and fixed exception handling
yzhedwin Sep 27, 2021
ea44036
Added Date and Time functionality
yzhedwin Sep 29, 2021
d57c1a3
Merge pull request #1 from yzhedwin/branch-Level-8
yzhedwin Sep 29, 2021
a2984d6
Added Find Feature
yzhedwin Sep 29, 2021
4a890e0
Merge pull request #2 from yzhedwin/branch-Level-9
yzhedwin Sep 29, 2021
8a47b70
Documentation for A-JavaDoc
yzhedwin Sep 29, 2021
5217ca0
Merge pull request #3 from yzhedwin/branch-A-JavaDoc
yzhedwin Sep 29, 2021
d3eb8f0
Updated JavaDoc
yzhedwin Sep 29, 2021
468e2f1
Completed User Guide
yzhedwin Sep 30, 2021
9c9496a
Set theme jekyll-theme-cayman
yzhedwin Sep 30, 2021
0f9c8b7
Completed User Guide
yzhedwin Sep 30, 2021
788449d
Merge branch 'master' of github.com:yzhedwin/ip
yzhedwin Sep 30, 2021
e8616f6
Build ip.jar
yzhedwin Sep 30, 2021
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
6 changes: 6 additions & 0 deletions .classpath
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
<classpathentry kind="src" path="src/main/java"/>
<classpathentry kind="output" path="out/production/ip"/>
</classpath>
17 changes: 17 additions & 0 deletions .project
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<?xml version="1.0" encoding="UTF-8"?>
<projectDescription>
<name>ip</name>
<comment></comment>
<projects>
</projects>
<buildSpec>
<buildCommand>
<name>org.eclipse.jdt.core.javabuilder</name>
<arguments>
</arguments>
</buildCommand>
</buildSpec>
<natures>
<nature>org.eclipse.jdt.core.javanature</nature>
</natures>
</projectDescription>
3 changes: 3 additions & 0 deletions data/save.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
D/0/cs10102/monday
E/1/cs2020/com
T/0/cs1010
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

3 changes: 3 additions & 0 deletions src/main/java/META-INF/MANIFEST.MF
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
Manifest-Version: 1.0
Main-Class: duke.Duke

210 changes: 210 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
package duke;

import duke.command.CommandList;
import duke.task.Task;

import java.io.File;
import java.io.FileNotFoundException;
import java.util.ArrayList;
import java.util.Scanner;
import java.io.FileWriter;
import java.io.IOException;


public class Duke {
private static final int FOUR = 4;
private static final int FIVE = 5;
private static final int SIX = 6;;
private static final int EIGHT = 8;
private static final int NINE = 9;

Choose a reason for hiding this comment

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

These string constants to refer to an integer may seem redundant and self-explanatory.
If you are working to replace them with magic numbers I would suggest more meaningful names.

private static final int ERROR_TODO_IS_EMPTY = 1;
private static final int ERROR_EVENT_IS_EMPTY = 2;
private static final int ERROR_DEADLINE_IS_EMPTY = 3;
private static final int ERROR_COMMAND_NOT_FOUND = 4;
private static final int ERROR_IS_INVALID = 5;
private static final int CMD_NOT_FOUND = 0;
private static final int CMD_TODO = 1;
private static final int CMD_EVENT = 2;
private static final int CMD_DEADLINE = 3;
private static final int CMD_LIST = 4;
private static final int CMD_DONE = 5;
private static final int CMD_TERMINATE = 6;
private static final int CMD_DELETE = 7;
private static final String LIST = "list";
private static final String TODO = "todo";
private static final String EVENT = "event";
private static final String DEADLINE = "deadline";
private static final String DONE = "done";
private static final String BYE = "bye";
private static final String DELETE = "delete";
private static final String BY = "/by";
private static final String AT = "/at";
private static final ArrayList<Task> items = new ArrayList<>();
private static final String fileDir = "./data";
private static final String fileName = "./data/save.txt";
private static final String logo =
" _____ ___ _____ ______\n"
+ "|___ | | | |_____| / / -- \\ \\ \n"
+ " / / | | / / | | | | \n"
+ " / / | | / / | | | |\n"
+ " / /___ | | /_/__ | | -- | |\n"
+ "|_____| | | |_____| \\ \\____/ /\n";
private static final String border = "____________________________________________________________\n";


public static void printStartMessage() {
System.out.println(logo);
System.out.println(border + "Hi bro, my name is Echo");
System.out.println("What do you want?\n" + border);
System.out.println("Type bye to exit\n" + border);
}
public static boolean isInvalid(CommandList task, String line , String key) {
if (!line.split(key)[1].trim().isEmpty()) {

Choose a reason for hiding this comment

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

This expression is a little complicated - calling 3 methods and a negation. See how to improve the quality of expressions here.

if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) {
return true;
}
}
return (line.length() <= FIVE);
}
private static void createSave() {
File saveFile = new File(Duke.fileName);
File saveDir = new File(Duke.fileDir);
if (saveDir.mkdirs()) {
System.out.println("Successfully created save dir");
} else {
System.out.println("Save folder already exists.");
}
try {
if (saveFile.createNewFile()) {
System.out.println("Successfully created save file");
} else {
System.out.println("Save file already exists");
}
} catch (IOException e) {
e.printStackTrace();

Choose a reason for hiding this comment

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

How about including the previous statements in the try catch block? After all, this method is doing file related operations and the program could catch file related exceptions.

}
}

private static void updateSave() {
Task[] saveLists = new Task[items.size()];
items.toArray(saveLists);
try {
FileWriter fw = new FileWriter(Duke.fileName);
for (Task saveList : saveLists) {
fw.write(saveList.toString().charAt(1) + "/");
fw.write(saveList.getStatus() + "/" + saveList.getDescription());

Choose a reason for hiding this comment

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

For the parameters to the file writer, you could have declared variables for each for them to improve readability. E.g. fileStatus = saveList.getStatus().

if (!saveList.getDate().equals("empty")) {
fw.write("/" + saveList.getDate());
}
fw.write("\n");
}
System.out.println("Automatically saved to " + fileName);
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
}

private static void readSave(CommandList task) throws FileNotFoundException {

Choose a reason for hiding this comment

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

It would be good to provide method descriptions as comments/javadocs to improve code readability.

File saveFile = new File(Duke.fileName);
Scanner s = new Scanner(saveFile);
while (s.hasNext()) {
String[] chars = s.nextLine().split("/");
String type = chars[0].trim();
String status = chars[1].trim();
String description = chars[2].trim();
switch (type) {
case "E":
String date = chars[3].trim();
task.addEvent(Duke.items, description, date);
if (status.equals("1")) {
items.get(task.getTaskCount()).markDone();
}
break;
case "D":
date = chars[3].trim();
task.addDeadline(Duke.items, description, date);
if (status.equals("1")) {
items.get(task.getTaskCount()).markDone();
}
break;
case "T":
task.addTodo(Duke.items, description);
break;
}
}
task.loadTaskCount(items);
System.out.println("Save file successfully loaded");
}

public static String readCommand(Scanner in, CommandList task, DukeException error) {

Choose a reason for hiding this comment

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

This method is parsing the command string, but it is not obvious how it is done with seemingly arbitrary constants like FOUR FIVE EIGHT etc. You could consider a more direct way of comparing strings and checking parameters.

String line;
do {
line = in.nextLine();
if (line.matches(LIST)) {
task.setCommand(CMD_LIST);
task.executeCommand(items, error, line);
System.out.println(border);
} else if (line.length() > FOUR && line.substring(0, FOUR).contains(DONE)) {
if (isInvalid(task, line, DONE)) {
task.setCommand(CMD_NOT_FOUND);
error.setErrorType(ERROR_IS_INVALID);
} else {
task.setCommand(CMD_DONE);
}
task.executeCommand(items, error, line);
} else if (line.length() >= FOUR && line.substring(0, FOUR).contains(TODO)) {
if (line.length() <= FIVE) {
error.setErrorType(ERROR_TODO_IS_EMPTY);
task.setCommand(CMD_NOT_FOUND);
} else {
task.setCommand(CMD_TODO);
}
task.executeCommand(items, error, line);
} else if (line.length() >= FIVE && line.substring(0, FIVE).contains(EVENT)) {
if (line.length() > SIX && line.contains(AT)) {
task.setCommand(CMD_EVENT);
} else {
error.setErrorType(ERROR_EVENT_IS_EMPTY);
task.setCommand(CMD_NOT_FOUND);
}
task.executeCommand(items, error, line);
} else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains(DEADLINE)) {
if (line.length() >= NINE && line.contains(BY)) {
task.setCommand(CMD_DEADLINE);
} else {
error.setErrorType(ERROR_DEADLINE_IS_EMPTY);
task.setCommand(CMD_NOT_FOUND);
}
task.executeCommand(items, error, line);
} else if (line.length() >= SIX && line.substring(0, SIX).contains(DELETE)) {
if (isInvalid(task, line,DELETE)) {
task.setCommand(CMD_NOT_FOUND);
error.setErrorType(ERROR_IS_INVALID);
} else {
task.setCommand(CMD_DELETE);
}
task.executeCommand(items, error, line);
} else if (!line.matches(BYE)) {
error.setErrorType(ERROR_COMMAND_NOT_FOUND);
task.setCommand(CMD_NOT_FOUND);
task.executeCommand(items, error, line);
}
} while (!line.matches(BYE));
return line;
}

public static void main(String[] args) throws IOException {
Scanner in = new Scanner(System.in);
CommandList task = new CommandList();
DukeException error = new DukeException();
String line;
createSave();
readSave(task);
printStartMessage();
line = readCommand(in, task, error);
updateSave();
task.setCommand(CMD_TERMINATE);
task.executeCommand(items, error, line);
}
}
59 changes: 59 additions & 0 deletions src/main/java/duke/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package duke;

public class DukeException {
protected int errorType;
private static final int ERROR_TODO_IS_EMPTY = 1;
private static final int ERROR_EVENT_IS_EMPTY = 2;
private static final int ERROR_DEADLINE_IS_EMPTY = 3;
private static final int ERROR_COMMAND_NOT_FOUND = 4;
private static final int ERROR_IS_INVALID = 5;

private static final String border = "____________________________________________________________\n";

public static void printEmptyDescriptionErrorMessage(String command) {
System.out.println(border);
System.out.println("\uD83D\uDE00 " + "OOPS!!! The description of a " + command + " cannot be empty.");
System.out.println(border);
}
public static void printCommandMismatchErrorMessage() {
System.out.println(border);
System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-(");
System.out.println(border);
}
public static void printCommandIsInvalid() {
System.out.println(border);
System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid.");
System.out.println(border);
}
public DukeException() {
this.errorType = ERROR_COMMAND_NOT_FOUND;
}
public void setErrorType(int type) {
this.errorType = type;
}
public int getErrorType() {
return errorType;
}
public void printError(int type) {
switch (type) {
case ERROR_TODO_IS_EMPTY:
printEmptyDescriptionErrorMessage("todo");
break;
case ERROR_EVENT_IS_EMPTY:
printEmptyDescriptionErrorMessage("event");
break;
case ERROR_DEADLINE_IS_EMPTY:
printEmptyDescriptionErrorMessage("deadline");
break;
case ERROR_IS_INVALID:
printCommandIsInvalid();
break;
default:
printCommandMismatchErrorMessage();
break;
}
}

}


Loading