-
Notifications
You must be signed in to change notification settings - Fork 114
[Ryan Tan] iP #103
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
base: master
Are you sure you want to change the base?
[Ryan Tan] iP #103
Changes from 9 commits
812d612
5f645dc
398cd22
c034cfe
e40ebef
ac8dc0f
629ee35
2e66a92
24f24e1
85d21eb
baaa892
b541ecf
3f8a8c4
86955e5
f6e1caf
0026848
792321a
1deffba
320ae65
6fc8417
13228d4
4f06d99
125ed9b
ca9594a
f7f975c
5a0c049
0c922cd
7abb9b4
0e51847
2af8a9f
4cdbe9d
eb37c84
2cd120d
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 |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| public class Deadline extends Task{ | ||
|
|
||
| protected String by; | ||
|
|
||
| public Deadline(boolean isDone, String taskName, String by) { | ||
| super(isDone, taskName); | ||
| this.by = by; | ||
| } | ||
|
|
||
| public Deadline updateIsDone(boolean newIsDone) { | ||
| return new Deadline(newIsDone, this.taskName, this.by); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| public class Event extends Task{ | ||
|
|
||
| protected String from; | ||
| protected String to; | ||
|
|
||
|
|
||
| public Event(boolean isDone, String taskName, String from, String to) { | ||
| super(isDone, taskName); | ||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
|
|
||
| public Event updateIsDone(boolean newIsDone) { | ||
| return new Event(newIsDone, this.taskName, this.from, this.to); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + " (from: " + this.from + " to " + this.to + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,40 @@ | ||
| public class InputHandler { | ||
|
|
||
| public static void processInputCases(String input) { | ||
| if (input.equals("list")) { | ||
| TaskList.listTasks(); | ||
| return; | ||
| } | ||
|
|
||
| String[] words = input.split(" "); | ||
| int taskIndex; | ||
|
|
||
| switch (words[0]) { | ||
| case "mark": | ||
| taskIndex = Integer.parseInt(words[1]) - 1; | ||
| TaskList.markTask(taskIndex); | ||
| break; | ||
| case "unmark": | ||
| taskIndex = Integer.parseInt(input.substring(7)) - 1; | ||
| TaskList.unmarkTask(taskIndex); | ||
| break; | ||
| case "todo": | ||
| TaskList.addTask(new ToDo(false, input.substring(5))); | ||
| break; | ||
| case "deadline": | ||
| String[] deadlineParts = input.substring(9).split("/by", 2); | ||
| TaskList.addTask(new Deadline(false, deadlineParts[0], deadlineParts[1])); | ||
| break; | ||
|
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 the clear logic, use of switch and how compact everything is! One thing to consider is adding more error handling, especially around Integer.parseInt() and array indexing, to prevent crashes on invalid inputs. It might also be worth refactoring the splitting of strings into helper methods for better readability . |
||
| case "event": | ||
| String[] eventParts = input.split("/from", 2); | ||
|
|
||
| // Split the remaining part by /to | ||
| String[] timeParts = eventParts[1].split("/to", 2); | ||
|
|
||
| TaskList.addTask(new Event(false, eventParts[0], timeParts[0], timeParts[1])); | ||
| break; | ||
| default: | ||
| TaskList.addTask(new Task(false, input)); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class Task { | ||
|
|
||
| protected final boolean isDone; | ||
| protected final String taskName; | ||
|
|
||
| public Task(boolean isDone, String taskName) { | ||
| this.isDone = isDone; | ||
| this.taskName = taskName; | ||
| } | ||
|
|
||
| public Task updateIsDone(boolean newIsDone) { | ||
| return new Task(newIsDone, this.taskName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return ("[" + (isDone ? "X" : " ") + "] " + taskName); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| public class TaskList { | ||
|
|
||
| private static final int MAX_TASKS = 100; | ||
|
|
||
| private static final Task[] tasks = new Task[MAX_TASKS]; // Array to store tasks | ||
| private static int tasksCount = 0; // Counter to keep track of the number of tasks | ||
|
|
||
| public static void listTasks() { | ||
| Tommi.printLine(); | ||
| System.out.println("Here are the tasks in your list:"); | ||
| for (int i = 0; i < tasksCount; i++) { | ||
| System.out.println((i + 1) + ". " + tasks[i]); | ||
| } | ||
| Tommi.printLine(); | ||
| } | ||
|
|
||
| public static void addTask(Task task) { | ||
| tasks[tasksCount] = task; | ||
| tasksCount++; | ||
| Tommi.printLine(); | ||
| System.out.println("Sure. I've added the task: " + System.lineSeparator() | ||
| + task + System.lineSeparator() | ||
| + "There are now " + tasksCount + " tasks in the list."); | ||
| Tommi.printLine(); | ||
|
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 class is well structured, and the methods are concise and really easy to understand 😄. Would you want to switch a dynamic data structure later down the track (i.e. ArrayList), to give you some scalability? It might also be good to add boundary checks for adding tasks to ensure it doesn’t exceed MAX_TASKS. Overall, great foundation! |
||
| } | ||
|
|
||
| public static void markTask(int index) { | ||
| if (index >= 0 && index < tasksCount) { | ||
| Tommi.printLine(); | ||
| System.out.println("Awesomesauce! I've marked this task as done:"); | ||
| tasks[index] = tasks[index].updateIsDone(true); | ||
| System.out.println(tasks[index]); | ||
| Tommi.printLine(); | ||
| } | ||
| } | ||
|
|
||
| public static void unmarkTask(int index) { | ||
| if (index >= 0 && index < tasksCount) { | ||
| Tommi.printLine(); | ||
| System.out.println("OK, I've marked this task as undone:"); | ||
| tasks[index] = tasks[index].updateIsDone(false); | ||
| System.out.println(tasks[index]); | ||
| Tommi.printLine(); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| public class ToDo extends Task{ | ||
|
|
||
| public ToDo(boolean isDone, String taskName) { | ||
| super(isDone, taskName); | ||
| } | ||
|
|
||
| public ToDo updateIsDone(boolean newIsDone) { | ||
| return new ToDo(newIsDone, this.taskName); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,44 @@ | ||
| import java.util.Scanner; | ||
|
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 adding comments to help with readability |
||
|
|
||
| public class Tommi { | ||
|
|
||
| public static void main(String[] args) { | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| printIntroMessage(); | ||
| readInputStrings(scanner); | ||
| printExitMessage(); | ||
| } | ||
|
|
||
| private static void readInputStrings(Scanner scanner) { | ||
| String input = scanner.nextLine(); | ||
| while (!input.equals("bye")) { | ||
| InputHandler.processInputCases(input); | ||
| input = scanner.nextLine(); | ||
| } | ||
| } | ||
|
|
||
| private static void printIntroMessage() { | ||
| String intro = """ | ||
| ______ \s | ||
| /_ __/__ __ _ __ _ (_) | ||
| / / / _ \\/ ' \\/ ' \\/ /\s | ||
| /_/ \\___/_/_/_/_/_/_/_/ \s | ||
| ____________________________________________________________ | ||
| Hello! I'm Tommi! | ||
| What can I do for you? | ||
| ____________________________________________________________ | ||
| """; | ||
| System.out.println(intro); | ||
| } | ||
|
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 appreciate the effort gone to customise your logo, and abstract it to a function for later use! |
||
|
|
||
| private static void printExitMessage() { | ||
| printLine(); | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| printLine(); | ||
| } | ||
|
|
||
| public static void printLine() { | ||
| System.out.println("____________________________________________________________"); | ||
|
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 using ( "_*30" ) instead |
||
| } | ||
| } | ||
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.
I like the immutability of the updateIsDone method and the clear toString representation. A small suggestion would be to validate the by field if it's supposed to follow a specific format (like a date or time). You might also want to add a method to update the deadline (by) in the future, just like you did for isDone. Otherwise, everything looks really well-structured!