Skip to content
Open
Show file tree
Hide file tree
Changes from 7 commits
Commits
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
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,4 @@ bin/

/text-ui-test/ACTUAL.TXT
text-ui-test/EXPECTED-UNIX.TXT
*.class
12 changes: 12 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
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 + ")";
}
}
83 changes: 77 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,81 @@
import java.util.Scanner;
import java.util.ArrayList;
public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
Scanner scanner = new Scanner(System.in);
boolean isRunning = true;
int taskCount = 0;
ArrayList<Task> tasks = new ArrayList<>();
// Task[] tasks = new Task[100];
System.out.println("Hello! I'm Chatty\nWhat can I do for you?");
System.out.println("____________________________________________________________");
while (isRunning) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like that there are spaces around the brackets :D

try {
String userInput = scanner.nextLine();
System.out.println("____________________________________________________________");
if (userInput.equals("bye")) {
System.out.println("Bye. Hope to see you again soon!");
isRunning = false;
} else if (userInput.equals("list")){
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
} else if (userInput.equals("list")){
} else if (userInput.equals("list")) {

System.out.println("Here are the tasks in your list:");
for (int i = 0; i < taskCount; i++) {
System.out.println(i + 1 + "." + tasks.get(i));
}
} else if (userInput.startsWith("mark ")) {
System.out.println("Nice! I've marked this task as done:");
int taskIndex = Integer.parseInt(userInput.substring(5)) - 1;
tasks.get(taskIndex).switchCheck();
System.out.println(tasks.get(taskIndex).toString());
} else if (userInput.startsWith("unmark ")) {
System.out.println("OK, I've marked this task as not done yet:");
int taskIndex = Integer.parseInt(userInput.substring(7)) - 1;
tasks.get(taskIndex).switchCheck();
System.out.println(tasks.get(taskIndex).toString());
} else if (userInput.startsWith("todo ")) {
if (userInput.length() <= 5) {
throw new DukeException("OOPS!!! The description of a todo cannot be empty.");
}
Task newToDo = new Todo(userInput.substring(5));
System.out.println("Got it. I've added this task:");
tasks.add(new Todo(userInput));
taskCount++;
System.out.println(newToDo.toString());
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else if (userInput.startsWith("deadline ")) {
System.out.println("Got it. I've added this task:");
String description = userInput.substring(9, userInput.indexOf("/by")).trim();
String by = userInput.substring(userInput.indexOf("/by") + 4).trim();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would there be issues if no /by parameter is specified? Perhaps a DukeException could be thrown in that case?

Task newDeadline = new Deadline(description, by);
tasks.add(newDeadline);
taskCount++;
System.out.println(newDeadline.toString());
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else if (userInput.startsWith("event ")) {
System.out.println("Got it. I've added this task:");
String description = userInput.substring(6, userInput.indexOf("/from")).trim();
String from = userInput.substring(userInput.indexOf("/from") + 6, userInput.indexOf("/to")).trim();
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
String from = userInput.substring(userInput.indexOf("/from") + 6, userInput.indexOf("/to")).trim();
String from = userInput.substring(
userInput.indexOf("/from") + 6, userInput.indexOf("/to")
).trim();

the original line is over 100 characters ;-;

String to = userInput.substring(userInput.indexOf("/to") + 4).trim();
Task newEvent = new Event(description, from, to);
tasks.add(newEvent);
taskCount++;
System.out.println(newEvent.toString());
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else if (userInput.startsWith("delete ")) {
System.out.println("Noted. I've removed this task:");
int taskIndex = Integer.parseInt(userInput.substring(7)) - 1;
System.out.println(tasks.get(taskIndex));
tasks.remove(taskIndex);
taskCount--;
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else {
throw new DukeException("OOPS!!! I'm sorry, but I don't know what that means :-(");
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can put all these into another class, which makes it more simple and clear

} catch (DukeException e) {
// Handle Duke-specific exceptions with meaningful error messages
System.out.println(e.getMessage());
}
System.out.println("____________________________________________________________");
}
scanner.close();
}
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can add more Javadosc to the code

}
5 changes: 5 additions & 0 deletions src/main/java/DukeException.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class DukeException extends Exception {
public DukeException(String message) {
super(message);
}
}
14 changes: 14 additions & 0 deletions src/main/java/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Event extends Task {
protected String from;
protected String to;

public Event(String description, String from, String to) {
super(description);
this.from = from;
this.to = to;
}
@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + this.from + " to: " + this.to + ")";
}
}
22 changes: 22 additions & 0 deletions src/main/java/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Task {
protected String description;
protected boolean isDone;

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

public String getStatusIcon() {
return (isDone ? "X" : " ");
}

public void switchCheck() {
this.isDone = !this.isDone;
}

@Override
public String toString() {
return "[" + this.getStatusIcon() + "]" + " " + this.description;
}
}
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 @@
public class Todo extends Task {
public Todo(String description) {
super(description);

}

@Override
public String toString() {
return "[T]" + super.toString();
}
}