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
2 changes: 1 addition & 1 deletion docs/README.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
# Duke User Guide
# DBot User Guide

// Update the title above to match the actual product name

Expand Down
111 changes: 111 additions & 0 deletions src/main/java/DBot.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
import java.util.*;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Avoid using wildcard imports
Import only the functions that you need

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps you could import just the libraries you need?


public class DBot {
private static boolean isOn;
private static List<Task> datas;

public static void main(String[] args) {
isOn = true;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider using more descriptive names, that is easily identified to serve a definite purpose

datas = new ArrayList<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps you could use a more descriptive name for your array list, as it is unclear what is being stored here.


String greeting = "____________________________________________________________\n" +

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

use "final" and SCREAMING_SNAKE_CASE for the greeting variable as it is a constant

"Hello! I'm DBot\nWhat can I do for you?\n"
+ "____________________________________________________________\n";
System.out.println(greeting);

Scanner in = new Scanner(System.in);

while (isOn) {
System.out.print("Command: ");
String line = in.nextLine().strip();
System.out.println("____________________________________________________________");

if (line.equals("bye")) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Followed if else layout convention. Good job

System.out.println("Bye. Hope to see you again soon!");
isOn = false;
} else if (line.equals("list")) {
list();
} else if (line.startsWith("mark ")) {
mark(line);
} else if (line.startsWith("unmark ")) {
unmark(line);
} else if (line.startsWith("todo ")) {
todo(line);
} else if (line.startsWith("event ")) {
event(line);
} else if (line.startsWith("deadline ")) {
deadline(line);
} else {
add(line);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps you could refactor this into another function? This chunk is long and hard to read.


System.out.println("____________________________________________________________");
}
}

private static void add(String line) {
datas.add(new Task(line));
System.out.print("added: ");
System.out.println(line);
}

private static void list() {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider naming it as listTasks to avoid confusion with the inbuilt type

for (int i = 0; i < datas.size(); i++) {
System.out.printf("%d.", i + 1);
System.out.println(datas.get(i).toString());
}
}

private static void mark(String line) {
try {
int option = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
System.out.println("Nice! I've marked this task as done:");
datas.get(option - 1).mark();
System.out.println(datas.get(option - 1).toString());
} catch (Exception e) {
System.out.println("Invalid input");
}
}

private static void unmark(String line) {
try {
int option = Integer.parseInt(line.substring(line.indexOf(" ") + 1));
System.out.println("OK, I've marked this task as not done yet:");
datas.get(option - 1).unmark();
System.out.println(datas.get(option - 1).toString());
} catch (Exception e) {
System.out.println("Invalid input");
}
}

private static void todo(String line) {
String todo = line.substring(line.indexOf(" ") + 1).trim();
Todo task =new Todo(todo);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Fix the formatting after the = sign.

datas.add(task);
System.out.println("Got it. I've added this task:");
System.out.println(task);
System.out.println("Now you have " + datas.size() + " tasks in the list.");
}

private static void deadline(String line) {
String deadlinePrompt = line.substring(line.indexOf(" ") + 1).trim();
Hashtable<String, String> arguments = Utilities.getCommandArgument(deadlinePrompt);
String deadline = deadlinePrompt.substring(0, deadlinePrompt.indexOf("/")).trim();
Deadline task = new Deadline(deadline, arguments.get("by"));
datas.add(task);
System.out.println("Got it. I've added this task:");
System.out.println(task);
System.out.println("Now you have " + datas.size() + " tasks in the list.");
}

private static void event(String line) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider naming the methods better for easy maintainability and scalability

String eventPrompt = line.substring(line.indexOf(" ") + 1).trim();
Hashtable<String, String> argument = Utilities.getCommandArgument(eventPrompt);
String event = eventPrompt.substring(0, eventPrompt.indexOf("/")).trim();
Event task = new Event(event, argument.get("from"), argument.get("to"));
datas.add(task);
System.out.println("Got it. I've added this task:");
System.out.println(task);
System.out.println("Now you have " + datas.size() + " tasks in the list.");
}
}
11 changes: 11 additions & 0 deletions src/main/java/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
public class Deadline extends Task {
String by;
public Deadline(String task, String by) {
super(task);
this.by = by;
}
@Override
public String toString(){
return "[D]" + super.toString() + " (by: " + by + ")";
}
}
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

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 {
String from;
String to;
Comment on lines +2 to +3

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider something like fromDate , toDate

public Event(String task, String from, String to) {
super(task);
this.from = from;
this.to = to;
}

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

public Task(String task) {
this(task, false);
}
public Task(String task, boolean isDone) {
this.task = task.strip();
this.isDone = isDone;
}

public String getTask() {
return task;
}

public boolean isDone() {
return isDone;
}

public void mark(){

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

add a space before the "{" to keep layout consistent

isDone = true;
}

public void unmark(){
isDone = false;
}

public String toString(){
return "[" +
(isDone ? "X" : " ") +
"] " +
task;
Comment on lines +67 to +70

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Perhaps you could write this all in 1 line of code to make your code more readable.

Comment on lines +67 to +70

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Consider same line implementation to increase code readability

}

}
10 changes: 10 additions & 0 deletions src/main/java/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
public class Todo extends Task {
public Todo(String task) {
super(task);
}

@Override
public String toString() {
return "[T]" + super.toString();
}
}
19 changes: 19 additions & 0 deletions src/main/java/Utilities.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import java.util.Hashtable;

public class Utilities {
public static Hashtable<String, String> getCommandArgument(String line) {
Hashtable<String, String> args = new Hashtable<>();

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Try to not use vague names for variables

String[] commandParams = (line.substring(line.indexOf(" ") + 1).split("(?=/)|(?<=/)"));
boolean isArgument = false;
for (String s : commandParams) {
s = s.trim();
if (s.equals("/")) {
isArgument = true;
} else if (isArgument) {
args.put(s.substring(0, s.indexOf(' ')), s.substring(s.indexOf(' ')).trim());
isArgument = false;
}
}
return args;
}
}