-
Notifications
You must be signed in to change notification settings - Fork 114
[Qin Kai] iP #98
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?
[Qin Kai] iP #98
Changes from 12 commits
517cbb7
1a784d3
2585910
1e3dbd6
94390c0
77151b3
c6668cd
03e144a
8940ee6
3dcb73b
5046611
fe57d16
f74f207
72a5578
79ca35c
0098143
8216257
df24974
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,24 @@ | ||
| public class Deadline extends Task { | ||
| protected String by; | ||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
| public void setBy(String by) { | ||
| this.by = by; | ||
| } | ||
| public String getBy() { | ||
| return by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + currentStatus() + " " + description + " (by: " + by + ")"; | ||
| } | ||
|
|
||
| @Override | ||
| public String toSave() { | ||
| return "D |" + super.toSave() + " | " + by; | ||
| } | ||
|
|
||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,23 @@ | ||
| 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 toString() { | ||
| return "[E]" + currentStatus() + " " + description + " (from: " + from + " to: " + to + ")"; | ||
|
|
||
| } | ||
|
|
||
| @Override | ||
| public String toSave() { | ||
| return "E |" + super.toSave() + " | " + from + " | " + to; | ||
| } | ||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,38 @@ | ||
| 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 String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public String currentStatus() { | ||
| return (isDone ? "[X]" : "[ ]"); | ||
| } | ||
|
|
||
| public String toSave() { | ||
| return (isDone ? "1" : "0") + " | " + description; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return currentStatus() + " " + description; | ||
| } | ||
|
|
||
|
|
||
|
|
||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Todo extends Task { | ||
|
|
||
| public Todo(String task) { | ||
| super(task); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String toSave() { | ||
| return "T |" + super.toSave() + " | "; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,215 @@ | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
| import java.io.*; | ||
|
|
||
| public class bro { | ||
|
|
||
| private static final ArrayList<Task> storer = new ArrayList<>(); | ||
| private static final String FILE_PATH = "data/duke.txt"; | ||
|
|
||
| public static void level0() { | ||
|
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. Did bro hard code level 0 ? 😭 Did not remove redundant function again. |
||
| System.out.println("Hello! I'm bro"); | ||
| System.out.println("What can I do for you?"); | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
|
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. This line should not be here? The chatbot prints Bye almost instantly 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. Bye should not be in level 0 |
||
| } | ||
|
|
||
| public static void saveTasks() { | ||
| try { | ||
| File file = new File(FILE_PATH); | ||
| file.getParentFile().mkdirs(); | ||
| FileWriter writer = new FileWriter(file); | ||
|
|
||
| for (Task task : storer) { | ||
| writer.write(task.toSave() + System.lineSeparator()); | ||
| } | ||
| writer.close(); | ||
| } catch (IOException e) { | ||
| System.out.println("Error while saving tasks: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public static void loadTasks() { | ||
| File file = new File(FILE_PATH); | ||
| if (!file.exists()) { | ||
| System.out.println("No previous tasks found. Please create a new task list."); | ||
| return; | ||
| } | ||
|
|
||
| try (Scanner scanner = new Scanner(file)) { | ||
| while (scanner.hasNext()) { | ||
| String line = scanner.nextLine(); | ||
| String[] parts = line.split(" \\| "); | ||
| switch(parts[0]) { | ||
| case "T": | ||
| Todo todo = new Todo(parts[2]); | ||
| if (parts[1].equals("1")) { | ||
| todo.markAsDone(); | ||
| } | ||
| storer.add(todo); | ||
| break; | ||
| case "D": | ||
| Deadline ddl = new Deadline(parts[2], parts[3]); | ||
|
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. Variables like ddl, taskInt, and task_num can be more descriptive. For instance, use deadlineTask instead of ddl, and taskIndex instead of taskInt. Consistency in naming conventions (camelCase) is also important |
||
| if (parts[1].equals("1")) { | ||
| ddl.markAsDone(); | ||
| } | ||
| storer.add(ddl); | ||
| break; | ||
| case "E": | ||
| Event event = new Event(parts[2], parts[3], parts[4]); | ||
| if (parts[1].equals("1")) { | ||
| event.markAsDone(); | ||
| } | ||
| storer.add(event); | ||
| break; | ||
| } | ||
| } | ||
| } catch (FileNotFoundException e) { | ||
| System.out.println("Error while loading tasks: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public static void echo() { | ||
|
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. Did not remove redundant function that is no longer used. |
||
|
|
||
|
|
||
| String line; | ||
| Scanner in = new Scanner(System.in); | ||
| while (true) { | ||
| line = in.nextLine(); | ||
|
|
||
| if (line.equals("Bye")) { | ||
| break; | ||
| } | ||
| System.out.println(line); | ||
| } | ||
|
|
||
| System.out.println("Bye. Hope to see you again soon!"); | ||
|
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. This line is redundant. Needs to be displayed only when 'bye' is entered by user. |
||
|
|
||
| } | ||
|
|
||
| public static void addList() { | ||
|
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. Use descriptive names for your methods. For example, addList could be renamed to manageTasks, as it implies that this method is responsible for task management 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 addList method is quite long. Break it down into smaller methods for each task type handling to improve readability and maintainability. |
||
|
|
||
| loadTasks(); | ||
|
|
||
| String line; | ||
| Scanner in = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
|
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. Wait if you have while(true) loop in your addList and you call your mark only after addList, wouldnt you be marking after the user inputs bye. |
||
| line = in.nextLine(); | ||
|
|
||
| if (line.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. Avoid using magic strings directly in your code (e.g., "Bye", "list", "todo"). Instead, consider defining constants at the beginning of your class to improve readability and maintainability. |
||
| break; | ||
|
|
||
| } else if (line.equals("list")) { | ||
|
|
||
|
|
||
| for (int i = 0; i < storer.size(); i++) { | ||
| System.out.println((i + 1) + ". " + storer.get(i)); | ||
| } | ||
|
|
||
| } else if (line.startsWith("todo")) { | ||
|
|
||
| if (line.trim().length() <= 4) { | ||
| System.out.println("Description of a todo cannot be empty. Please provide a task description."); | ||
| } else { | ||
| String description = line.substring(5); | ||
| Todo todo = new Todo(description); | ||
| storer.add(todo); | ||
| System.out.println("Got it. I've added this task\n " + todo); | ||
| System.out.println("Now you have " + storer.size() + " tasks in the list."); | ||
| saveTasks(); | ||
| } | ||
|
|
||
| } else if (line.startsWith("deadline")) { | ||
|
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 sections handling different task types (Todo, Deadline, Event) have similar patterns for adding and saving tasks. Can create a helper method to reduce duplication |
||
| try { | ||
| String[] infos = line.substring(9).split(" /by "); | ||
| if (infos.length < 2) { | ||
| throw new Exception("Deadline description / date missing."); | ||
| } | ||
| Deadline deadline = new Deadline(infos[0], infos[1]); | ||
| storer.add(deadline); | ||
| System.out.println("Got it. I've added this task\n " + deadline); | ||
| System.out.println("Now you have " + storer.size() + " tasks in the list."); | ||
| saveTasks(); | ||
| } catch (Exception e){ | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
| } else if (line.startsWith("event")) { | ||
| try { | ||
| String[] infos = line.substring(6).split(" /from | /to "); | ||
| if (infos.length < 3) { | ||
| throw new Exception("Event description / date missing."); | ||
| } | ||
| Event event = new Event(infos[0], infos[1], infos[2]); | ||
| storer.add(event); | ||
| System.out.println("Got it. I've added this task\n " + event); | ||
| System.out.println("Now you have " + storer.size() + " tasks in the list."); | ||
| saveTasks(); | ||
|
|
||
| } catch (Exception e){ | ||
| System.out.println(e.getMessage()); | ||
| } | ||
|
|
||
| } else if (line.startsWith("delete")){ | ||
| try { | ||
| int taskInt = Integer.parseInt(line.split(" ")[1]) - 1; | ||
| if (taskInt < 0 || taskInt >= storer.size()) { | ||
| throw new Exception("Invalid task number."); | ||
| } | ||
|
|
||
| Task tasktoRemove = storer.remove(taskInt); | ||
| System.out.println("I've removed this task for you: "); | ||
| System.out.println(tasktoRemove); | ||
| System.out.println("Now you have " + storer.size() + " tasks in the list."); | ||
| saveTasks(); | ||
|
|
||
| } catch (Exception e){ | ||
| System.out.println(("Error: " + e.getMessage())); | ||
| } | ||
|
|
||
| } else if (line.startsWith("mark")) { | ||
|
|
||
| int task_num = Integer.parseInt(line.split(" ")[1]) - 1; | ||
| Task task = storer.get(task_num); | ||
| task.markAsDone(); | ||
| System.out.println("Nice! I've marked this task as done:"); | ||
| System.out.println(storer.get(task_num)); | ||
| saveTasks(); | ||
|
|
||
| } else if (line.startsWith("unmark")) { | ||
| int task_num = Integer.parseInt(line.split(" ")[1]) - 1; | ||
| Task task = storer.get(task_num); | ||
| task.markAsUndone(); | ||
| System.out.println("Ok, I've marked this task as not done yet:"); | ||
| System.out.println(storer.get(task_num)); | ||
| saveTasks(); | ||
| } else { | ||
| storer.add(new Task(line)); | ||
| saveTasks(); | ||
| } | ||
|
|
||
| } | ||
|
|
||
| System.out.println("Bye. Hope to see you again soon!"); | ||
|
|
||
|
|
||
| } | ||
|
|
||
| public static void main(String[] args) { | ||
| String logo = " ____ _ \n" | ||
| + "| _ \\ _ _| | _____ \n" | ||
| + "| | | | | | | |/ / _ \\\n" | ||
| + "| |_| | |_| | < __/\n" | ||
| + "|____/ \\__,_|_|\\_\\___|\n"; | ||
| System.out.println("Hello from\n" + logo); | ||
|
|
||
| // level0(); | ||
| // echo(); | ||
| addList(); | ||
| // mark(); | ||
|
|
||
| } | ||
| } | ||
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.
The class name bro should follow Java naming conventions. Use PascalCase for class names. Consider renaming it to something more descriptive, like TaskManager or TaskApp.