Skip to content

Commit

Permalink
Updated level 5 & 6 features. Refactored out todo, events and deadline
Browse files Browse the repository at this point in the history
  • Loading branch information
joelczk committed Feb 12, 2020
1 parent efc452d commit 98f1864
Show file tree
Hide file tree
Showing 7 changed files with 206 additions and 185 deletions.
1 change: 1 addition & 0 deletions database.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[T],true,say hello,
184 changes: 14 additions & 170 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,182 +1,24 @@
import java.util.Scanner;
import java.util.ArrayList;

public class Duke {

private ArrayList <Task> tasks;
private ToDo newToDo;
private Event newEvent;
private Deadline newDeadline;
private UI userInterface;
private Parser parser;
private DukeExceptions dukeExceptions;
private TaskList tasks;
private Storage storage;
private final int startIndex = 0;
private final int lengthOfDeadline = 9;
private final int lengthOfEvent = 5;
private final int lengthOfDone = 5;
private final int lengthOfDelete = 7;

public Duke() {
this.tasks = new ArrayList<Task>();
userInterface = new UI();
parser = new Parser();
dukeExceptions = new DukeExceptions();
tasks = new TaskList();
storage = new Storage();
}

private void printList() {
int position = 1;
for (Task text: tasks) {
System.out.println(" " + position + ". " + text);
position ++;
}
}

private void list() {
tasks = storage.loadTasks();
if (tasks.size() == 0) {
dukeExceptions.printListExceptions();
} else {
System.out.println(" Here are the list of tasks:");
printList();
}
}

private int getIndex(String input, int position) {
String removeTrailingSpaces = input.trim();
String numericalIndex = removeTrailingSpaces.substring(position,input.length()).trim();
int index = Integer.valueOf(numericalIndex) - 1;
return index;
}

private void markAsDone(int index) {
Task completedTask = tasks.get(index);
boolean isCompleted = completedTask.getStatus();
if (isCompleted) {
dukeExceptions.printDoneExceptions();
} else {
completedTask.markAsDone();
tasks.set(index, completedTask);
System.out.println(" Duke has marked the following tasks as done:");
System.out.println(" " + completedTask);
storage.save(tasks);
}
}

private void getDoneExceptions(String input) {
try {
int index = getIndex(input, lengthOfDone);
markAsDone(index);
} catch(IndexOutOfBoundsException exception) {
dukeExceptions.printDoneIOBExceptions(input);
} catch (NumberFormatException exception) {
dukeExceptions.printDoneNFExceptions();
}
}

private void addToDo(String input) {
String removeTrailingSpaces = input.trim();
String description = removeTrailingSpaces.substring(5, input.length());
newToDo = new ToDo(description);
tasks.add(newToDo);
System.out.println(" Got it! I have added the following tasks: ");
System.out.println( newToDo);
System.out.println("There are currently " + tasks.size() + " task(s) queued");
storage.save(tasks);
}

private void getToDoExceptions(String input) {
try {
addToDo(input);
} catch (StringIndexOutOfBoundsException exception) {
dukeExceptions.toDoExceptions(input);
}
}

private void addEvent(String input) {
String description = input.substring(lengthOfEvent).trim();
int index = description.indexOf("/");
String details = description.substring(startIndex, index).trim();
String specifier = description.substring(index + 1, index + 3);
String time = description.substring(index + 3, description.length()).trim();
boolean isAt = specifier.toLowerCase().equals("at");
if (details.isEmpty() || time.isEmpty() || !isAt) {
String errorMessage = "Missing Event Description / Event Time / Missing parameters";
dukeExceptions.printEventExceptions(errorMessage, input);
} else {
newEvent = new Event(details, time);
tasks.add(newEvent);
System.out.println(" New task has been added:");
System.out.println(" " + newEvent);
System.out.println(" There are currently " + tasks.size() + " task(s) being queued");
storage.save(tasks);
}
}

private void getEventExceptions(String input) {
try {
addEvent(input);
} catch (IndexOutOfBoundsException exception) {
String errorMessage = "Missing Event Description / Event Time & Missing parameters";
dukeExceptions.printEventExceptions(errorMessage, input);
}
}

private void addDeadline(String input) {
String description = input.substring(lengthOfDeadline, input.length()).trim();
int index = description.indexOf("/");
String details = description.substring(startIndex, index);
String day = description.substring(index + 3, description.length()).trim();
String specifier = description.substring(index + 1, index + 3);
boolean isBy = specifier.toLowerCase().equals("by");
if (details.isEmpty() || day.isEmpty() || !isBy) {
String errorMessage = "Missing Event Description / Event Time / Missing parameters";
dukeExceptions.printDeadlineExceptions(errorMessage, input);
} else {
newDeadline = new Deadline(details, day);
tasks.add(newDeadline);
System.out.println(" Got it!I've added this task:");
System.out.println(" " + newDeadline);
System.out.println(" There are currently " + tasks.size() + " being queued");
storage.save(tasks);
}
}

private void getDeadlineExceptions(String input) {
try {
addDeadline(input);
} catch (IndexOutOfBoundsException exception) {
String errorMessage = "Missing Event Description / Event Time & Missing parameters";
dukeExceptions.printDeadlineExceptions(errorMessage, input);
}
}

private void deleteItems(int index) {
Task deletedTask = tasks.get(index);
int length = tasks.size() - 1;
tasks.remove(deletedTask);
System.out.println(" Got it! I've removed this task:");
System.out.println(" " + deletedTask);
System.out.println(" There are currently " + length + " task(s) being queued");
storage.save(tasks);
}

private void getDeleteExceptions(String input) {
try {
int index = getIndex(input, lengthOfDelete);
deleteItems(index);
} catch (NumberFormatException exceptions) {
dukeExceptions.printNFEDeleteExceptions(input);
} catch (IndexOutOfBoundsException exception) {
dukeExceptions.printIOBDeleteExceptions(input);
}
}

private void run() {
Scanner sc = new Scanner(System.in);
userInterface.printGreetingMessage();
storage.restoreArray(sc, tasks);
tasks.list();
userInterface.printLine();
while (true) {
String input = sc.nextLine().trim();
parser.updateInput(input);
Expand All @@ -186,25 +28,27 @@ private void run() {
userInterface.printLine();
break;
} else if (parser.isList()) {
list();
tasks.list();
} else if (parser.isDone()) {
getDoneExceptions (input);
tasks.getDoneExceptions(input);
} else if (parser.isToDo()) {
getToDoExceptions (input);
tasks.getToDoExceptions (input);
} else if (parser.isEvent()) {
getEventExceptions(input);
tasks.getEventExceptions(input);
} else if (parser.isDeadline()) {
getDeadlineExceptions(input);
} else if (parser.isDelete()) {
getDeleteExceptions(input);
tasks.getDeadlineExceptions(input);
} else if(parser.isDelete()) {
tasks.getDeleteExceptions(input);
} else if (parser.isSave()) {
System.out.println("SAVING...");
} else {
System.out.println(" Duke does not understand your command! Can you repeat again?");
}
userInterface.printLine();
}
sc.close();
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
Expand Down
22 changes: 11 additions & 11 deletions src/main/java/DukeExceptions.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
public class DukeExceptions {
protected String input;

public DukeExceptions() {
this.input = null;
}
Expand All @@ -12,25 +13,25 @@ public void printDoneExceptions() {
System.out.println(" [Warning: The task(s) has already been marked as done]");
}

public void printDoneIOBExceptions(String input) {
public void printIOBDoneExceptions(String input) {
System.out.println(" Command Entered: " + input);
System.out.println(" [Error: Index is missing / out of bounds]");
System.out.println(" done : check the task as completed");
System.out.println(" Parameters: done [index] ");
System.out.println(" [Error: Specifier entered is out of range]");
System.out.println(" done: marks task to be completed");
System.out.println(" Parameters: done [TASK NUMBER]");
System.out.println(" Example: done 1");
}

public void printDoneNFExceptions() {
public void printNFEDoneExceptions(String input) {
System.out.println(" Command Entered: " + input);
System.out.println(" [Error: Index is missing / out of bounds]");
System.out.println(" done : check the task as completed");
System.out.println(" Parameters: done [index] ");
System.out.println(" [Error: Specifier entered is not a numerical value]");
System.out.println(" done: marks task to be completed");
System.out.println(" Parameters: done [Specifier]");
System.out.println(" Example: done 1");
}

public void toDoExceptions(String input) {
public void printToDoExceptions(String input) {
System.out.println(" Command Entered: " + input);
System.out.println(" [Error: Event Description is missing]");
System.out.println(" [Error: Missing specifier(s)]");
System.out.println(" todo: Adds todo items");
System.out.println(" Parameters: todo [description]");
System.out.println(" Example: todo read book");
Expand Down Expand Up @@ -69,5 +70,4 @@ public void printNFEDeleteExceptions(String input) {
System.out.println(" Parameters: delete [TASK NUMBER]");
System.out.println(" Example: delete 1");
}

}
6 changes: 5 additions & 1 deletion src/main/java/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,5 +54,9 @@ public boolean isDelete() {
String firstWord = getFirstWord();
return firstWord.equals("delete");
}


public boolean isSave() {
String firstWord = getFirstWord();
return firstWord.equals("save");
}
}
6 changes: 4 additions & 2 deletions src/main/java/Storage.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@
import java.io.IOException;
import java.util.ArrayList;
import java.io.FileNotFoundException;
import java.util.Scanner;
import java.util.Scanner;

public class Storage{
protected File file;
protected String filePath;
protected ArrayList <Task> tasks;

public Storage() {
file = new File("store.txt");
file = new File("database.txt");
filePath = file.getAbsolutePath();
}

Expand Down Expand Up @@ -61,6 +61,8 @@ public ArrayList <Task> loadTasks() {
fileReader.close();
} catch (FileNotFoundException exception) {
System.out.println("file not found");
} catch (Exception exception) {
System.out.println("File not found");
}
return tasksList;
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/Task.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public Task (String description, boolean isDone) {
this.description = description;
this.isDone = isDone;
}

public boolean getStatus() {
return isDone;
}
Expand Down
Loading

0 comments on commit 98f1864

Please sign in to comment.