Skip to content
Open
Changes from 5 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
121 changes: 115 additions & 6 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,10 +1,119 @@
import java.util.HashMap;
import java.util.Scanner;

public class Duke {
private static Task[] tasks = new Task[100];
private static int numTasks = 0;
public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
+ "| | | | | | | |/ / _ \\\n"
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
printGreeting();
Comment thread
kvn-zhang marked this conversation as resolved.
Outdated

Scanner in = new Scanner(System.in);
String line;
while (in.hasNextLine()) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Maybe can check "bye" condition here

line = in.nextLine();
if (line.equals("bye")) {
break;
}

if (line.equals("list")) {
printTasks();
} else {
handleCommand(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.

What do you think about refactoring this into an "if - else if - else" statement instead of having a separate "if" statement and another "if - else" statement?

}
System.out.println("Bye. Hope to see you again soon!");
}

private static void handleCommand(String line) {
Comment thread
kvn-zhang marked this conversation as resolved.
Outdated
int divider = line.indexOf(" ");

if (line.contains("mark")) {
int idx = Integer.parseInt(line.substring(divider + 1)) - 1;
if (idx < 0 || tasks[idx] == null) {
System.out.println("Sorry! That's not a valid task");
return;
}

markTask(idx, line.startsWith("mark"));
return;
}
if (line.startsWith("todo")){
addTask(new Todo(line.substring(divider + 1)));
return;
}

HashMap<String, String> parameters = parseParameters(line);
String description = parameters.get("description");
if (description == null) {
System.out.println("Sorry! Please provide a valid description");
return;
}
if (line.startsWith("deadline")) {
String by = parameters.get("by");
if (by == null) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

you should consider using if-else statements instead of all if statements. Another alternative if using switch-case statements

System.out.println("Sorry! Please provide a valid `by`");
return;
}
addTask(new Deadline(description, parameters.get("by")));
} else if (line.startsWith("event")) {
String from = parameters.get("from");
String to = parameters.get("to");
if (from == null || to == null) {
System.out.println("Sorry! Please provide a valid `from` and/or `to`");
return;
}
addTask(new Event(description, from, to));
}
}

private static HashMap<String, String> parseParameters(String line) {
HashMap<String, String> fieldToValue = new HashMap<>();

int startDescription = line.indexOf(" ");

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

for consistency, can rename to startOfDescription

int endOfDescription = line.indexOf(" /");
fieldToValue.put("description", line.substring(startDescription + 1, endOfDescription));

String[] splitParams = line.split(" /");
for (int i = 1; i < splitParams.length; i++) {
String rawParam = splitParams[i];
int divider = rawParam.indexOf(" ");

String field = rawParam.substring(0, divider);
String value = rawParam.substring(divider + 1);
fieldToValue.put(field, value);
Comment thread
kvn-zhang marked this conversation as resolved.
Outdated
}

return fieldToValue;
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I love that you factored this out into a separate method!
However, what do you think about adding a small Javadoc comment? It took me a bit to figure out how exactly the HashMap you return is structured, and I think that just documenting this in a small comment might make it easier for future contributors


public static void markTask(int index, boolean isDone) {
tasks[index].setStatus(isDone);
if (isDone) {
System.out.println("Nice! I've marked this task as done:");
} else {
System.out.println("OK, I've marked this task as not done yet:");
}
System.out.println(tasks[index].getFormattedTask());
}

private static void printGreeting() {
System.out.println("Hello! I'm KevBot");
System.out.println("What can I do for you?");
}

public static void printTasks() {
for (int i = 0; i < tasks.length && tasks[i] != null; i++) {
System.out.println((i + 1) + ". " + tasks[i].getFormattedTask());
}
}

public static void addTask(Task task) {
if (numTasks == tasks.length) {
System.out.println("Sorry. The task list is all full");
}

tasks[numTasks] = task;
numTasks++;
System.out.println("Got it. I've added this task:\n" + task.getFormattedTask() + "\nNow you have " + numTasks + " tasks in the 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.

That line seems a bit long (although in the GitHub Web Interface I unfortunately can't see its exact length); what do you think about breaking it into two lines?

Comment thread
kvn-zhang marked this conversation as resolved.
Outdated
}
}