Skip to content

Commit

Permalink
Level 4. ToDos, Events, Deadlines
Browse files Browse the repository at this point in the history
  • Loading branch information
Impala36 committed Aug 29, 2020
1 parent 2c84b2a commit bbe87d5
Show file tree
Hide file tree
Showing 7 changed files with 147 additions and 14 deletions.
8 changes: 8 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Deadline extends Todo{
private String[] split;

Deadline(String input) {
super(input);
split = input.split("/by");
}
}
3 changes: 1 addition & 2 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import java.lang.reflect.Array;
import java.util.*;
import java.util.Scanner;

/**
* A Personal Assistant Chatbot that helps a person to keep track of various things.
Expand Down
8 changes: 8 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class Event extends Todo{
private String[] split;

Event(String input) {
super(input);
split = input.split("/at");
}
}
18 changes: 15 additions & 3 deletions src/main/java/Parser.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class Parser {
class Parser {
/**
* @param line The entire user input.
*/
Expand All @@ -25,16 +25,28 @@ boolean parseInput(String line, UI ui, TaskList tasks) {
tasks.changeDone(line, ui, tasks);
return true;
}
case "todo" -> {
tasks.createTodo(line, ui, tasks);
return true;
}
case "deadline" -> {
tasks.createDeadline(line, ui, tasks);
return true;
}
case "event" -> {
tasks.createEvent(line, ui, tasks);
return true;
}
// also exit when user input is empty
case "bye", "" -> {
ui.farewell();
return false;
}
default -> {
tasks.add(new Task(line));
System.out.println(line);
ui.invalidCommand();
return true;
}
}
}

}
79 changes: 74 additions & 5 deletions src/main/java/TaskList.java
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import java.util.ArrayList;

/**
* Responsible for keeping the in-memory task list. This class will use an ArrayList inside it.
* Responsible for managing all the tasks in the list. This class will use an ArrayList inside it.
*/
class TaskList {
private ArrayList<Task> tasks;
Expand Down Expand Up @@ -46,12 +46,81 @@ private void markAsDone(String line, UI ui, TaskList tasks) {
int index = Integer.parseInt(line);
tasks.get(index - 1).setDone(true);
ui.markedAsDone(index, tasks);
} else System.out.println(ui.colorRed("There are no tasks to mark as done."));
} else ui.printError("There are no tasks to mark as done.");
} catch (NumberFormatException e) {
System.out.println(ui.colorRed("Please type a number for the index."));
ui.printError("Please type a number for the index.");
} catch (IndexOutOfBoundsException e) {
System.out.println(ui.colorRed("Please type an index number from 1 to "
+ tasks.size() + "."));
ui.printError("Please type an index number from 1 to "
+ tasks.size() + ".");
}
}

void createTodo(String line, UI ui, TaskList tasks) {
try {
if (line.substring("todo".length()).trim().isEmpty()) {
throw new DukeException
(ui.colorRed("Please type in the 'todo <something>' format."));
} else if (line.contains("todo")) {
line = line.replaceFirst("todo", "[Todo] ");
tasks.add(new Todo(line));
ui.taskAdded(line, tasks);
}
} catch (DukeException e) {
ui.printError(e.getMessage());
}
}

void createDeadline(String line, UI ui, TaskList tasks) {
try {
if (line.substring("deadline".length()).trim().isEmpty() || !line.contains("/by")) {
throw new DukeException
(ui.colorRed("Please type in the 'deadline <something> /by <when>' format."));
} else if (line.contains("deadline")) {
String[] split = line.split("/by");
if (split[0].isEmpty() || split[1].isEmpty()) {
throw new DukeException
(ui.colorRed("Please type in the 'deadline " +
"<something> /by <when>' format."));
} else if (line.contains("deadline")) {
line = line.replaceFirst("deadline", "[Deadline]")
.replaceFirst("/by", "(by:")
.concat(")");
tasks.add(new Deadline(line));
ui.taskAdded(line, tasks);
}
}
} catch (DukeException e) {
ui.printError(e.getMessage());
} catch (IndexOutOfBoundsException e) {
ui.printError("Please type in something for <when>" +
" after 'deadline <something> /by'.");
}
}

void createEvent(String line, UI ui, TaskList tasks) {
try {
if (line.substring("event".length()).trim().isEmpty() || !line.contains("/at")) {
throw new DukeException
(ui.colorRed("Please type in the 'event <something> /at <when>' format."));
} else if (line.contains("event")) {
String[] split = line.split("/at");
if (split[0].isEmpty() || split[1].isEmpty()) {
throw new DukeException
(ui.colorRed("Please type in the 'event " +
"<something> /at <when>' format."));
} else if (line.contains("event")) {
line = line.replaceFirst("event", "[Event] ")
.replaceFirst("/at", "(at:")
.concat(")");
tasks.add(new Event(line));
ui.taskAdded(line, tasks);
}
}
} catch (DukeException e) {
ui.printError(e.getMessage());
} catch (IndexOutOfBoundsException e) {
ui.printError("Please type in something for <when>" +
" after 'deadline <something> /by'.");
}
}
}
11 changes: 11 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
class Todo extends Task{

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

Todo(String input, boolean isDone) {
super(input);
setDone(isDone);
}
}
34 changes: 30 additions & 4 deletions src/main/java/UI.java
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
public class UI {
class UI {

void greet() {
String greeting = "____________________________________________________________\n"
Expand All @@ -7,19 +7,45 @@ void greet() {
+ "____________________________________________________________\n";
System.out.println(greeting);
}

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

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

void showTotal(TaskList tasks) {

String total = "Tasks in the list: " + tasks.size()
+ "\n____________________________________________________________\n";
System.out.println(total);
}

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

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

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

public void taskAdded(String line, TaskList tasks) {
String added = "____________________________________________________________\n"
+ "Great! You have entered a proper task as shown below:\n"
+ line;
System.out.println(added);
showTotal(tasks);
}
}

0 comments on commit bbe87d5

Please sign in to comment.