-
Notifications
You must be signed in to change notification settings - Fork 479
[SinhaVedant] iP #567
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?
[SinhaVedant] iP #567
Changes from 26 commits
28ad2b8
ed6d4d2
376c1ce
0732b2b
f68b343
27b6769
5893c75
c36b93a
a5b63ad
7574be3
a6c3d8e
fc4e137
6277d7a
a942795
d9b68b9
d4fd725
2c11281
3ef321f
2570bf9
5794d91
1cc9b66
296495b
d673212
cdb5f1a
7d946a4
9d019e5
ceecb31
ace54d8
eb44458
e6cc044
33deb12
095f541
0bd78ca
7d9ea7a
026c221
37ad6dd
32e34a6
0865487
9939560
a8d5455
756f992
e887e87
2776539
9f74420
ef7f6fb
e1a8c5d
3045fb4
dac21a2
b3a9246
ff5848c
d648e07
9d6be40
a7ba89a
aee1430
aaabd4b
a0078c7
5c9b926
2c0fc4c
69cedb8
9609ab4
fb64bd2
9803c2b
1e3472f
f9eb6b9
87520d1
ed11395
125f802
ec30bc1
7987ed9
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,6 @@ | ||
| T | 1 | jog | ||
| D | 0 | return book | Sunday | ||
| E | 0 | project meeting | Mon 2pm-4pm | ||
| T | 0 | fly | ||
| E | 0 | project meeting | Mon 2pm-4pm | ||
| T | 0 | sleep |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,20 @@ | ||
| package duke; | ||
|
|
||
| public class Deadline extends Task { | ||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by, char taskType) { | ||
| super(description, taskType); | ||
| this.by = by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toFileString() { | ||
| return super.toFileString() + " | " + by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,218 @@ | ||||||||||||||||||||||||
| package duke; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| import java.util.Scanner; | ||||||||||||||||||||||||
| import java.util.ArrayList; | ||||||||||||||||||||||||
| import java.io.File; | ||||||||||||||||||||||||
| import java.io.FileNotFoundException; | ||||||||||||||||||||||||
| import java.io.FileWriter; | ||||||||||||||||||||||||
| import java.io.IOException; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public class Duke { | ||||||||||||||||||||||||
|
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. It would be good to add header comments to public classes |
||||||||||||||||||||||||
| private static final String FILE_PATH = "./data/duke.txt"; | ||||||||||||||||||||||||
| private static final ArrayList<Task> taskList = new ArrayList<>(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static void main(String[] args) { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| loadPreviousTasks(FILE_PATH); | ||||||||||||||||||||||||
| } catch (FileNotFoundException e) { | ||||||||||||||||||||||||
| System.out.println("File not found"); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| Scanner scanner = new Scanner(System.in); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| // Greeting message | ||||||||||||||||||||||||
| System.out.println("Hello! I'm Sivraj"); | ||||||||||||||||||||||||
| System.out.println("What can I do for you?"); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| while (true) { | ||||||||||||||||||||||||
| String echo = scanner.nextLine(); | ||||||||||||||||||||||||
| String dashLine = "----------------------------------------"; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| if (echo.equals("bye")) { | ||||||||||||||||||||||||
| // Farewell message | ||||||||||||||||||||||||
| System.out.println("Bye. Hope to see you again soon!"); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| appendTasksToFile(taskList, FILE_PATH); | ||||||||||||||||||||||||
| } catch (IOException e) { | ||||||||||||||||||||||||
| System.out.println("Something went wrong: " + e.getMessage()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| break; | ||||||||||||||||||||||||
| } else if (echo.equals("list")) { | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Here are the tasks in your list:"); | ||||||||||||||||||||||||
| for (int i = 0; i < taskList.size(); i++) { | ||||||||||||||||||||||||
| System.out.println(" " + (i + 1) + "." + taskList.get(i)); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else if (echo.startsWith("todo")) { | ||||||||||||||||||||||||
| if (echo.length() <= 5) { | ||||||||||||||||||||||||
| throw new DukeException("The description of a todo cannot be empty."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| String description = echo.substring(5).trim(); | ||||||||||||||||||||||||
| taskList.add(new ToDo(description, 'T')); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Got it. I've added this task:"); | ||||||||||||||||||||||||
| System.out.println(" " + taskList.get(taskList.size() - 1)); | ||||||||||||||||||||||||
| System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
|
||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } else if (echo.startsWith("deadline")) { | ||||||||||||||||||||||||
| int byIndex = echo.indexOf("/by"); | ||||||||||||||||||||||||
| String description = echo.substring(9, byIndex).trim(); | ||||||||||||||||||||||||
| String by = echo.substring(byIndex + 3).trim(); | ||||||||||||||||||||||||
| taskList.add(new Deadline(description, by, 'D')); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Got it. I've added this task:"); | ||||||||||||||||||||||||
| System.out.println(" " + taskList.get(taskList.size() - 1)); | ||||||||||||||||||||||||
| System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else if (echo.startsWith("event")) { | ||||||||||||||||||||||||
| int fromIndex = echo.indexOf("/from"); | ||||||||||||||||||||||||
| int toIndex = echo.indexOf("/to"); | ||||||||||||||||||||||||
| String description = echo.substring(6, fromIndex).trim(); | ||||||||||||||||||||||||
| String from = echo.substring(fromIndex + 5, toIndex).trim(); | ||||||||||||||||||||||||
| String to = echo.substring(toIndex + 3).trim(); | ||||||||||||||||||||||||
| taskList.add(new Event(description, from, to, 'E')); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Got it. I've added this task:"); | ||||||||||||||||||||||||
| System.out.println(" " + taskList.get(taskList.size() - 1)); | ||||||||||||||||||||||||
| System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else if (echo.startsWith("mark")) { | ||||||||||||||||||||||||
| int taskIndex = Integer.parseInt(echo.substring(5).trim()) - 1; | ||||||||||||||||||||||||
| if (taskIndex >= 0 && taskIndex < taskList.size()) { | ||||||||||||||||||||||||
| taskList.get(taskIndex).markAsDone(); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Nice! I've marked this task as done:"); | ||||||||||||||||||||||||
| System.out.println(" " + taskList.get(taskIndex)); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| System.out.println("Invalid task index."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else if (echo.startsWith("unmark")) { | ||||||||||||||||||||||||
| int taskIndex = Integer.parseInt(echo.substring(7).trim()) - 1; | ||||||||||||||||||||||||
| if (taskIndex >= 0 && taskIndex < taskList.size()) { | ||||||||||||||||||||||||
| taskList.get(taskIndex).markAsNotDone(); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" OK, I've marked this task as not done yet:"); | ||||||||||||||||||||||||
| System.out.println(" " + taskList.get(taskIndex)); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| System.out.println("Invalid task index."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else if (echo.startsWith("delete")) { | ||||||||||||||||||||||||
| int taskIndex = Integer.parseInt(echo.substring(7).trim()) - 1; | ||||||||||||||||||||||||
| if (taskIndex >= 0 && taskIndex < taskList.size()) { | ||||||||||||||||||||||||
| Task removedTask = taskList.remove(taskIndex); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" Noted. I've removed this task:"); | ||||||||||||||||||||||||
| System.out.println(" " + removedTask); | ||||||||||||||||||||||||
| System.out.println(" Now you have " + taskList.size() + " tasks in the list."); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| System.out.println("Invalid task index."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| throw new DukeException("I'm sorry, but I don't know what that means :-("); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } catch (DukeException e) { | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| System.out.println(" OOPS " + e.getMessage()); | ||||||||||||||||||||||||
| System.out.println(dashLine); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| scanner.close(); | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static void appendTasksToFile(ArrayList<Task> taskList, String filePath) | ||||||||||||||||||||||||
| throws IOException { | ||||||||||||||||||||||||
| FileWriter fw = new FileWriter(filePath); | ||||||||||||||||||||||||
| for (Task task: taskList) { | ||||||||||||||||||||||||
| fw.write(task.toFileString() + System.lineSeparator()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| fw.close(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static void loadPreviousTasks(String filePath) throws FileNotFoundException { | ||||||||||||||||||||||||
| File f = new File(filePath); | ||||||||||||||||||||||||
| Scanner s = new Scanner(f); | ||||||||||||||||||||||||
| while (s.hasNext()) { | ||||||||||||||||||||||||
| String line = s.nextLine(); | ||||||||||||||||||||||||
| try { | ||||||||||||||||||||||||
| Task task = parseTaskFromLine(line); | ||||||||||||||||||||||||
| taskList.add(task); | ||||||||||||||||||||||||
| } catch (DukeException e) { | ||||||||||||||||||||||||
| System.err.println("Error parsing task from file: " + e.getMessage()); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| s.close(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| public static Task parseTaskFromLine(String line) throws DukeException { | ||||||||||||||||||||||||
| String[] parts = line.split(" \\| "); | ||||||||||||||||||||||||
| if (parts.length < 3) { | ||||||||||||||||||||||||
| throw new DukeException("Invalid data format in the file."); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| char taskType = parts[0].charAt(0); | ||||||||||||||||||||||||
| boolean isDone = parts[1].equals("1"); | ||||||||||||||||||||||||
| String description = parts[2]; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
| switch (taskType) { | ||||||||||||||||||||||||
| case 'T': | ||||||||||||||||||||||||
| ToDo toDoTask = new ToDo(description, taskType); | ||||||||||||||||||||||||
| if (isDone) { | ||||||||||||||||||||||||
| toDoTask.markAsDone(); | ||||||||||||||||||||||||
| } else { | ||||||||||||||||||||||||
| toDoTask.markAsNotDone(); | ||||||||||||||||||||||||
| } | ||||||||||||||||||||||||
| return toDoTask; | ||||||||||||||||||||||||
|
|
||||||||||||||||||||||||
|
||||||||||||||||||||||||
| switch (taskType) { | |
| case 'T': | |
| ToDo toDoTask = new ToDo(description, taskType); | |
| if (isDone) { | |
| toDoTask.markAsDone(); | |
| } else { | |
| toDoTask.markAsNotDone(); | |
| } | |
| return toDoTask; | |
| switch (taskType) { | |
| case 'T': |
If i'm not wrong, the 'case' line should not have indentation
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| package duke; | ||
|
|
||
| public class DukeException extends Exception { | ||
| public DukeException(String message) { | ||
| super(message); | ||
| } | ||
| } |
| 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, char taskType) { | ||
| super(description, taskType); | ||
| this.from = from; | ||
| this.to = to; | ||
| } | ||
|
|
||
| @Override | ||
| public String toFileString() { | ||
| return super.toFileString() + " | " + from + "-" + to; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return super.toString() + " (from: " + from + " to: " + to + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package duke; | ||
|
|
||
| public class Parser { | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package duke; | ||
|
|
||
| public class Storage { | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| package duke; | ||
|
|
||
| public class Task { | ||
| protected String description; | ||
| protected boolean isDone; | ||
| protected char taskType; | ||
|
|
||
| public Task(String description, char taskType) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| this.taskType = taskType; | ||
| } | ||
|
|
||
| public void markAsDone() { | ||
| isDone = true; | ||
| } | ||
|
|
||
| public void markAsNotDone() { | ||
| isDone = false; | ||
| } | ||
|
|
||
| public String getStatusIcon() { | ||
| return (isDone ? "[X]" : "[ ]"); | ||
| } | ||
|
|
||
| public String toFileString() { | ||
| return taskType + " | " + (isDone ? "1" : "0") + " | " + description; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[" + taskType + "]" + getStatusIcon() + " " + description; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package duke; | ||
|
|
||
| public class TaskList { | ||
| } | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package duke; | ||
|
|
||
|
|
||
| public class ToDo extends Task { | ||
| public ToDo(String description, char taskType) { | ||
| super(description, taskType); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| package duke; | ||
|
|
||
| public class Ui { | ||
| } | ||
|
|
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 believe this is a typo that might've been introduced by IntelliJ's auto-refactoring process?