-
Notifications
You must be signed in to change notification settings - Fork 191
[Edwin] iP #191
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?
[Edwin] iP #191
Changes from 23 commits
bcf20ee
2c2a310
da7c42b
7e2f13a
0b9ce39
074899d
4ce8ac0
c2a8cf3
e61f1e7
daef42c
3fc7b81
ed20d64
e8ce66f
acfd97c
88ad15e
d3cbc29
b573d4c
3974bd3
22af079
3c647fc
a294af8
86a9032
c3af9c5
92cbe7d
ea44036
d57c1a3
a2984d6
4a890e0
8a47b70
5217ca0
d3eb8f0
468e2f1
9c9496a
0f9c8b7
788449d
e8616f6
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 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <classpath> | ||
| <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/> | ||
| <classpathentry kind="src" path="src/main/java"/> | ||
| <classpathentry kind="output" path="out/production/ip"/> | ||
| </classpath> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| <?xml version="1.0" encoding="UTF-8"?> | ||
| <projectDescription> | ||
| <name>ip</name> | ||
| <comment></comment> | ||
| <projects> | ||
| </projects> | ||
| <buildSpec> | ||
| <buildCommand> | ||
| <name>org.eclipse.jdt.core.javabuilder</name> | ||
| <arguments> | ||
| </arguments> | ||
| </buildCommand> | ||
| </buildSpec> | ||
| <natures> | ||
| <nature>org.eclipse.jdt.core.javanature</nature> | ||
| </natures> | ||
| </projectDescription> |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| D/0/cs10102/monday | ||
| E/1/cs2020/com | ||
| T/0/cs1010 |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| Manifest-Version: 1.0 | ||
| Main-Class: duke.Duke | ||
|
|
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,210 @@ | ||
| package duke; | ||
|
|
||
| import duke.command.CommandList; | ||
| import duke.task.Task; | ||
|
|
||
| import java.io.File; | ||
| import java.io.FileNotFoundException; | ||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
|
|
||
|
|
||
| public class Duke { | ||
| private static final int FOUR = 4; | ||
| private static final int FIVE = 5; | ||
| private static final int SIX = 6;; | ||
| private static final int EIGHT = 8; | ||
| private static final int NINE = 9; | ||
| private static final int ERROR_TODO_IS_EMPTY = 1; | ||
| private static final int ERROR_EVENT_IS_EMPTY = 2; | ||
| private static final int ERROR_DEADLINE_IS_EMPTY = 3; | ||
| private static final int ERROR_COMMAND_NOT_FOUND = 4; | ||
| private static final int ERROR_IS_INVALID = 5; | ||
| private static final int CMD_NOT_FOUND = 0; | ||
| private static final int CMD_TODO = 1; | ||
| private static final int CMD_EVENT = 2; | ||
| private static final int CMD_DEADLINE = 3; | ||
| private static final int CMD_LIST = 4; | ||
| private static final int CMD_DONE = 5; | ||
| private static final int CMD_TERMINATE = 6; | ||
| private static final int CMD_DELETE = 7; | ||
| private static final String LIST = "list"; | ||
| private static final String TODO = "todo"; | ||
| private static final String EVENT = "event"; | ||
| private static final String DEADLINE = "deadline"; | ||
| private static final String DONE = "done"; | ||
| private static final String BYE = "bye"; | ||
| private static final String DELETE = "delete"; | ||
| private static final String BY = "/by"; | ||
| private static final String AT = "/at"; | ||
| private static final ArrayList<Task> items = new ArrayList<>(); | ||
| private static final String fileDir = "./data"; | ||
| private static final String fileName = "./data/save.txt"; | ||
| private static final String logo = | ||
| " _____ ___ _____ ______\n" | ||
| + "|___ | | | |_____| / / -- \\ \\ \n" | ||
| + " / / | | / / | | | | \n" | ||
| + " / / | | / / | | | |\n" | ||
| + " / /___ | | /_/__ | | -- | |\n" | ||
| + "|_____| | | |_____| \\ \\____/ /\n"; | ||
| private static final String border = "____________________________________________________________\n"; | ||
|
|
||
|
|
||
| public static void printStartMessage() { | ||
| System.out.println(logo); | ||
| System.out.println(border + "Hi bro, my name is Echo"); | ||
| System.out.println("What do you want?\n" + border); | ||
| System.out.println("Type bye to exit\n" + border); | ||
| } | ||
| public static boolean isInvalid(CommandList task, String line , String key) { | ||
| if (!line.split(key)[1].trim().isEmpty()) { | ||
|
||
| if (Integer.parseInt(line.split(key)[1].trim()) > task.getTaskCount()) { | ||
| return true; | ||
| } | ||
| } | ||
| return (line.length() <= FIVE); | ||
| } | ||
| private static void createSave() { | ||
| File saveFile = new File(Duke.fileName); | ||
| File saveDir = new File(Duke.fileDir); | ||
| if (saveDir.mkdirs()) { | ||
| System.out.println("Successfully created save dir"); | ||
| } else { | ||
| System.out.println("Save folder already exists."); | ||
| } | ||
| try { | ||
| if (saveFile.createNewFile()) { | ||
| System.out.println("Successfully created save file"); | ||
| } else { | ||
| System.out.println("Save file already exists"); | ||
| } | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
|
||
| } | ||
| } | ||
|
|
||
| private static void updateSave() { | ||
| Task[] saveLists = new Task[items.size()]; | ||
| items.toArray(saveLists); | ||
| try { | ||
| FileWriter fw = new FileWriter(Duke.fileName); | ||
| for (Task saveList : saveLists) { | ||
| fw.write(saveList.toString().charAt(1) + "/"); | ||
| fw.write(saveList.getStatus() + "/" + saveList.getDescription()); | ||
|
||
| if (!saveList.getDate().equals("empty")) { | ||
| fw.write("/" + saveList.getDate()); | ||
| } | ||
| fw.write("\n"); | ||
| } | ||
| System.out.println("Automatically saved to " + fileName); | ||
| fw.close(); | ||
| } catch (IOException e) { | ||
| e.printStackTrace(); | ||
| } | ||
| } | ||
|
|
||
| private static void readSave(CommandList task) throws FileNotFoundException { | ||
|
||
| File saveFile = new File(Duke.fileName); | ||
| Scanner s = new Scanner(saveFile); | ||
| while (s.hasNext()) { | ||
| String[] chars = s.nextLine().split("/"); | ||
| String type = chars[0].trim(); | ||
| String status = chars[1].trim(); | ||
| String description = chars[2].trim(); | ||
| switch (type) { | ||
| case "E": | ||
| String date = chars[3].trim(); | ||
| task.addEvent(Duke.items, description, date); | ||
| if (status.equals("1")) { | ||
| items.get(task.getTaskCount()).markDone(); | ||
| } | ||
| break; | ||
| case "D": | ||
| date = chars[3].trim(); | ||
| task.addDeadline(Duke.items, description, date); | ||
| if (status.equals("1")) { | ||
| items.get(task.getTaskCount()).markDone(); | ||
| } | ||
| break; | ||
| case "T": | ||
| task.addTodo(Duke.items, description); | ||
| break; | ||
| } | ||
| } | ||
| task.loadTaskCount(items); | ||
| System.out.println("Save file successfully loaded"); | ||
| } | ||
|
|
||
| public static String readCommand(Scanner in, CommandList task, DukeException error) { | ||
|
||
| String line; | ||
| do { | ||
| line = in.nextLine(); | ||
| if (line.matches(LIST)) { | ||
| task.setCommand(CMD_LIST); | ||
| task.executeCommand(items, error, line); | ||
| System.out.println(border); | ||
| } else if (line.length() > FOUR && line.substring(0, FOUR).contains(DONE)) { | ||
| if (isInvalid(task, line, DONE)) { | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| error.setErrorType(ERROR_IS_INVALID); | ||
| } else { | ||
| task.setCommand(CMD_DONE); | ||
| } | ||
| task.executeCommand(items, error, line); | ||
| } else if (line.length() >= FOUR && line.substring(0, FOUR).contains(TODO)) { | ||
| if (line.length() <= FIVE) { | ||
| error.setErrorType(ERROR_TODO_IS_EMPTY); | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| } else { | ||
| task.setCommand(CMD_TODO); | ||
| } | ||
| task.executeCommand(items, error, line); | ||
| } else if (line.length() >= FIVE && line.substring(0, FIVE).contains(EVENT)) { | ||
| if (line.length() > SIX && line.contains(AT)) { | ||
| task.setCommand(CMD_EVENT); | ||
| } else { | ||
| error.setErrorType(ERROR_EVENT_IS_EMPTY); | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| } | ||
| task.executeCommand(items, error, line); | ||
| } else if (line.length() >= EIGHT && line.substring(0, EIGHT).contains(DEADLINE)) { | ||
| if (line.length() >= NINE && line.contains(BY)) { | ||
| task.setCommand(CMD_DEADLINE); | ||
| } else { | ||
| error.setErrorType(ERROR_DEADLINE_IS_EMPTY); | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| } | ||
| task.executeCommand(items, error, line); | ||
| } else if (line.length() >= SIX && line.substring(0, SIX).contains(DELETE)) { | ||
| if (isInvalid(task, line,DELETE)) { | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| error.setErrorType(ERROR_IS_INVALID); | ||
| } else { | ||
| task.setCommand(CMD_DELETE); | ||
| } | ||
| task.executeCommand(items, error, line); | ||
| } else if (!line.matches(BYE)) { | ||
| error.setErrorType(ERROR_COMMAND_NOT_FOUND); | ||
| task.setCommand(CMD_NOT_FOUND); | ||
| task.executeCommand(items, error, line); | ||
| } | ||
| } while (!line.matches(BYE)); | ||
| return line; | ||
| } | ||
|
|
||
| public static void main(String[] args) throws IOException { | ||
| Scanner in = new Scanner(System.in); | ||
| CommandList task = new CommandList(); | ||
| DukeException error = new DukeException(); | ||
| String line; | ||
| createSave(); | ||
| readSave(task); | ||
| printStartMessage(); | ||
| line = readCommand(in, task, error); | ||
| updateSave(); | ||
| task.setCommand(CMD_TERMINATE); | ||
| task.executeCommand(items, error, line); | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,59 @@ | ||
| package duke; | ||
|
|
||
| public class DukeException { | ||
| protected int errorType; | ||
| private static final int ERROR_TODO_IS_EMPTY = 1; | ||
| private static final int ERROR_EVENT_IS_EMPTY = 2; | ||
| private static final int ERROR_DEADLINE_IS_EMPTY = 3; | ||
| private static final int ERROR_COMMAND_NOT_FOUND = 4; | ||
| private static final int ERROR_IS_INVALID = 5; | ||
|
|
||
| private static final String border = "____________________________________________________________\n"; | ||
|
|
||
| public static void printEmptyDescriptionErrorMessage(String command) { | ||
| System.out.println(border); | ||
| System.out.println("\uD83D\uDE00 " + "OOPS!!! The description of a " + command + " cannot be empty."); | ||
| System.out.println(border); | ||
| } | ||
| public static void printCommandMismatchErrorMessage() { | ||
| System.out.println(border); | ||
| System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, but I don't know what that means :-("); | ||
| System.out.println(border); | ||
| } | ||
| public static void printCommandIsInvalid() { | ||
| System.out.println(border); | ||
| System.out.println("\uD83D\uDE00 " + "OOPS!!! I'm sorry, the command is invalid."); | ||
| System.out.println(border); | ||
| } | ||
| public DukeException() { | ||
| this.errorType = ERROR_COMMAND_NOT_FOUND; | ||
| } | ||
| public void setErrorType(int type) { | ||
| this.errorType = type; | ||
| } | ||
| public int getErrorType() { | ||
| return errorType; | ||
| } | ||
| public void printError(int type) { | ||
| switch (type) { | ||
| case ERROR_TODO_IS_EMPTY: | ||
| printEmptyDescriptionErrorMessage("todo"); | ||
| break; | ||
| case ERROR_EVENT_IS_EMPTY: | ||
| printEmptyDescriptionErrorMessage("event"); | ||
| break; | ||
| case ERROR_DEADLINE_IS_EMPTY: | ||
| printEmptyDescriptionErrorMessage("deadline"); | ||
| break; | ||
| case ERROR_IS_INVALID: | ||
| printCommandIsInvalid(); | ||
| break; | ||
| default: | ||
| printCommandMismatchErrorMessage(); | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| } | ||
|
|
||
|
|
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.
These string constants to refer to an integer may seem redundant and self-explanatory.
If you are working to replace them with magic numbers I would suggest more meaningful names.