-
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 10 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,200 @@ | ||
import java.util.Scanner; | ||
import java.util.Arrays; | ||
|
||
class Task { | ||
protected String description; | ||
protected boolean isDone; | ||
|
||
public Task(String description) { | ||
this.description = description; | ||
this.isDone = false; | ||
} | ||
public void markAsDone() { | ||
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. you can consider spacing out (added new lines between) your functions for better readability |
||
isDone = true; | ||
} | ||
public void markAsUndone() { | ||
isDone = false; | ||
} | ||
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. you can consider combining this to become a setter method like "void setFound(boolean isFound);" |
||
public boolean isDone() { | ||
return isDone; | ||
} | ||
public String getDescription() { | ||
return description; | ||
} | ||
public String getTaskType() { | ||
return "Task"; | ||
} | ||
@Override | ||
public String toString() { | ||
if (isDone) { | ||
return "[X] " | ||
+ description; | ||
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. Any reason for separating it on different lines? It's a rather short line :) |
||
} else { | ||
return "[ ] " | ||
+ description; | ||
} | ||
} | ||
} | ||
|
||
class Todo extends Task { | ||
public Todo(String description) { | ||
super(description); | ||
} | ||
@Override | ||
public String getTaskType() { | ||
return "Todo"; | ||
} | ||
@Override | ||
public String toString() { | ||
return "[T]" | ||
+ super.toString(); | ||
} | ||
} | ||
|
||
class Deadline extends Task { | ||
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. having a new file for each class can keep your code more organised (abstraction) |
||
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() | ||
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. The code uses string concatenation to build the final string for the object's representation. While this approach works, it can be a bit less readable, especially when dealing with multiple string elements. Consider using String.format() for improved readability and performance. |
||
+ " (by: " | ||
+ by | ||
+ ")"; | ||
} | ||
} | ||
|
||
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 + ")"; | ||
} | ||
} | ||
|
||
public class Duke { | ||
public static void main(String[] args) { | ||
String logo = " ____ _ \n" | ||
+ "| _ \\ _ _| | _____ \n" | ||
+ "| | | | | | | |/ / _ \\\n" | ||
+ "| |_| | |_| | < __/\n" | ||
+ "|____/ \\__,_|_|\\_\\___|\n"; | ||
System.out.println("Hello from\n" + logo); | ||
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 Task[100]; | ||
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. Good job on always having proper naming of functions, classes and variables! 👍 |
||
int count = 0; | ||
String userInput; | ||
|
||
while (true) { | ||
// Get the user input first | ||
userInput = scanner.nextLine(); | ||
System.out.println(LINE); | ||
|
||
if (userInput.equals("bye")) { | ||
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. could this be refactored into a switch statement for better readability? |
||
break; | ||
} else if (userInput.equals("list")) { | ||
printTasks(tasks, count); | ||
} else if (userInput.startsWith("mark ")) { | ||
markTask(userInput, tasks); | ||
} else if (userInput.startsWith("unmark ")) { | ||
unmarkTask(userInput, tasks); | ||
} else if (userInput.startsWith("todo ")) { | ||
addTask(userInput, tasks, count, "Todo"); | ||
count++; | ||
} else if (userInput.startsWith("deadline ")) { | ||
addTask(userInput, tasks, count, "Deadline"); | ||
count++; | ||
} else if (userInput.startsWith("event ")) { | ||
addTask(userInput, tasks, count, "Event"); | ||
count++; | ||
} | ||
System.out.println(LINE); | ||
} | ||
|
||
System.out.println("Bye. Hope to see you again soon!\n" + LINE); | ||
scanner.close(); | ||
} | ||
} | ||
|
||
// 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; | ||
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. Perhaps there might be a better more suggestive naming of the variable? |
||
|
||
// Check for any inputs with '/' | ||
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. I like how you added comments for easier readability! 👍 |
||
int checkSlash = userInput.indexOf("/"); | ||
if (checkSlash != -1) { | ||
description = userInput.substring(type.length() + 1, checkSlash).trim(); | ||
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. Could this be refactored into a function? As we repeat it twice |
||
bracketInfo = userInput.substring(checkSlash).trim(); | ||
} else { | ||
description = userInput.substring(type.length() + 1).trim(); | ||
bracketInfo = ""; | ||
} | ||
|
||
if (type.equals("Todo")) { | ||
tasks[count] = new Todo(description); | ||
} else if (type.equals("Deadline")) { | ||
String by = bracketInfo.replace("/by", "").trim(); | ||
tasks[count] = new Deadline(description, by); | ||
} else if (type.equals("Event")) { | ||
String from = bracketInfo.split("/from")[1].split("/to")[0].trim(); | ||
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. The code is relatively concise, but it might benefit from some additional comments or variable names to make it more self-explanatory. For example, you could define variables with more descriptive names for the split values. |
||
String to = bracketInfo.split("/to")[1].trim(); | ||
tasks[count] = new Event(description, from, to); | ||
} | ||
|
||
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) { | ||
System.out.println("Here are the tasks in your list:"); | ||
for (int i = 0; i < count; i++) { | ||
System.out.println((i + 1) | ||
+ ". " | ||
+ tasks[i].toString()); | ||
} | ||
} | ||
|
||
// Mark a task as done | ||
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. Great job in explicitly commenting on the purpose of functions you added! |
||
private static void markTask(String userInput, Task[] tasks) { | ||
int taskIndex = Integer.parseInt(userInput.substring(5)) - 1; | ||
tasks[taskIndex].markAsDone(); | ||
System.out.println("Nice! I've marked this task as done:\n" | ||
+ tasks[taskIndex]); | ||
} | ||
|
||
// Unmark a task | ||
private static void unmarkTask(String userInput, Task[] tasks) { | ||
int taskIndex = Integer.parseInt(userInput.substring(7)) - 1; | ||
tasks[taskIndex].markAsUndone(); | ||
System.out.println("OK, I've marked this task as not done yet:\n" | ||
+ tasks[taskIndex]); | ||
} | ||
} |
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.
You did well in following the naming conventions (PascalCase for class and camelCase for variables)!!