-
Notifications
You must be signed in to change notification settings - Fork 267
[Thomas Cherian] iP #274
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?
[Thomas Cherian] iP #274
Changes from 8 commits
d839859
f793428
a2a56a0
cd380db
137a3e6
850f362
3f1737f
b0d9915
57b362b
566e815
ee770fd
c30f64c
f3ca7bb
2b56999
22bf473
d95d9ba
5b61b13
dc3a89e
d71f0d8
0e3967d
7b3dca2
77c95e0
abb74db
f15a3db
91c7ff3
9458bc3
065efba
0224d85
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,17 @@ | ||
| public class Deadline extends Task{ | ||
| private final String dateTime; | ||
|
|
||
| String getDateTime() { | ||
| return dateTime; | ||
| } | ||
|
|
||
| Deadline(String description, String dateTime) { | ||
| super(description); | ||
| this.dateTime = dateTime; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[D]" + super.toString() + "(by: " + getDateTime() + ")"; | ||
| } | ||
| } | ||
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| public class Event extends Task { | ||
| private final String dateTime; | ||
|
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. final var too 😄 |
||
|
|
||
| String getDateTime() { | ||
| return dateTime; | ||
| } | ||
|
|
||
| Event(String description, String dateTime) { | ||
| super(description); | ||
| this.dateTime = dateTime; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[E]" + super.toString() + "(at: " + getDateTime() + ")"; | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| public enum Keyword { | ||
| LIST, | ||
| MARK, | ||
| UNMARK, | ||
| TODO, | ||
| DEADLINE, | ||
| EVENT, | ||
| DELETE, | ||
| BYE, | ||
| ERROR | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,229 @@ | ||
| import java.util.Scanner; | ||
| import java.util.ArrayList; | ||
|
|
||
| public class Pac { | ||
| static String logo = " ____ ___ _____\n" | ||
| + "| _ \\ / _ \\ | ___|\n" | ||
| + "| |_| | | |_| | | |\n" | ||
| + "| __/ | | | | | |___\n" | ||
| + "|_| |_| |_| |_____|\n"; | ||
| static String newline = "----------------------------------------------------"; | ||
|
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. Should the logo and newline variables be made final? And their names changed to fit the coding standard for naming constants eg: LOGO |
||
| static ArrayList<Task> tasks = new ArrayList<>(); | ||
|
|
||
| public static void main(String[] args) { | ||
|
|
||
| System.out.println("\n" + newline + "\n" + newline + "\n" + logo); | ||
| System.out.println("Hello there! I'm Pac, your very own Personal Assistant ChatBot.\nHow may I help you?"); | ||
| System.out.println(newline + "\n"); | ||
|
|
||
| Scanner sc = new Scanner(System.in); | ||
|
|
||
| while (true) { | ||
| String input = sc.nextLine(); | ||
| try { | ||
| String[] inputArray = input.split(" ", 2); | ||
| String firstword = inputArray[0].toLowerCase(); | ||
|
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. name as firstWord maybe better |
||
| Keyword keyword; | ||
|
|
||
| switch (firstword) { | ||
|
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 coding standard says that we shouldn't indent the |
||
| case "bye": | ||
| keyword = Keyword.BYE; | ||
| break; | ||
| case "list": | ||
| keyword = Keyword.LIST; | ||
| break; | ||
| case "mark": | ||
| keyword = Keyword.MARK; | ||
| break; | ||
| case "unmark": | ||
| keyword = Keyword.UNMARK; | ||
| break; | ||
| case "todo": | ||
| keyword = Keyword.TODO; | ||
| break; | ||
| case "deadline": | ||
| keyword = Keyword.DEADLINE; | ||
| break; | ||
| case "event": | ||
| keyword = Keyword.EVENT; | ||
| break; | ||
| case "delete": | ||
| keyword = Keyword.DELETE; | ||
| break; | ||
| default: | ||
| keyword = Keyword.ERROR; | ||
| break; | ||
| } | ||
|
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. can consider using a hashmap to lookup the right Keyword given a string? same functionality but may save some lines, may look slightly more readable as well, but it's up to you |
||
|
|
||
| switch (keyword) { | ||
|
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 |
||
| case BYE: | ||
| exitPac(); | ||
| break; | ||
| case LIST: | ||
| displayList(); | ||
| break; | ||
| case MARK: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("MARK should be followed by a integer."); | ||
| } | ||
| try { | ||
| markTask(Integer.parseInt(inputArray[1]) - 1); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println(newline + "\nSorry! MARK should be followed by a integer.\n" | ||
| + newline + "\n"); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.println(newline + "\nSorry! Please mention a valid task number.\n" | ||
| + newline + "\n"); | ||
| } | ||
| break; | ||
| case UNMARK: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("UNMARK should be followed by a integer."); | ||
| } | ||
| try { | ||
| unmarkTask(Integer.parseInt(inputArray[1]) - 1); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println(newline + "\nSorry! MARK should be followed by a integer.\n" | ||
| + newline + "\n"); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.println(newline + "\nSorry! Please mention a valid task number.\n" | ||
| + newline + "\n"); | ||
| } | ||
| break; | ||
| case TODO: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("Please mention a task."); | ||
| } | ||
| addToDo(inputArray[1]); | ||
| break; | ||
| case DEADLINE: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("Please mention a task."); | ||
| } | ||
| try { | ||
| addDeadline(inputArray[1]); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| System.out.println(newline + "\nSorry! Command format is incorrect.\n" | ||
| + newline + "\n"); | ||
| } | ||
| break; | ||
| case EVENT: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("Please mention a task."); | ||
| } | ||
| try { | ||
| addEvent(inputArray[1]); | ||
| } catch (ArrayIndexOutOfBoundsException e) { | ||
| System.out.println(newline + "\nSorry! Command format is incorrect.\n" | ||
| + newline + "\n"); | ||
| } | ||
| break; | ||
| case DELETE: | ||
| if (inputArray.length == 1) { | ||
| throw new PacException("DELETE should be followed by a integer."); | ||
| } | ||
| try { | ||
| deleteTask(Integer.parseInt(inputArray[1]) - 1); | ||
| } catch (NumberFormatException e) { | ||
| System.out.println(newline + "\nSorry! MARK should be followed by a integer.\n" | ||
| + newline + "\n"); | ||
| } catch (IndexOutOfBoundsException e) { | ||
| System.out.println(newline + "\nSorry! Please mention a valid task number.\n" | ||
| + newline + "\n"); | ||
| } | ||
| break; | ||
| case ERROR: | ||
| throw new PacException("I don't know what this means"); | ||
| } | ||
|
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. can consider rewriting these lines of code into separate functions to avoid deep nesting |
||
| } catch (PacException e) { | ||
| System.out.println(newline + "\n" + e.getMessage() + "\n" + newline + "\n"); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| public static void addToDo(String description) { | ||
| try { | ||
| if (description.isBlank()) { | ||
| throw new PacException("Please mention a task."); | ||
| } | ||
| Task task = new ToDo(description); | ||
| tasks.add(task); | ||
| System.out.println(newline + "\nadded: " + task.toString()); | ||
| System.out.println("You have " + tasks.size() + " tasks in your list" + "\n" + newline + "\n"); | ||
| } catch (PacException e) { | ||
| System.out.println(newline + "\n" + e.getMessage() + "\n" + newline + "\n"); | ||
| } | ||
| } | ||
|
|
||
| public static void addDeadline(String input) { | ||
| try { | ||
| String[] inputArray = input.split("/"); | ||
| inputArray[1] = inputArray[1].replaceFirst("BY ", "by "); | ||
| inputArray[1] = inputArray[1].replaceFirst("By ", "by "); | ||
| inputArray[1] = inputArray[1].replaceFirst("bY ", "by "); | ||
| inputArray[1] = inputArray[1].split("by ", 2)[1]; | ||
| if (inputArray[1].isBlank()) { | ||
| throw new PacException("Please enter a valid date or time."); | ||
| } | ||
| Task task = new Deadline(inputArray[0], inputArray[1]); | ||
| tasks.add(task); | ||
| System.out.println(newline + "\nadded: " + task.toString()); | ||
| System.out.println("You have " + tasks.size() + " tasks in your list" + "\n" + newline + "\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. Maybe you can add a few blank lines in this code block to make it more readable? The coding standard suggest that "Logical units within a block should be separated by one blank line." Perhaps you could add blank lines to separate the code into sections dealing with formatting, processing and output. |
||
| } catch (PacException e) { | ||
| System.out.println(newline + "\n" + e.getMessage() + "\n" + newline + "\n"); | ||
| } | ||
| } | ||
|
|
||
| public static void addEvent(String input) { | ||
| try { | ||
| String[] inputArray = input.split("/"); | ||
| inputArray[1] = inputArray[1].replaceFirst("AT ", "by "); | ||
| inputArray[1] = inputArray[1].replaceFirst("At ", "by "); | ||
| inputArray[1] = inputArray[1].replaceFirst("aT ", "by "); | ||
| inputArray[1] = inputArray[1].split("at ", 2)[1]; | ||
| if (inputArray[1].isBlank()) { | ||
| throw new PacException("Please enter a valid date or time."); | ||
| } | ||
| Task task = new Event(inputArray[0], inputArray[1]); | ||
| tasks.add(task); | ||
| System.out.println(newline + "\nadded: " + task.toString()); | ||
| System.out.println("You have " + tasks.size() + " tasks in your list" + "\n" + newline + "\n"); | ||
| } catch (PacException e) { | ||
| System.out.println(newline + "\n" + e.getMessage() + "\n" + newline + "\n"); | ||
| } | ||
| } | ||
|
|
||
| public static void markTask(int index) { | ||
| tasks.get(index).markAsDone(); | ||
| System.out.println(newline); | ||
| System.out.println("Task is marked as done. \n" + tasks.get(index)); | ||
| System.out.println(newline + "\n"); | ||
| } | ||
|
|
||
| public static void unmarkTask(int index) { | ||
| tasks.get(index).markAsNotDone(); | ||
| System.out.println(newline); | ||
| System.out.println("Task is marked as not done. \n" + tasks.get(index)); | ||
| System.out.println(newline + "\n"); | ||
| } | ||
|
|
||
| public static void deleteTask(int index) { | ||
| System.out.println(newline + "\nTask has been deleted: " + tasks.get(index)); | ||
| tasks.remove(index); | ||
| System.out.println("You have " + tasks.size() + " tasks in your list" + "\n" + newline + "\n"); | ||
| } | ||
|
|
||
| public static void displayList() { | ||
| int i = 1; | ||
| System.out.println(newline); | ||
| for (Task task : tasks) { | ||
| System.out.println(i++ +". " + task);; | ||
| } | ||
| System.out.println(newline + "\n"); | ||
| } | ||
|
|
||
| public static void exitPac() { | ||
| System.out.println(newline + "\n" + "Goodbye! See you soon. :)\n" + newline); | ||
| System.exit(0); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class PacException extends Exception{ | ||
| PacException(String message) { | ||
| super(message); | ||
| } | ||
|
|
||
| @Override | ||
| public String getMessage() { | ||
| return "Sorry! " + super.getMessage(); | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| public class Task { | ||
| private final String description; | ||
| private boolean isDone; | ||
|
|
||
| Task(String description) { | ||
| this.description = description; | ||
| this.isDone = false; | ||
| } | ||
|
|
||
| boolean markAsDone() { | ||
| this.isDone = true; | ||
| return this.isDone; | ||
| } | ||
|
|
||
| boolean markAsNotDone() { | ||
| this.isDone = false; | ||
| return this.isDone; | ||
| } | ||
|
|
||
| String getDescription() { | ||
| return this.description; | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| if (this.isDone) { | ||
| return "[X] " + this.description; | ||
| } else { | ||
| return "[ ] " + this.description; | ||
| } | ||
| } | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,10 @@ | ||
| public class ToDo extends Task { | ||
| ToDo(String description) { | ||
| super(description); | ||
| } | ||
|
|
||
| @Override | ||
| public String toString() { | ||
| return "[T]" + super.toString(); | ||
| } | ||
| } |
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.
Var declares as final may need to be in all capital letters, eg: DATE_TIME