-
Notifications
You must be signed in to change notification settings - Fork 78
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
base: master
Are you sure you want to change the base?
Changes from 16 commits
326786b
942c0c4
c971713
1eef87b
ab74f0a
b8c0f92
d668a45
ad137e5
7ffd63c
62bc7a5
2c6d3b6
aa236d0
a3b8c95
47c7370
7d2298f
8b22a82
7380c55
2da4d1d
31bc5a8
327f29d
87d459f
8b28585
4721aee
9e5e42d
80dd827
db3d7b7
2a15cd2
1b5de63
a369c08
b62c924
d5a00ce
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
This file was deleted.
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 + ")"; | ||
} | ||
} |
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; | ||
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(); | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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": | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
} | ||
} |
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 + ")"; | ||
} | ||
} | ||
|
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; | ||
} | ||
} | ||
} |
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(); | ||
} | ||
} |
There was a problem hiding this comment.
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.