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

Branch level 5 #115

Open
wants to merge 19 commits into
base: master
Choose a base branch
from
Open
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

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

public class Deadline extends Task {
protected String by;

public Deadline(String description, String by) {
super(description);
this.by = by;
}

@Override
public String toString() {
return "[D]" + super.toString() + " (by: " + by + ")";
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/DeadlineException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class DeadlineException extends Exception {
}
195 changes: 195 additions & 0 deletions src/main/java/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
import java.util.Scanner;

public class Duke {
static int noOfTasks = 0;
static String sectionDivider = "____________________________________________________________";
public static boolean invalidInput = false;

public static void main(String[] args) {
Task[] TaskArray = new Task[100];
Task newItem = null;
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println(sectionDivider);
System.out.println("Hello! I'm Duke");
System.out.println("Please type tasks to do OR (list) to list all the tasks OR (bye) to exit.");
Scanner sc= new Scanner(System.in);
String userInput;

userInput = getUserInput(sc);

// Clean text input, handle errors
while (invalidInput) {
userInput = getUserInput(sc);
}

while (true) {
if (userInput.equals("bye")) {
System.out.println(sectionDivider);
System.out.println("Bye. Hope to see you again soon!");
System.out.println(sectionDivider);
break;
}
if (userInput.equals("list")){
System.out.println(sectionDivider);
System.out.println("Here are the tasks in your to-do list:");
for(int i= 1; i!=noOfTasks+1; i++){
System.out.println(i + "." + TaskArray[i-1].toString());
}
System.out.println(sectionDivider);
}
if (userInput.contains("done")){
System.out.println(sectionDivider);
String[] splitInput = userInput.split(" ");
int taskNumber = Integer.parseInt(splitInput[1]);
TaskArray[taskNumber-1].markAsDone();
System.out.println("Nice! I've marked this task as done: ");
System.out.println(taskNumber + ". [" + TaskArray[taskNumber-1].getStatusIcon() + " ]" + TaskArray[taskNumber-1].getDescription());
System.out.println(sectionDivider);
} else {
System.out.println(sectionDivider);
String[] splitInput = userInput.split(" ");
if (splitInput[0].equals("deadline")) {
int wordsCount = splitInput.length;
String descriptionString = "";
String byString = "";
int byIndex = 0;
for (int i = 1; i != wordsCount; i++) {
if (splitInput[i].equals("/by")) {
byIndex = i + 1;
break;
} else {
descriptionString = descriptionString + splitInput[i] + " ";
}
}
while (byIndex != wordsCount) {
byString = byString + splitInput[byIndex] + " ";
byIndex++;
}
newItem = new Deadline(descriptionString, byString);
TaskArray[noOfTasks] = newItem;
noOfTasks = noOfTasks + 1;
System.out.println("Got it. I've added this task:");
System.out.println(TaskArray[noOfTasks - 1].toString());
System.out.println("Now you have " + noOfTasks + " tasks in the list.");
System.out.println(sectionDivider);
}
if (splitInput[0].equals("event")) {
int wordsCount = splitInput.length;
String descriptionString = "";
String atString = "";
int atIndex = 0;
for (int i = 1; i != wordsCount; i++) {
if (splitInput[i].equals("/at")) {
atIndex = i + 1;
break;
} else {
descriptionString = descriptionString + splitInput[i] + " ";
}
}
while (atIndex != wordsCount) {
atString = atString + splitInput[atIndex] + " ";
atIndex++;
}
newItem = new Event(descriptionString, atString);
TaskArray[noOfTasks] = newItem;
noOfTasks = noOfTasks + 1;
System.out.println("Got it. I've added this task:");
System.out.println(TaskArray[noOfTasks - 1].toString());
System.out.println("Now you have " + noOfTasks + " tasks in the list.");
System.out.println(sectionDivider);
}
if (splitInput[0].equals("todo")) {
int wordsCount = splitInput.length;
String descriptionString = "";
for (int i = 1; i != wordsCount; i++) {
descriptionString = descriptionString + splitInput[i] + " ";
}
newItem = new Todo(descriptionString);
TaskArray[noOfTasks] = newItem;
noOfTasks = noOfTasks + 1;
System.out.println("Got it. I've added this task:");
System.out.println(TaskArray[noOfTasks - 1].toString());
System.out.println("Now you have " + noOfTasks + " tasks in the list.");
System.out.println(sectionDivider);
}
}
}
}
public static String getUserInput(Scanner sc) {
String userInput;
userInput = sc.nextLine();
try {
handleErrorUserInputs(userInput);
} catch (TaskException e) {
invalidInput = true;
System.out.println("☹ OOPS!!! The description of a todo cannot be empty.");
System.out.println(sectionDivider);
} catch (EventException e) {
invalidInput = true;
System.out.println("☹ OOPS!!! The description of an event cannot be empty or is incomplete (/at).");
System.out.println(sectionDivider);
} catch (DeadlineException e) {
invalidInput = true;
System.out.println("☹ OOPS!!! The description of a deadline cannot be empty or is incomplete (/by).");
System.out.println(sectionDivider);
}
return userInput;
}
public static void handleErrorUserInputs(String userInput) throws TaskException, EventException, DeadlineException {
userInput = userInput.toLowerCase().trim();

// check if todo description is empty
if (userInput.contains("todo") && !(userInput.contains("event")) && !(userInput.contains("deadline"))) {
validateTodo(userInput);
// check if event description is empty
} else if (userInput.contains("event") && !(userInput.contains("todo")) && !(userInput.contains("deadline"))) {
validateEvent(userInput);
// check if deadline description is empty
} else if (userInput.contains("deadline") && !(userInput.contains("todo")) && !(userInput.contains("event"))) {
validateDeadline(userInput);
// check if keyword were used in userInput
} else if (userInput.contains("list")
|| userInput.contains("bye")
|| userInput.contains("done")
|| userInput.contains("delete")) {
invalidInput = false;
// incorrect input
} else {
invalidInput = true;
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
System.out.println(sectionDivider);
}
}
public static void validateDeadline(String userInput) throws DeadlineException {
if (userInput.substring(9 - 1).trim().isEmpty()
|| !(userInput.contains("/by"))
|| userInput.substring(userInput.indexOf("/by") + 3).trim().isEmpty() ) {
throw new DeadlineException();
} else {
invalidInput = false;
}
}

public static void validateEvent(String userInput) throws EventException {
if (userInput.substring(6 - 1).trim().isEmpty()
|| !(userInput.contains("/at"))
|| userInput.substring(userInput.indexOf("/at") + 3).trim().isEmpty()) {
throw new EventException();
} else {
invalidInput = false;
}
}

public static void validateTodo(String userInput) throws TaskException {
if (userInput.substring(5 - 1).trim().isEmpty()) {
throw new TaskException();
} else {
invalidInput = false;
}
}
}
15 changes: 15 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package duke;

public class Event extends Task{
protected String at;

public Event(String description, String at) {
super(description);
this.at = at;
}

@Override
public String toString() {
return "[E]" + super.toString() + " (at: " + at + ")";
}
}
4 changes: 4 additions & 0 deletions src/main/java/duke/EventException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
package duke;

public class EventException extends Exception{
}
28 changes: 28 additions & 0 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
package duke;

public class Task {
protected String description;
protected boolean isDone;

public Task(String description) {
this.description = description;
this.isDone = false;
}

public String getDescription() {
return (this.description);
}

public String getStatusIcon() {
return (isDone ? "X" : " "); // mark done task with X
}

public void markAsDone(){
this.isDone = true;
}

public String toString(){
return "[" + getStatusIcon() + "] " + this.description;
}

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

public class TaskException extends Exception {
}
19 changes: 19 additions & 0 deletions src/main/java/duke/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke;

public class Todo extends Task{
public Todo(String description) {
super(description);
}

@Override
public String toString() {
return "[T]" + super.toString();
}
public boolean isDescriptionEmpty(String s){
if (s.isEmpty()){
System.out.println("OOPS!!! The description of a todo cannot be empty.");
return true;
}
return false;
}
}
57 changes: 57 additions & 0 deletions text-ui-test/EXPECTED.TXT
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,60 @@ Hello from
| |_| | |_| | < __/
|____/ \__,_|_|\_\___|

____________________________________________________________
Hello! I'm Duke
Please type tasks to do OR (list) to list all the tasks OR (bye) to exit.
____________________________________________________________
Got it. I've added this task:
[T][ ] laundry
Now you have 1 tasks in the list.
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[T][ ] borrow book
Now you have 2 tasks in the list.
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[D][ ] return book (by: Sunday )
Now you have 3 tasks in the list.
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[E][ ] project meeting (at: Mon 2-4pm )
Now you have 4 tasks in the list.
____________________________________________________________
____________________________________________________________
Got it. I've added this task:
[D][ ] do homework (by: no idea :-p )
Now you have 5 tasks in the list.
____________________________________________________________
____________________________________________________________
Here are the tasks in your to-do list:
1.[T][ ] laundry
2.[T][ ] borrow book
3.[D][ ] return book (by: Sunday )
4.[E][ ] project meeting (at: Mon 2-4pm )
5.[D][ ] do homework (by: no idea :-p )
____________________________________________________________
____________________________________________________________
____________________________________________________________
Nice! I've marked this task as done:
2. [X ]borrow book
____________________________________________________________
____________________________________________________________
Nice! I've marked this task as done:
1. [X ]laundry
____________________________________________________________
____________________________________________________________
Here are the tasks in your to-do list:
1.[T][X] laundry
2.[T][X] borrow book
3.[D][ ] return book (by: Sunday )
4.[E][ ] project meeting (at: Mon 2-4pm )
5.[D][ ] do homework (by: no idea :-p )
____________________________________________________________
____________________________________________________________
____________________________________________________________
Bye. Hope to see you again soon!
____________________________________________________________
10 changes: 10 additions & 0 deletions text-ui-test/input.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
todo laundry
todo borrow book
deadline return book /by Sunday
event project meeting /at Mon 2-4pm
deadline do homework /by no idea :-p
list
done 2
done 1
list
bye