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

[Guoyi] ip #109

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all 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
126 changes: 126 additions & 0 deletions src/main/java/Duke.java
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import java.util.Scanner;

public class Duke {
public static void main(String[] args) {
String logo = " ____ _ \n"
Expand All @@ -6,5 +8,129 @@ public static void main(String[] args) {
+ "| |_| | |_| | < __/\n"
+ "|____/ \\__,_|_|\\_\\___|\n";
System.out.println("Hello from\n" + logo);
System.out.println("Hello! I'm Duke");
System.out.println("What can I do for you?");

Scanner scanner = new Scanner(System.in);
Task[] tasks = new Task[100];
Copy link

@joshuan98 joshuan98 Sep 16, 2024

Choose a reason for hiding this comment

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

You can avoid the use of 100 here which is a magic number.

int taskCount = 0;
String input;

while (true) {
input = scanner.nextLine();
if (input.equals("bye")) {

Choose a reason for hiding this comment

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

Instead of using a long list of else ifs you could consider re factoring this into switch case form to improve readability. Just a suggestion!

System.out.println("Bye. Hope to see you again soon!");
break;
} else if (input.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[i]);
}
} else if (input.startsWith("mark ")) {
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1;
tasks[taskIndex].markAsDone();
System.out.println("Nice! I've marked this task as done:");
System.out.println(" " + tasks[taskIndex]);
} else if (input.startsWith("unmark ")) {
int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1;
tasks[taskIndex].markAsNotDone();
System.out.println("OK, I've marked this task as not done yet:");
System.out.println(" " + tasks[taskIndex]);
} else if (input.startsWith("todo ")) {
String description = input.substring(5);
tasks[taskCount] = new ToDo(description);
taskCount++;
System.out.println("Got it. I've added this task:");
System.out.println(" " + tasks[taskCount - 1]);
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else if (input.startsWith("deadline ")) {
String[] parts = input.substring(9).split(" /by ");
tasks[taskCount] = new Deadline(parts[0], parts[1]);

Choose a reason for hiding this comment

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

You could consider adding some edge case catchers here to remind user the correct argument usage.
For example, if the user forgets to put /by you could print out something like expected argument containing /by

taskCount++;
System.out.println("Got it. I've added this task:");
System.out.println(" " + tasks[taskCount - 1]);
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else if (input.startsWith("event ")) {
String[] parts = input.substring(6).split(" /from | /to ");
tasks[taskCount] = new Event(parts[0], parts[1], parts[2]);
taskCount++;
System.out.println("Got it. I've added this task:");
System.out.println(" " + tasks[taskCount - 1]);
System.out.println("Now you have " + taskCount + " tasks in the list.");
} else {
System.out.println("I'm sorry, I don't understand that command.");
}
}

scanner.close();
}
}

class Task {
protected String description;
protected boolean isDone;

Choose a reason for hiding this comment

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

Great job following the naming conventions for boolean variables!


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

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

public void markAsNotDone() {
this.isDone = false;
}

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

@Override
public String toString() {
return "[" + getStatusIcon() + "] " + description;
}
}

class ToDo extends Task {

public ToDo(String description) {
super(description);
}

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

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 + ")";
}
}

class Event extends Task {
protected String from;

Choose a reason for hiding this comment

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

Great job keeping all variables in protected accessibility to improve security of your object's members!

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: " + from + " to: " + to + ")";
}
}