-
Notifications
You must be signed in to change notification settings - Fork 184
[shawnpong] iP #178
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?
[shawnpong] iP #178
Changes from 29 commits
55f9f9f
f837ddb
a6f7324
acf8fdf
623f216
46afd35
66efd52
8cb1b88
a37a35f
5195542
118c2de
0e1e695
ea1adcb
2d89f52
190535f
14cbebb
f6e276f
a2c95c2
3fd63f3
52ab77b
4894ef2
3992ded
4293c01
a67bf9b
46d6cc1
d3003ef
0c702d7
aa38640
2288a85
db336b4
c8b6a97
c6a0097
e7bc46d
19e3ebf
f45955d
02ec784
6f0f9e7
00aadd0
8d5f63a
c7abf86
bb716bf
8dc40dc
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 |
|---|---|---|
| @@ -1,29 +1,30 @@ | ||
| # User Guide | ||
| # Duke User Guide | ||
|
|
||
| ## Features | ||
| // Update the title above to match the actual product name | ||
|
|
||
| ### Feature-ABC | ||
| // Product screenshot goes here | ||
|
|
||
| Description of the feature. | ||
| // Product intro goes here | ||
|
|
||
| ### Feature-XYZ | ||
| ## Adding deadlines | ||
|
|
||
| Description of the feature. | ||
| // Describe the action and its outcome. | ||
|
|
||
| ## Usage | ||
| // Give examples of usage | ||
|
|
||
| ### `Keyword` - Describe action | ||
| Example: `keyword (optional arguments)` | ||
|
|
||
| Describe the action and its outcome. | ||
| // A description of the expected outcome goes here | ||
|
|
||
| Example of usage: | ||
| ``` | ||
| expected output | ||
| ``` | ||
|
|
||
| `keyword (optional arguments)` | ||
| ## Feature ABC | ||
|
|
||
| Expected outcome: | ||
| // Feature details | ||
|
|
||
| Description of the outcome. | ||
|
|
||
| ``` | ||
| expected output | ||
| ``` | ||
| ## Feature XYZ | ||
|
|
||
| // Feature details |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,19 @@ | ||
| public class Deadline extends Task { | ||
|
|
||
| protected String by; | ||
|
|
||
| public Deadline(String description, String by, Boolean isDone) { | ||
| super(description); | ||
| this.by = by; | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public String getBy() { | ||
| return by; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D] " + super.getStatusIcon() + " " + super.toString() + " (by: " + by + ")"; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,26 @@ | ||
| public class Events extends Task { | ||
|
|
||
| protected String from; | ||
| protected String to; | ||
|
|
||
| public Events(String description, String from, String to, Boolean isDone) { | ||
| super(description); | ||
| this.from = from; | ||
| this.to = to; | ||
| this.isDone = isDone; | ||
| } | ||
|
|
||
| public String getFrom() { | ||
| return from; | ||
| } | ||
|
|
||
| public String getTo() { | ||
| return to; | ||
| } | ||
|
|
||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E] " + super.getStatusIcon() + " " + super.toString() + " (from: " + from + " to: " + to + ")"; | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,248 @@ | ||
| 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. I like how you specified java.util.Scanner instead of java.util.* , this complies with coding standards, well done ! |
||
| import java.util.ArrayList; | ||
| import java.io.File; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
| import java.io.FileNotFoundException; | ||
|
|
||
|
|
||
| public class Floda { | ||
| private static ArrayList<Task> list = new ArrayList<>(); | ||
| private final static String NAME = "Floda"; | ||
| private static final String FILE_PATH = "./data/tasks.txt"; | ||
|
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'm not sure why the NAME constant left out the |
||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
|
shawnpong marked this conversation as resolved.
|
||
| System.out.println("Hello! I'm " + NAME); | ||
| try { | ||
| checkIfFileExists(); | ||
| readFromFile(); | ||
| } catch (FileNotFoundException | InvalidInputException e) { | ||
| System.out.println("Error: " + e.getMessage()); | ||
| } | ||
| Scanner scanner = new Scanner(System.in); | ||
| System.out.println("I can keep track of a to-do list for you! Just type what you want to add to the list."); | ||
| while (true) { | ||
| try { | ||
| String line = scanner.nextLine().trim(); | ||
| String[] parts = line.split(" ", 2); | ||
| String command = parts[0].trim(); | ||
| switch (command) { | ||
| case "bye": | ||
| System.out.println("Bye. Hope to see you again soon!"); | ||
| scanner.close(); | ||
| saveToFile(); | ||
| return; | ||
| case "list": | ||
| if (list.isEmpty()) { | ||
| System.out.println("Your to-do list is empty."); | ||
| } else { | ||
| System.out.println("List so far: "); | ||
| for (int i = 0; i < list.size(); i++) { | ||
| System.out.println((i + 1) + "." + list.get(i)); | ||
| } | ||
| } | ||
|
shawnpong marked this conversation as resolved.
Outdated
|
||
| break; | ||
| case "mark": | ||
| handleMarkTask(line); | ||
| saveToFile(); | ||
| break; | ||
| case "unmark": | ||
| handleUnmarkTask(line); | ||
| saveToFile(); | ||
| break; | ||
| case "deadline": | ||
| handleDeadlineTask(line); | ||
| saveToFile(); | ||
| break; | ||
| case "todo": | ||
| handleTodoTask(line); | ||
| saveToFile(); | ||
| break; | ||
| case "event": | ||
| handleEventTask(line); | ||
| saveToFile(); | ||
| break; | ||
| case "delete": | ||
| handleDeleteTask(line); | ||
| saveToFile(); | ||
| break; | ||
| default: | ||
| throw new InvalidInputException("Invalid input!"); | ||
| } | ||
| } catch (InvalidInputException | IOException e){ | ||
| } | ||
|
shawnpong marked this conversation as resolved.
Outdated
|
||
| } | ||
| } | ||
|
|
||
| private static void handleDeleteTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| if (taskScanner.hasNextInt()) { | ||
| int taskNumber = taskScanner.nextInt() - 1; | ||
| if (isValidTaskNumber(taskNumber)) { | ||
| System.out.println("Deleting task: " + list.get(taskNumber)); | ||
| list.remove(taskNumber); | ||
| System.out.println("Task deleted successfully!"); | ||
| } else { | ||
| throw new InvalidInputException("Invalid task number! Please check with 'list'."); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void handleMarkTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| if (taskScanner.hasNextInt()) { | ||
| int taskNumber = taskScanner.nextInt() - 1; | ||
| if (isValidTaskNumber(taskNumber)) { | ||
| list.get(taskNumber).setDone(true); | ||
| System.out.println("I have marked this task as done:\n" + list.get(taskNumber)); | ||
| } else { | ||
| throw new InvalidInputException("Invalid task number! Please check with 'list'."); | ||
| } | ||
| } else { | ||
| throw new InvalidInputException("Invalid input! Please check with 'list'."); | ||
| } | ||
| taskScanner.close(); | ||
| } | ||
|
|
||
| private static void handleUnmarkTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| if (taskScanner.hasNextInt()) { | ||
| int taskNumber = taskScanner.nextInt() - 1; | ||
| if (isValidTaskNumber(taskNumber)) { | ||
| list.get(taskNumber).setDone(false); | ||
| System.out.println("I have marked this task as not done:\n" + list.get(taskNumber)); | ||
| } else { | ||
| throw new InvalidInputException("Invalid task number! Please check with 'list'."); | ||
| } | ||
| } else { | ||
| throw new InvalidInputException("Invalid input! Please check with 'list'."); | ||
| } | ||
| taskScanner.close(); | ||
| } | ||
|
|
||
| private static void handleDeadlineTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| String remaining = taskScanner.nextLine().trim(); | ||
| int byIndex = remaining.indexOf("/by"); | ||
| if (byIndex == -1 || byIndex == 0) { | ||
| throw new InvalidInputException("Invalid input format! Use: 'deadline <description> /by <time>'"); | ||
| } | ||
| String description = remaining.substring(0, byIndex).trim(); | ||
| String by = remaining.substring(byIndex + 3).trim(); | ||
| list.add(new Deadline(description, by, false)); | ||
| System.out.println("Added: " + list.getLast() + "\nNow you have " + list.size() + " items in the list!"); | ||
| } | ||
|
|
||
| private static void handleEventTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| String remaining = taskScanner.nextLine().trim(); | ||
| int fromIndex = remaining.indexOf("/from"); | ||
| if (fromIndex == -1 || fromIndex == 0) { | ||
| throw new InvalidInputException("Invalid input format! Use: 'event <description> /from <start time> /to <end time>'"); | ||
| } | ||
| int toIndex = remaining.indexOf("/to"); | ||
| if (toIndex == -1 || toIndex <= fromIndex + 5) { | ||
| throw new InvalidInputException("Invalid input format! Use: 'event <description> /from <start time> /to <end time>'"); | ||
| } | ||
| String description = remaining.substring(0, fromIndex).trim(); | ||
| String from = remaining.substring(fromIndex + 5, toIndex).trim(); | ||
| String to = remaining.substring(toIndex + 3).trim(); | ||
| list.add(new Events(description, from, to, false)); | ||
| System.out.println("Added: " + list.getLast() + "\nNow you have " + list.size() + " items in the list!"); | ||
| } | ||
|
|
||
| private static void handleTodoTask(String line) throws InvalidInputException { | ||
| Scanner taskScanner = new Scanner(line); | ||
| taskScanner.next(); | ||
| String remaining = taskScanner.nextLine().trim(); | ||
| list.add(new ToDo(remaining, false)); | ||
| System.out.println("Added: " + list.getLast() + "\nNow you have " + list.size() + " items in the list!"); | ||
| } | ||
|
shawnpong marked this conversation as resolved.
Outdated
|
||
|
|
||
|
|
||
|
|
||
| private static boolean isValidTaskNumber(int taskNumber) { | ||
| return taskNumber >= 0 && taskNumber < list.size(); | ||
| } | ||
|
|
||
| private static void checkIfFileExists() { | ||
| File f = new File(FILE_PATH); | ||
| if (!f.exists()) { | ||
| try { | ||
| f.createNewFile(); | ||
| System.out.println("File created: " + f.getAbsolutePath()); | ||
| } catch (IOException e) { | ||
| System.out.println("Error creating file: " + e.getMessage()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| private static void readFromFile() throws FileNotFoundException, InvalidInputException { | ||
| File file = new File(FILE_PATH); | ||
| Scanner scanner = new Scanner(file); | ||
| while (scanner.hasNextLine()) { | ||
| String line = scanner.nextLine(); | ||
| parseTask(line); | ||
| } | ||
| scanner.close(); | ||
| } | ||
|
|
||
| private static void parseTask(String line) throws InvalidInputException { | ||
| String[] parts = line.split("\\|"); | ||
| if (parts.length < 3) { | ||
| throw new InvalidInputException("Invalid input format in file"); | ||
| } | ||
| String type = parts[0].trim(); | ||
| boolean isDone = parts[1].trim().equals("1"); | ||
| String description = parts[2].trim(); | ||
|
|
||
| switch (type) { | ||
| case "T": | ||
| list.add(new ToDo(description, isDone)); | ||
| break; | ||
| case "D": | ||
| if (parts.length < 4) { | ||
| throw new InvalidInputException("Invalid input format for deadline in file"); | ||
| } | ||
| String by = parts[3].trim(); | ||
| list.add(new Deadline(description, by, isDone)); | ||
| break; | ||
| case "E": | ||
| if (parts.length < 5) { | ||
| throw new InvalidInputException("Invalid input format for event in file"); | ||
| } | ||
| String from = parts[3].trim(); | ||
| String to = parts[4].trim(); | ||
| list.add(new Events(description, from, to, isDone)); | ||
| break; | ||
| default: | ||
| throw new InvalidInputException("Unknown task type in file"); | ||
| } | ||
| } | ||
|
|
||
| private static void saveToFile() throws IOException { | ||
| FileWriter fw = new FileWriter(FILE_PATH); | ||
| for (Task task : list) { | ||
| fw.write(taskToLine(task) + "\n"); | ||
| } | ||
| System.out.println("Saved to file"); | ||
| fw.close(); | ||
| } | ||
|
|
||
| private static String taskToLine(Task task) { | ||
| if (task instanceof ToDo todo) { | ||
| return "T | " + (todo.isDone() ? "1" : "0") + " | " + todo.getDescription(); | ||
| } else if (task instanceof Deadline deadline) { | ||
| return "D | " + (deadline.isDone() ? "1" : "0") + " | " + deadline.getDescription() + " | " + deadline.getBy(); | ||
| } else if (task instanceof Events event) { | ||
| return "E | " + (event.isDone() ? "1" : "0") + " | " + event.getDescription() + " | " + event.getFrom() + " | " + event.getTo(); | ||
| } | ||
| return ""; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| public class InvalidInputException extends Exception { | ||
| public InvalidInputException(String errorMessage) { | ||
| System.out.println(errorMessage); | ||
| } | ||
| } |
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.
Great use of Override annotation to signal method overriding