-
Notifications
You must be signed in to change notification settings - Fork 114
[kestryix] iP #96
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?
[kestryix] iP #96
Changes from 13 commits
beabdec
d4e2c74
d9c8a22
9e3f1f9
582b04f
1b21a14
0b90f88
b4c258f
345fde9
b74b7e6
458c528
80c39e8
6775b73
254e14a
e637fcb
2d72853
26619e2
47100fd
344e3f1
665dc42
3c85d94
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 |
|---|---|---|
|
|
@@ -15,3 +15,5 @@ bin/ | |
|
|
||
| /text-ui-test/ACTUAL.TXT | ||
| text-ui-test/EXPECTED-UNIX.TXT | ||
|
|
||
| data/ | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| public class Deadline extends Task { | ||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by) { | ||
| super(description); | ||
| this.by = by; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("[D]%s (by: %s)", super.toString(), this.by); | ||
| } | ||
| } |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| 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; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("[E]%s (from: %s to: %s)", super.toString(), this.from, this.to); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,46 @@ | ||
| import java.util.HashMap; | ||
|
|
||
| public class InputParser { | ||
| public static final String COMMAND = "command"; | ||
| public static final String ARGUMENT = "argument"; | ||
|
|
||
| public static HashMap<String, String> parseCommands(String input) { | ||
| HashMap<String, String> commandArguments = new HashMap<>(); | ||
| String[] splitInput = input.split(" "); | ||
|
|
||
| // check if input is empty | ||
| if (splitInput.length == 0) { | ||
| commandArguments.put(InputParser.COMMAND, ""); | ||
| return commandArguments; | ||
| } | ||
|
|
||
| // set first element as command | ||
| commandArguments.put(InputParser.COMMAND, splitInput[0]); | ||
|
|
||
| String argumentDescription = InputParser.ARGUMENT; | ||
| StringBuilder argument = new StringBuilder(); | ||
|
|
||
| // parse remaining input | ||
| for (int i = 1; i < splitInput.length; i++) { | ||
| String arg = splitInput[i]; | ||
|
|
||
| if (arg.startsWith("/")) { | ||
| if (!argumentDescription.isEmpty()) { | ||
| commandArguments.put(argumentDescription, argument.toString().strip()); | ||
| } | ||
|
|
||
| argumentDescription = arg; | ||
| argument.setLength(0); | ||
| } else { | ||
| argument.append(" ").append(arg); | ||
| } | ||
| } | ||
|
|
||
| // add last argument | ||
| if (!argument.isEmpty()) { | ||
| commandArguments.put(argumentDescription, argument.toString().strip()); | ||
| } | ||
|
|
||
| return commandArguments; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.nio.file.Files; | ||
| import java.nio.file.Path; | ||
| import java.util.ArrayList; | ||
| import java.util.List; | ||
|
|
||
| public class SaveTaskList { | ||
| static Path directoryPath = Path.of("./data"); | ||
| static Path filePath = directoryPath.resolve("data.txt"); | ||
|
|
||
| public static void saveTasks(ArrayList<Task> taskList) { | ||
| try { | ||
| if (!Files.exists(directoryPath)) { | ||
| Files.createDirectories(directoryPath); | ||
| System.out.println("Directory created: " + directoryPath); | ||
| } | ||
|
|
||
| if (!Files.exists(filePath)) { | ||
| Files.createFile(filePath); | ||
| System.out.println("File created: " + filePath); | ||
| } | ||
|
|
||
| try (var writer = new FileWriter(filePath.toFile())) { | ||
| for (var item : taskList) { | ||
| writer.write(item + System.lineSeparator()); | ||
| } | ||
| System.out.println("Task list data saved to file: " + filePath); | ||
| } | ||
|
|
||
| } catch (IOException e) { | ||
| System.out.println("An error occurred: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
| public static List<String> loadTasks() { | ||
| List<String> taskList; | ||
|
|
||
| if (!Files.exists(directoryPath)) { | ||
| System.out.println("Data directory does not exist"); | ||
| } | ||
|
|
||
| try { | ||
| if (Files.exists(filePath)) { | ||
| taskList = Files.readAllLines(filePath); | ||
| return taskList; | ||
| } else { | ||
| System.out.println("Data file does not exist: " + filePath); | ||
| } | ||
| } catch (IOException e) { | ||
| System.out.println("An error occurred while reading the file: " + e.getMessage()); | ||
| } | ||
| return null; | ||
| } | ||
| } | ||
|
|
||
|
|
||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 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 | ||
| } | ||
|
Comment on lines
+25
to
+27
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. Maybe you can make the code more explicit here instead of using ternary if operator? |
||
|
|
||
| public void markAsDone() { | ||
| this.isDone = true; | ||
| } | ||
|
|
||
| public void markAsNotDone() { | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| public String getDescription() { | ||
| return description; | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("[%s] %s", this.getStatusIcon(), this.getDescription()); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,192 @@ | ||
| import java.util.ArrayList; | ||
| import java.util.HashMap; | ||
| import java.util.List; | ||
| import java.util.Objects; | ||
|
|
||
| public class TaskList { | ||
| public ArrayList<Task> taskList; | ||
| protected int taskIndex; | ||
| public static final int INVALID_INDEX = -1; | ||
|
|
||
| public TaskList() { | ||
| this.taskList = new ArrayList<>(); | ||
| } | ||
|
|
||
| public void addTask(Task task) { | ||
| System.out.println("--------------------------------------------"); | ||
| this.taskList.add(task); | ||
| System.out.println("Okay! I have added this task: "); | ||
| System.out.println(task.toString()); | ||
| System.out.printf("You currently have %d tasks in your list \n", taskList.size()); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void deleteTask(HashMap<String, String> commandArguments) { | ||
| String argument = commandArguments.get("argument"); | ||
| getTaskIndex(argument); | ||
| Task task = taskList.get(this.taskIndex); | ||
| System.out.println("--------------------------------------------"); | ||
| this.taskList.remove(task); | ||
| System.out.println("Okay! I have removed this task: "); | ||
| System.out.println(task.toString()); | ||
| System.out.printf("You currently have %d tasks in your list \n", taskList.size()); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void listTasks() { | ||
| System.out.println("--------------------------------------------"); | ||
| System.out.println("Here are your current tasks: "); | ||
| for (int i = 0; i < taskList.size(); i++) { | ||
| System.out.println(i + 1 + "." + this.taskList.get(i)); | ||
| } | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void addToDo(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskDescriptionException { | ||
| String argument = commandArguments.get("argument"); | ||
| if (argument == null) { | ||
| throw new TulipTaskException.InvalidTaskDescriptionException(); | ||
| } | ||
|
|
||
| ToDo task = new ToDo(commandArguments.get("argument")); | ||
| addTask(task); | ||
| } | ||
|
|
||
| public void addDeadline(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidDeadlineException, TulipTaskException.InvalidTaskDescriptionException { | ||
| String argument = commandArguments.get("argument"); | ||
| String by = commandArguments.get("/by"); | ||
|
|
||
| if (argument == null) { | ||
| throw new TulipTaskException.InvalidTaskDescriptionException(); | ||
| } | ||
|
|
||
| if (by == null) { | ||
| throw new TulipTaskException.InvalidDeadlineException(); | ||
| } | ||
|
|
||
| Deadline task = new Deadline(argument, by); | ||
| addTask(task); | ||
| } | ||
|
|
||
| public void addEvent(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskDescriptionException, TulipTaskException.InvalidStartDateException, TulipTaskException.InvalidEndDateException { | ||
| String argument = commandArguments.get("argument"); | ||
| String from = commandArguments.get("/from"); | ||
| String to = commandArguments.get("/to"); | ||
|
|
||
| if (argument == null) { | ||
| throw new TulipTaskException.InvalidTaskDescriptionException(); | ||
| } | ||
|
|
||
| if (from == null) { | ||
| throw new TulipTaskException.InvalidStartDateException(); | ||
| } | ||
|
|
||
| if (to == null) { | ||
| throw new TulipTaskException.InvalidEndDateException(); | ||
| } | ||
|
|
||
| Event task = new Event(argument, from, to); | ||
| addTask(task); | ||
| } | ||
|
|
||
| public void getTaskIndex(String indexString) { | ||
| int index = Integer.parseInt(indexString) - 1; | ||
|
|
||
| if (index < 0 || index > taskList.size() - 1) { | ||
| this.taskIndex = INVALID_INDEX; | ||
| return; | ||
| } | ||
|
|
||
| this.taskIndex = index; | ||
| } | ||
|
|
||
| public void markTaskAsDone(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskIndexException { | ||
| String argument = commandArguments.get("argument"); | ||
| getTaskIndex(argument); | ||
|
|
||
| if (this.taskIndex == INVALID_INDEX) { | ||
| throw new TulipTaskException.InvalidTaskIndexException(); | ||
| } | ||
|
|
||
| Task task = taskList.get(this.taskIndex); | ||
| task.markAsDone(); | ||
| System.out.println("--------------------------------------------"); | ||
| System.out.println("Great job! I have marked this task as done: "); | ||
| System.out.println(task); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void markTaskAsNotDone(HashMap<String, String> commandArguments) throws TulipTaskException.InvalidTaskIndexException { | ||
| String argument = commandArguments.get("argument"); | ||
| getTaskIndex(argument); | ||
|
|
||
| if (this.taskIndex == INVALID_INDEX) { | ||
| throw new TulipTaskException.InvalidTaskIndexException(); | ||
| } | ||
|
|
||
| Task task = taskList.get(this.taskIndex); | ||
| task.markAsNotDone(); | ||
| System.out.println("--------------------------------------------"); | ||
| System.out.println("Okay, I have marked this task as not done: "); | ||
| System.out.println(task); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void saveTaskToFile() { | ||
| System.out.println("--------------------------------------------"); | ||
| SaveTaskList.saveTasks(this.taskList); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void loadTaskFromFile() { | ||
| List<String> list = SaveTaskList.loadTasks(); | ||
| for (int i = 0; i < Objects.requireNonNull(list).size(); i++) { | ||
| parseTask(list.get(i)); | ||
| } | ||
| System.out.println("--------------------------------------------"); | ||
| System.out.println("Tasks have been successfully loaded!"); | ||
| System.out.println("--------------------------------------------"); | ||
| } | ||
|
|
||
| public void parseTask(String line) { | ||
| String type = line.substring(1, 2); | ||
| boolean completed = line.charAt(4) == 'X'; | ||
|
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. Maybe the naming of your boolean can be improved to sound like a boolean! |
||
|
|
||
| switch (type) { | ||
|
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. For your switch statements, maybe you could follow the style mentioned here. |
||
| case "T" -> { | ||
| String description = line.substring(7); | ||
| ToDo task = new ToDo(description); | ||
| this.taskList.add(task); | ||
| if (completed) { | ||
| task.markAsDone(); | ||
| } | ||
|
|
||
| } | ||
| case "D" -> { | ||
| int deadlineIndex = line.indexOf("(by:"); | ||
| String description = line.substring(7, deadlineIndex).trim(); | ||
| String deadline = line.substring(deadlineIndex + 5, line.length() - 1); | ||
| Deadline task = new Deadline(description, deadline); | ||
| this.taskList.add(task); | ||
| if (completed) { | ||
| task.markAsDone(); | ||
| } | ||
|
|
||
| } | ||
| case "E" -> { | ||
| int eventIndex = line.indexOf("(from:"); | ||
| String description = line.substring(7, eventIndex).trim(); | ||
| String timeInfo = line.substring(eventIndex + 6, line.length() - 1); // Extract time info | ||
|
|
||
| String[] timeParts = timeInfo.split(" to: "); | ||
| String eventStart = timeParts[0].trim(); | ||
| String eventEnd = timeParts[1].trim(); | ||
| Event task = new Event(description, eventStart, eventEnd); | ||
| this.taskList.add(task); | ||
| if (completed) { | ||
| task.markAsDone(); | ||
| } | ||
| } | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,9 @@ | ||
| public class ToDo extends Task { | ||
| public ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| public String toString() { | ||
| return String.format("[T]%s", super.toString()); | ||
| } | ||
| } |
Uh oh!
There was an error while loading. Please reload this page.
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 boolean variable name "isDone" is well-named, as it clearly indicates its purpose and state.