-
Notifications
You must be signed in to change notification settings - Fork 114
[Ayushi Yadav] iP #125
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?
[Ayushi Yadav] iP #125
Changes from 10 commits
ad8e067
07f59be
bfdbfe9
0698d9b
7248062
3ae2e49
8f0e008
a417128
2e8c2f6
b8ae068
ac1aa44
b4e8976
40fc93a
75a9741
3579aea
e3f4fc3
8bd8b6d
bafe749
2dfc562
bada908
2e1d503
2433b68
7269ffa
dfc56c4
5bd867c
caa6b86
be6ab9d
7ca3341
aba22b9
8bf1b20
8a1825b
bdafc37
b837998
519e612
5cdce35
573b20e
2bb52c2
fe5010c
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,31 @@ | ||
| T | 0 | hi | ||
| T | 1 | hoisdjfhsd | ||
| T | 0 | dg | ||
| T | 0 | gwrs | ||
| T | 1 | grw | ||
| T | 0 | g | ||
| T | 0 | wrglisty | ||
| T | 0 | hiorgw | ||
| T | 0 | rg | ||
| T | 0 | wrg | ||
| T | 0 | wr | ||
| T | 0 | gwr | ||
| T | 0 | g | ||
| T | 0 | wrh | ||
| T | 0 | wrh | ||
| T | 0 | wrh | ||
| T | 0 | save | ||
| T | 0 | hi | ||
| T | 0 | sg | ||
| T | 0 | sr | ||
| T | 0 | gwr | ||
| T | 0 | g | ||
| T | 0 | rwg | ||
| T | 0 | rgw | ||
| T | 0 | wr | ||
| T | 0 | lisyt | ||
| T | 0 | todo | ||
| T | 0 | blah | ||
| T | 0 | todo | ||
| T | 0 | blah | ||
| T | 0 | print |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,10 +1,169 @@ | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.Scanner; | ||
|
|
||
| public class Duke { | ||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
| } | ||
| } | ||
| private static final String FILE_PATH = "./data/duke.txt"; | ||
| private static Task[] tasks = new Task[100]; | ||
| private static int taskCount = 0; | ||
|
|
||
| public static void main(String[] args) { | ||
| // Print the welcome message | ||
| System.out.println("____________________________________________________________"); | ||
| System.out.println(" Hello! I'm Ruhi."); | ||
| System.out.println(" What can I do for you?"); | ||
| System.out.println("____________________________________________________________"); | ||
|
|
||
| // Load existing tasks from file | ||
| loadTasksFromFile(); | ||
|
|
||
| // Initialize a scanner to read user input | ||
| Scanner scanner = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
| try { | ||
| String input = scanner.nextLine(); | ||
| System.out.println("____________________________________________________________"); | ||
|
|
||
| if (input.equals("bye")) { | ||
| System.out.println(" Bye. Hope to see you again soon!"); | ||
| System.out.println("____________________________________________________________"); | ||
| break; | ||
| } else if (input.equals("list")) { | ||
| // Display the list of tasks | ||
| System.out.println(" Here are the tasks in your list:"); | ||
| for (int i = 0; i < taskCount; i++) { | ||
| System.out.println((i + 1) + "." + tasks[i]); | ||
| } | ||
| System.out.println("____________________________________________________________"); | ||
| } else if (input.startsWith("mark")) { | ||
| handleMarkCommand(input); | ||
| } else if (input.startsWith("unmark")) { | ||
| handleUnmarkCommand(input); | ||
| } else if (input.startsWith("delete")) { | ||
| handleDeleteCommand(input); | ||
| } else if (input.trim().isEmpty()) { | ||
| throw new DukeException("Oops! Task description cannot be empty."); | ||
| } else { | ||
| handleAddCommand(input); | ||
| } | ||
| } catch (DukeException e) { | ||
| System.out.println(e.getMessage()); | ||
| System.out.println("____________________________________________________________"); | ||
| } catch (Exception e) { | ||
| System.out.println("Oopsy daisy! Something went wrong, check again."); | ||
| System.out.println("____________________________________________________________"); | ||
| } | ||
| } | ||
|
|
||
| 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 extracting parts of the code the a separate method to avoid having long methods |
||
|
|
||
| // Methods for handling different commands | ||
|
|
||
| private static void handleMarkCommand(String input) throws DukeException { | ||
| try { | ||
| int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1; | ||
| if (taskIndex < 0 || taskIndex >= taskCount) { | ||
| throw new DukeException("Invalid task number. Please provide a valid task number to mark."); | ||
| } | ||
| tasks[taskIndex].markAsDone(); | ||
| System.out.println(" Nice! I've marked this task as done:"); | ||
| System.out.println(" " + tasks[taskIndex]); | ||
| saveTasksToFile(); | ||
| } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { | ||
| throw new DukeException("Please specify a valid task number after 'mark'."); | ||
| } | ||
| } | ||
|
|
||
| private static void handleUnmarkCommand(String input) throws DukeException { | ||
| try { | ||
| int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1; | ||
| if (taskIndex < 0 || taskIndex >= taskCount) { | ||
| throw new DukeException("Invalid task number. Please provide a valid task number to unmark."); | ||
| } | ||
| tasks[taskIndex].markAsNotDone(); | ||
| System.out.println(" OK, I've marked this task as not done yet:"); | ||
| System.out.println(" " + tasks[taskIndex]); | ||
| saveTasksToFile(); | ||
| } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { | ||
| throw new DukeException("Please specify a valid task number after 'unmark'."); | ||
| } | ||
| } | ||
|
|
||
| private static void handleDeleteCommand(String input) throws DukeException { | ||
| try { | ||
| int taskIndex = Integer.parseInt(input.split(" ")[1]) - 1; | ||
| if (taskIndex < 0 || taskIndex >= taskCount) { | ||
| throw new DukeException("Invalid task number. Please provide a valid task number to delete."); | ||
| } | ||
| System.out.println(" Noted. I've removed this task:"); | ||
| System.out.println(" " + tasks[taskIndex]); | ||
| for (int i = taskIndex; i < taskCount - 1; i++) { | ||
| tasks[i] = tasks[i + 1]; | ||
| } | ||
| tasks[taskCount - 1] = null; | ||
| taskCount--; | ||
| saveTasksToFile(); | ||
| System.out.println(" Now you have " + taskCount + " tasks in the list."); | ||
| } catch (ArrayIndexOutOfBoundsException | NumberFormatException e) { | ||
| throw new DukeException("Please specify a valid task number after 'delete'."); | ||
| } | ||
| } | ||
|
|
||
| private static void handleAddCommand(String input) throws DukeException { | ||
| if (input.trim().isEmpty()) { | ||
| throw new DukeException("The description of a task cannot be empty."); | ||
| } | ||
| tasks[taskCount] = new Task(input); | ||
| taskCount++; | ||
| System.out.println("added: " + input); | ||
| saveTasksToFile(); | ||
| } | ||
|
|
||
| // Load and Save Tasks | ||
| private static void loadTasksFromFile() { | ||
| try { | ||
| File file = new File(FILE_PATH); | ||
| if (!file.exists()) { | ||
| File dir = new File("./data"); | ||
| if (!dir.exists()) { | ||
| dir.mkdirs(); | ||
| } | ||
| file.createNewFile(); | ||
| } | ||
|
|
||
| Scanner fileScanner = new Scanner(file); | ||
| while (fileScanner.hasNextLine()) { | ||
| String line = fileScanner.nextLine(); | ||
| String[] parts = line.split(" \\| "); | ||
| String type = parts[0]; | ||
| boolean isDone = parts[1].equals("1"); | ||
| String description = parts[2]; | ||
| Task task = new Task(description); | ||
| if (isDone) { | ||
| task.markAsDone(); | ||
| } | ||
| tasks[taskCount] = task; | ||
| taskCount++; | ||
| } | ||
| fileScanner.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Error loading tasks from file: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| private static void saveTasksToFile() { | ||
| try { | ||
| FileWriter writer = new FileWriter(FILE_PATH); | ||
| for (int i = 0; i < taskCount; i++) { | ||
| Task task = tasks[i]; | ||
| writer.write("T | " + (task.isDone ? "1" : "0") + " | " + task.description + "\n"); | ||
|
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. Would be good to assign magic strings to a named constant instead |
||
| } | ||
| writer.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Error saving tasks to file: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| // Custom Exception for Duke-related errors | ||
| public class DukeException extends Exception { | ||
| public DukeException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,27 @@ | ||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
|
|
||
| public Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "X" : " "); // mark done task with X | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public void markAsNotDone() { | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[" + getStatusIcon() + "] " + description; | ||
| } | ||
| } | ||
|
|
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.
Is the variable name "e" informative enough?