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

[azfarulmatin] IP #70

Open
wants to merge 31 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 16 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
326786b
Add chatbot message
azfarulmatin Sep 5, 2023
942c0c4
Add level 1
azfarulmatin Sep 5, 2023
c971713
Add and list
azfarulmatin Sep 5, 2023
1eef87b
Mark as done
azfarulmatin Sep 5, 2023
ab74f0a
Add the ability to echo commands
azfarulmatin Sep 6, 2023
b8c0f92
Add list function for user to store tasks
azfarulmatin Sep 6, 2023
d668a45
Add the ability to mark tasks as done
azfarulmatin Sep 6, 2023
ad137e5
Tweak the code to comply with a coding standard
azfarulmatin Sep 6, 2023
7ffd63c
Add support for tracking todo, deadlines and events
azfarulmatin Sep 6, 2023
62bc7a5
Improve code quality
azfarulmatin Sep 6, 2023
2c6d3b6
Handle errors for todo
azfarulmatin Sep 15, 2023
aa236d0
Handle errors for todo event and deadline
azfarulmatin Sep 15, 2023
a3b8c95
Fix bugs for spacebar error
azfarulmatin Sep 15, 2023
47c7370
Divide classes into packages
azfarulmatin Sep 15, 2023
7d2298f
Fix bugs for task list quantity
azfarulmatin Sep 15, 2023
8b22a82
Fix bugs for marking and unmarking of tasks.
azfarulmatin Sep 15, 2023
7380c55
Add support for deleting tasks from list
azfarulmatin Sep 19, 2023
2da4d1d
Create a method to parse a string into a Task object and convert task…
azfarulmatin Sep 19, 2023
31bc5a8
Save task into the harddisk automatically whenever there are changes
azfarulmatin Sep 19, 2023
327f29d
FIx bugs
azfarulmatin Sep 21, 2023
87d459f
Merge branch 'branch-Level-6'
azfarulmatin Sep 21, 2023
8b28585
Merge branch 'branch-Level-7'
azfarulmatin Sep 21, 2023
4721aee
Add jar manifest
azfarulmatin Sep 21, 2023
9e5e42d
Refactor the code to separate classes
azfarulmatin Oct 6, 2023
80dd827
Fix bugs for delete task
azfarulmatin Oct 6, 2023
db3d7b7
Fix bugs when trying to save Deadline task
azfarulmatin Oct 6, 2023
2a15cd2
Add find function
azfarulmatin Oct 6, 2023
1b5de63
Add JavaDoc comments to the code
azfarulmatin Oct 6, 2023
a369c08
Update README.md
azfarulmatin Oct 6, 2023
b62c924
A-UserGuide
azfarulmatin Oct 6, 2023
d5a00ce
Fix bugs for txt file location
azfarulmatin Oct 6, 2023
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
10 changes: 0 additions & 10 deletions src/main/java/Duke.java

This file was deleted.

19 changes: 19 additions & 0 deletions src/main/java/duke/Deadline.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
package duke;
public class Deadline extends Task {
protected String by;

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

@Override
public String getTaskType() {
return "Deadline";
}

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

public class Duke {
public void run() {
String LINE = "__________________________________________\n";
System.out.println(LINE
+ "Hello I'm MatinBot\n"
+ "What can I do for you?\n"
+ LINE);

Scanner scanner = new Scanner(System.in);
Task[] tasks = new duke.Task[100];
int count = 0;

Choose a reason for hiding this comment

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

Consider using a more suitable variable name to keep track of your number of tasks.

String userInput;

while (true) {
// Get the user input first
userInput = scanner.nextLine();
System.out.println(LINE);

try {
if (userInput.equals("bye")) {
break;
} else if (userInput.equals("list")) {
printTasks(tasks, count);
} else if (userInput.startsWith("mark")) {
markTask(userInput, tasks, count);
} else if (userInput.startsWith("unmark")) {
unmarkTask(userInput, tasks, count);
} else if (userInput.startsWith("todo")) {
String description = userInput.replaceFirst("todo", "").trim();
if (description.isEmpty()) {
throw new EmptyDescriptionException("todo");
}
addTask("todo " + description, tasks, count, "todo");
count++;
} else if (userInput.startsWith("deadline")) {
String description = userInput.replaceFirst("deadline", "").trim();
if (description.isEmpty()) {
throw new EmptyDescriptionException("deadline");
}
// Check for /by in the description
if (!description.contains("/by")) {
System.out.println("☹ OOPS!!! The deadline task must include '/by' to specify the date.");
} else {
addTask("deadline " + description, tasks, count, "deadline");
count++;
}
} else if (userInput.startsWith("event")) {
String description = userInput.replaceFirst("event", "").trim();
if (description.isEmpty()) {
throw new EmptyDescriptionException("event");
}
// Check for /from and /to in the description
if (!description.contains("/from") || !description.contains("to")) {
System.out.println("☹ OOPS!!! The event task must include '/from' and '/to' to specify the date range.");
} else {
addTask("event " + description, tasks, count, "event");
count++;
}
} else {
throw new UnknownCommandException();
}
} catch (EmptyDescriptionException e) {
System.out.println(e.getMessage());
} catch (UnknownCommandException e) {
System.out.println(e.getMessage());
}
System.out.println(LINE);
}

System.out.println("Bye. Hope to see you again soon!\n" + LINE);
scanner.close();
}

Choose a reason for hiding this comment

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

Consider abstracting away some of the heavy logic to reduce long methods. Generally more than 30 lines is too long.

// Create a task of a specific type to the tasks array
private static void addTask(String userInput, Task[] tasks, int count, String type) {
String description;
String bracketInfo;

// Check for any inputs with '/'
int checkSlash = userInput.indexOf("/");
if (checkSlash != -1) {
description = userInput.substring(type.length() + 1, checkSlash).trim();
bracketInfo = userInput.substring(checkSlash).trim();
} else {
description = userInput.substring(type.length() + 1).trim();
bracketInfo = "";
}

// Checks the task type
switch (type) {
case "todo":

Choose a reason for hiding this comment

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

Violation of coding standard here, there should not be an indentation for the case clause.

tasks[count] = new Todo(description);
break;
case "deadline":
String by = bracketInfo.replace("/by", "").trim();
tasks[count] = new Deadline(description, by);
break;
case "event":
String from = bracketInfo.split("/from")[1].split("/to")[0].trim();
String to = bracketInfo.split("/to")[1].trim();
tasks[count] = new Event(description, from, to);
break;
default:
System.out.println("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
return;
}

System.out.println("Got it. I've added this task:\n"
+ tasks[count]
+ "\nNow you have "
+ (count + 1)
+ " tasks in the list.");
}

// Print the task list
private static void printTasks(Task[] tasks, int count) {
// Checks if there is anything in the list
if (tasks.length == 0) {
System.out.println("☹ OOPS!!! The list is empty");
return;
}
else {
System.out.println("Here are the tasks in your list:");
for (int i = 0; i < count; i++) {
if (tasks[i] != null) {
System.out.println((i + 1) + ". " + tasks[i].toString());
}
}
}
}

// Mark a task as done
private static void markTask(String userInput, Task[] tasks, int count) {
// Check if the task list is empty
if (count == 0) {
System.out.println("☹ OOPS!!! There are no tasks to mark.");
return;
}

try {
int taskIndex = Integer.parseInt(userInput.substring(5)) - 1;

// Check if the task index is valid
if (taskIndex >= 0 && taskIndex < count) {
tasks[taskIndex].markAsDone();
System.out.println("Nice! I've marked this task as done:\n" + tasks[taskIndex]);
} else {
System.out.println("☹ OOPS!!! Please provide a valid task number.");
}
} catch (NumberFormatException e) {
System.out.println("☹ OOPS!!! Please use 'mark [task number]'.");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("☹ OOPS!!! Please use 'mark [task number]'.");
}
}

// Unmark a task
private static void unmarkTask(String userInput, Task[] tasks, int count) {
// Check if the task list is empty
if (count == 0) {
System.out.println("☹ OOPS!!! There are no tasks to unmark.");
return;
}

try {
int taskIndex = Integer.parseInt(userInput.substring(7)) - 1;

// Check if the task index is valid
if (taskIndex >= 0 && taskIndex < count) {
tasks[taskIndex].markAsUndone();
System.out.println("OK, I've marked this task as not done yet:\n" + tasks[taskIndex]);
} else {
System.out.println("☹ OOPS!!! Please provide a valid task number.");
}
} catch (NumberFormatException e) {
System.out.println("☹ OOPS!!! Please use 'unmark [task number]'.");
} catch (StringIndexOutOfBoundsException e) {
System.out.println("☹ OOPS!!! Please use 'unmark [task number]'.");
}
}

// Custom exception for empty task descriptions
static class EmptyDescriptionException extends Exception {
private String taskType;

public EmptyDescriptionException(String taskType) {
super("☹ OOPS!!! The description of a " + taskType + " cannot be empty.");
this.taskType = taskType;
}

public String getTaskType() {
return taskType;
}
}

// Custom exception for unknown commands
class UnknownCommandException extends Exception {
public UnknownCommandException() {
super("☹ OOPS!!! I'm sorry, but I don't know what that means :-(");
}
}
public static void main(String[] args) {
Duke duke = new Duke(); // Create an instance of the Duke class
duke.run(); // Run the chatbot
}
}
22 changes: 22 additions & 0 deletions src/main/java/duke/Event.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package duke;
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 getTaskType() {
return "Event";
}

@Override
public String toString() {
return "[E]" + super.toString() + " (from: " + from + " to: " + to + ")";
}
}

39 changes: 39 additions & 0 deletions src/main/java/duke/Task.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package duke;
public class Task {
protected String description;
protected boolean isDone;

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

public void markAsDone() {
isDone = true;
}

public void markAsUndone() {
isDone = false;
}

public boolean isDone() {
return isDone;
}

public String getDescription() {
return description;
}

public String getTaskType() {
return "Task";
}

@Override
public String toString() {
if (isDone) {
return "[X] " + description;
} else {
return "[ ] " + description;
}
}
}
16 changes: 16 additions & 0 deletions src/main/java/duke/Todo.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package duke;
public class Todo extends Task {
public Todo(String description) {
super(description);
}

@Override
public String getTaskType() {
return "Todo";
}

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