Skip to content

Commit

Permalink
Level 5. Handle Errors
Browse files Browse the repository at this point in the history
A-AbstractClasses
A-Packages
  • Loading branch information
Impala36 committed Aug 29, 2020
1 parent bbe87d5 commit 3005764
Show file tree
Hide file tree
Showing 12 changed files with 71 additions and 40 deletions.
11 changes: 0 additions & 11 deletions src/main/java/Todo.java

This file was deleted.

8 changes: 7 additions & 1 deletion src/main/java/Duke.java → src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
package duke;

import duke.ui.UI;
import duke.parser.Parser;
import duke.task.TaskList;

import java.util.Scanner;

/**
* A Personal Assistant Chatbot that helps a person to keep track of various things.
*
* @author Wang Zhenquan
* @version Level 3
* @version Level 5
* @since 20/08/2020
*/

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
package duke.exception;
/**
* Inherits the Exception class. <p>
* Overrides the constructor that takes a String parameter with custom error information
Expand All @@ -7,7 +8,7 @@
* Catch that exception somewhere else and print the message inside the exception object.
*/
public class DukeException extends Exception {
DukeException(String input) {
public DukeException(String input) {
super(input);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
class Parser {
package duke.parser;

import duke.task.TaskList;
import duke.ui.UI;

public class Parser {
/**
* @param line The entire user input.
*/
Expand All @@ -7,11 +12,11 @@ String getCommandWord(String line) {
}

/**
* @param line The entire user input.
* @param ui The UI created in the Duke class.
* @param tasks The TaskList created in the Duke class.
* @param line The entire user input.
* @param ui The UI created in the Duke class.
* @param tasks The TaskList created in the Duke class.
*/
boolean parseInput(String line, UI ui, TaskList tasks) {
public boolean parseInput(String line, UI ui, TaskList tasks) {

String command = getCommandWord(line);
switch (command) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Deadline extends Todo{
package duke.task;

class Deadline extends Todo {
private String[] split;

Deadline(String input) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class Event extends Todo{
package duke.task;

class Event extends Todo {
private String[] split;

Event(String input) {
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/Task.java → src/main/java/duke/task/Task.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
package duke.task;

/**
* A class for tasks.
* An abstract class that is inherited by the Todo, Deadline and Event classes.
*/
class Task {

public abstract class Task {
String task;
private boolean isDone;

Expand All @@ -20,7 +23,8 @@ boolean getDone() {
String getTaskStatus(Boolean isDone) {
return "[" + (isDone ? "\u2713" : "\u2718") + "] "; //return tick or X symbols
}
String getDescription() {

public String getDescription() {
return getTaskStatus(isDone) + task;
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,37 @@
package duke.task;

import duke.exception.DukeException;
import duke.ui.UI;

import java.util.ArrayList;

/**
* Responsible for managing all the tasks in the list. This class will use an ArrayList inside it.
*/
class TaskList {
public class TaskList {
private ArrayList<Task> tasks;

TaskList(ArrayList<Task> tasks) {
this.tasks = tasks;
}

TaskList() {
public TaskList() {
tasks = new ArrayList<>();
}

int size() {
public int size() {
return tasks.size();
}

void add(Task task) {
tasks.add(task);
}

Task get(int index) {
public Task get(int index) {
return tasks.get(index);
}

void changeDone(String line, UI ui, TaskList tasks) {
public void changeDone(String line, UI ui, TaskList tasks) {
line = line.substring("done".length()).trim();
try {
if (!line.isEmpty()) {
Expand Down Expand Up @@ -55,7 +60,7 @@ private void markAsDone(String line, UI ui, TaskList tasks) {
}
}

void createTodo(String line, UI ui, TaskList tasks) {
public void createTodo(String line, UI ui, TaskList tasks) {
try {
if (line.substring("todo".length()).trim().isEmpty()) {
throw new DukeException
Expand All @@ -70,7 +75,7 @@ void createTodo(String line, UI ui, TaskList tasks) {
}
}

void createDeadline(String line, UI ui, TaskList tasks) {
public void createDeadline(String line, UI ui, TaskList tasks) {
try {
if (line.substring("deadline".length()).trim().isEmpty() || !line.contains("/by")) {
throw new DukeException
Expand All @@ -97,7 +102,7 @@ void createDeadline(String line, UI ui, TaskList tasks) {
}
}

void createEvent(String line, UI ui, TaskList tasks) {
public void createEvent(String line, UI ui, TaskList tasks) {
try {
if (line.substring("event".length()).trim().isEmpty() || !line.contains("/at")) {
throw new DukeException
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/duke/task/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package duke.task;

class Todo extends Task {

Todo(String input) {
super(input);
}

}
18 changes: 11 additions & 7 deletions src/main/java/UI.java → src/main/java/duke/ui/UI.java
Original file line number Diff line number Diff line change
@@ -1,20 +1,24 @@
class UI {
package duke.ui;

void greet() {
import duke.task.TaskList;

public class UI {

public void greet() {
String greeting = "____________________________________________________________\n"
+ "Hello! I'm Duke\n"
+ "What can I do for you?\n"
+ "____________________________________________________________\n";
System.out.println(greeting);
}

void farewell() {
public void farewell() {
String farewell = "Bye. Hope to see you again soon!\n"
+ "____________________________________________________________\n";
System.out.println(farewell);
}

void markedAsDone(int index, TaskList tasks) {
public void markedAsDone(int index, TaskList tasks) {
String done = "____________________________________________________________\n"
+ "\033[33mTask [" + index + "] "
+ tasks.get(index - 1).getDescription() + " has been set as completed.\033[0m"
Expand All @@ -29,15 +33,15 @@ void showTotal(TaskList tasks) {
System.out.println(total);
}

void printError(String s) {
public void printError(String s) {
System.out.println(colorRed(s));
}

String colorRed(String input) {
public String colorRed(String input) {
return "\033[31m" + input + "\033[0m";
}

void invalidCommand() {
public void invalidCommand() {
printError("Please enter a valid command.");
}

Expand Down
4 changes: 4 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ Hello from
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

____________________________________________________________
Hello! I'm Duke
What can I do for you?
____________________________________________________________
4 changes: 2 additions & 2 deletions text-ui-test/runtest.bat
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ REM create bin directory if it doesn't exist
if not exist ..\bin mkdir ..\bin

REM delete output from previous run
del ACTUAL.TXT
//del ACTUAL.TXT

REM compile the code into the bin folder
javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\Duke.java
javac -cp ..\src -Xlint:none -d ..\bin ..\src\main\java\duke\Duke.java
IF ERRORLEVEL 1 (
echo ********** BUILD FAILURE **********
exit /b 1
Expand Down

0 comments on commit 3005764

Please sign in to comment.