Skip to content
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

IP #355

Open
wants to merge 46 commits into
base: master
Choose a base branch
from
Open

IP #355

Changes from 1 commit
Commits
Show all changes
46 commits
Select commit Hold shift + click to select a range
2ca6b28
JavaFX tutorial: Support cross-platform JARs
j-lum Sep 20, 2019
6f2ddfa
Merge pull request #35 from j-lum/xplatformjfx
j-lum Feb 3, 2020
2697ae1
Remove unnecessary override annotation
jiachen247 Feb 11, 2020
1416279
Merge pull request #41 from jiachen247/patch-2
j-lum Feb 12, 2020
71d7ea8
Remove generic tutorials
May 24, 2020
ef3c8cd
Add text-ui-test files
May 24, 2020
87eb8b8
README.md: Fix typo of => or
damithc Jul 3, 2020
569941e
add greet functionality
abnermtj Aug 19, 2020
58b01ea
add echo functionality
abnermtj Aug 26, 2020
d230588
add listing
abnermtj Aug 26, 2020
9001c6d
add finishing tasks
abnermtj Aug 26, 2020
c5684ba
change code variable names
abnermtj Aug 26, 2020
1d08529
add new Task subclasses ToDos, Events, Deadlines
abnermtj Sep 1, 2020
7d5a549
add basic testing
abnermtj Sep 1, 2020
b81f997
reduce linecount on verbose functions
abnermtj Sep 1, 2020
54a7a82
Fix Coding Standard violations
abnermtj Sep 2, 2020
3694276
fix formatting issues
abnermtj Sep 2, 2020
7857322
refactor duke.java
abnermtj Sep 2, 2020
6ec71a5
update gitignore
abnermtj Sep 8, 2020
86b9bdb
add DukeExceptions and handling
abnermtj Sep 8, 2020
bf94a80
Merge branch 'branch-Level-5'
abnermtj Sep 8, 2020
6a05692
created packages and updated testing
abnermtj Sep 8, 2020
b349939
Merge branch 'branch-A-Packages'
abnermtj Sep 8, 2020
377ef17
add delete function
abnermtj Sep 13, 2020
532842e
add save function
abnermtj Sep 13, 2020
4e0facb
Merge branch 'branch-Level-6'
abnermtj Sep 14, 2020
9548884
Merge branch 'branch-Level-7'
abnermtj Sep 14, 2020
417a070
refactor code
abnermtj Sep 16, 2020
c7ca30f
refactor Exception messages
abnermtj Sep 23, 2020
cf60ebf
add comments
abnermtj Sep 23, 2020
615c04b
Refactor to more OOP style
abnermtj Sep 24, 2020
49fe2bc
Add date time recognizing
abnermtj Sep 25, 2020
69c66ec
add find method
abnermtj Sep 25, 2020
925d447
add javadocs
abnermtj Sep 25, 2020
43849b1
Merge pull request #2 from abnermtj/branch-Level-8
abnermtj Sep 25, 2020
5cde6a5
resolve merge conflict
abnermtj Sep 25, 2020
8db68f8
Mer branch 'master' of github.com:abnermtj/ip
abnermtj Sep 25, 2020
12e8473
Merge pull request #4 from abnermtj/branch-A-JavaDoc
abnermtj Sep 25, 2020
2db9678
Merge branch 'master' of github.com:abnermtj/ip
abnermtj Sep 25, 2020
fd5279f
Merge branch 'master' into branch-Level-9
abnermtj Sep 25, 2020
4e43dd0
Merge pull request #3 from abnermtj/branch-Level-9
abnermtj Sep 25, 2020
e889556
fix merging mistakes
abnermtj Sep 25, 2020
f7df174
code refactoring
abnermtj Sep 25, 2020
c093875
add userguide
abnermtj Sep 26, 2020
e151110
Update README.md
abnermtj Sep 26, 2020
41b32f3
Update README.md
abnermtj Sep 26, 2020
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
Prev Previous commit
Next Next commit
add DukeExceptions and handling
abnermtj committed Sep 8, 2020
commit 86b9bdb7aee8144167e65808b35940513932c65d
160 changes: 77 additions & 83 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
import java.util.Scanner;

public class Duke {
public enum CommandStatus{ SUCCESS, FAIL }

public static final int NUM_ARGS_DEADLINE = 2;
public static final int NUM_ARGS_EVENT = 2;

@@ -15,47 +13,44 @@ public static void main(String[] args) {
printHomeScreen();

while(programIsRunning){
String[] input = new String[2];
String[] input;
input = getInputSeparateCommandAndDescription();

String command = input[0];
String description = "";
if (input.length == 2)
if(input.length == 2)
description = input[1];

CommandStatus commandResult = CommandStatus.FAIL;

switch(command) {
case "bye" :
commandResult = handleByeCommand();
break;
case "list" :
commandResult = handleListCommand();
break;
case "done" :
commandResult = handleDoneCommand(command, description);
break;
case "todo" : // Fallthrough
case "deadline" : // Fallthrough
case "event" :
commandResult = handleAddCommand(command, description);
break;
default :
System.out.println("I don't understand that command");
break;
}

if (commandResult == CommandStatus.FAIL){
System.out.println("Command Failed");
try {
switch(command) {
case "bye":
handleByeCommand();
break;
case "list":
handleListCommand();
break;
case "done":
handleDoneCommand(description);
break;
case "todo": // Fallthrough
case "deadline": // Fallthrough
case "event":
handleAddCommand(command, description);
break;
default:
throw new DukeException("Duke does not understand that command");
}
} catch(DukeException exception) {
handleException(exception);
}
}
printExitScreen();
}

public static void printHomeScreen(){
printLineSeparator();
System.out.println("Hello! I'm Duke");
System.out.println("What can I do for you?");
System.out.println("Hello! I'm Duke" + System.lineSeparator() +
"What can I do for you?");
printLineSeparator();
}

@@ -68,69 +63,78 @@ public static void printLineSeparator(){
System.out.println("---------------------------------------");
}

public static CommandStatus handleByeCommand(){
public static void printSuccessfulAddEntry(Task newTask) {
printLineSeparator();

System.out.print("Got it. I've added this task:" + System.lineSeparator() +
"\t" + newTask + System.lineSeparator()
+"Now you have " + tasksListIndex + " task");

if(tasksListIndex > 1) {
System.out.print("s");
}

System.out.println(" in the list.");

printLineSeparator();
}

public static void handleByeCommand(){
programIsRunning = false;
return CommandStatus.SUCCESS;
}

public static String[] getInputSeparateCommandAndDescription(){
String input = scanner.nextLine();
input = input.trim();

String[] commands = new String[2];
String[] commands;
commands = input.split(" ", 2);

return commands;
}

public static CommandStatus handleListCommand() {
public static void handleListCommand() {
int entryNum = 1;

printLineSeparator();

for (Task task : tasksList) {
if (task == null) {
for(Task task : tasksList) {
if(task == null) {
break;
} else {
System.out.print(String.valueOf(entryNum) + ".");
System.out.println(task);
System.out.println(entryNum + "." + task);
entryNum++;
}
}

printLineSeparator();

return CommandStatus.SUCCESS;
}


public static CommandStatus handleDoneCommand(String command, String description) {
if (description == null){
return CommandStatus.FAIL;
public static void handleDoneCommand(String description) throws DukeException{
if(description.equals("")){
throw new DukeException("The done command format is: done <taskDescription>");
}
// Looks for a task with a matching description task to mark as done
for (Task task : tasksList) {
if (task == null) {
return CommandStatus.FAIL;
} else if (task.description.equals(description) && task.isDone == false) {
task.isDone = true;

// Looks for a task with matching the description task and marks it as done
for(Task task : tasksList) {
if(task == null) {
return;
} else if(task.description.equals(description) && !task.isDone ) {
task.isDone = true;

System.out.print("Nice! I've marked this task as done:\n\t");
System.out.println(task);
System.out.println("Nice! I've marked this task as done:" + System.lineSeparator() +
"\t" + task);
printLineSeparator();

return CommandStatus.SUCCESS;
return;
}
}

return CommandStatus.FAIL;
}

public static CommandStatus handleAddCommand(String command, String description) {
public static void handleAddCommand(String command, String description) throws DukeException {
Task newTask = null;

switch (command){
switch(command){
case "todo" :
newTask = createToDo(description);
break;
@@ -144,53 +148,43 @@ public static CommandStatus handleAddCommand(String command, String description)
break;
}

if (newTask == null){
return CommandStatus.FAIL;
if(newTask == null){
throw new DukeException("Unable to create the new Task");
}

tasksList[tasksListIndex] = newTask;
tasksListIndex++;

printSuccessfulAddEntry(newTask);

return CommandStatus.SUCCESS;
}

public static Todo createToDo(String description){
public static void handleException(DukeException exception){
System.out.println("Exception!" + System.lineSeparator() +
"\t" + exception.description);
}
public static Todo createToDo(String description) throws DukeException{
if (description.equals("")){
throw new DukeException("The todo format is: todo <desc>");
}
return new Todo(description);
}

public static Deadlines createDeadline(String description){
public static Deadlines createDeadline(String description) throws DukeException{
String[] taskDetails = description.split(" /by ");
if (taskDetails.length < NUM_ARGS_DEADLINE){
return null;
if(taskDetails.length < NUM_ARGS_DEADLINE){
throw new DukeException("The deadline format is: deadline <desc> \\by <time>");
}

return new Deadlines(taskDetails[0], taskDetails[1]);
}

public static Event createEvent(String description){
public static Event createEvent(String description) throws DukeException{
String[] taskDetails = description.split(" /at ");
if (taskDetails.length < NUM_ARGS_DEADLINE){
return null;
if(taskDetails.length < NUM_ARGS_EVENT){
throw new DukeException("The event format is: event <desc> \\at <time>");
}

return new Event(taskDetails[0], taskDetails[1]);
}

public static void printSuccessfulAddEntry(Task newTask) {
printLineSeparator();

System.out.print("Got it. I've added this task:" + System.lineSeparator() +
"\t" + newTask + System.lineSeparator()
+"Now you have " + tasksListIndex + " task");

if (tasksListIndex > 1) {
System.out.print("s");
}

System.out.println(" in the list.");

printLineSeparator();
}
}
7 changes: 7 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
public class DukeException extends Exception{
String description;

public DukeException (String description){
this.description = description;
}
}
7 changes: 4 additions & 3 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
@@ -2,8 +2,8 @@
Hello! I'm Duke
What can I do for you?
---------------------------------------
I don't understand that command
Command Failed
Exception!
Duke does not understand that command
---------------------------------------
---------------------------------------
---------------------------------------
@@ -40,7 +40,8 @@ Nice! I've marked this task as done:
2.[E][✘] 123 (at: 12pm)
3.[D][✓] study (by: Sleeptime)
---------------------------------------
Command Failed
Exception!
The done command format is: done <taskDescription>
---------------------------------------
1.[T][✓] 123
2.[E][✘] 123 (at: 12pm)