-
Notifications
You must be signed in to change notification settings - Fork 114
[Mikolaj Jedrzejewski] iP #97
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
Open
mikolajed
wants to merge
42
commits into
nus-cs2113-AY2425S1:master
Choose a base branch
from
mikolajed:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
42 commits
Select commit
Hold shift + click to select a range
68c58c1
Add Gradle support
03523ec
Bump gradle and lib version
Eclipse-Dominator 81a9c53
build.gradle: Prevent generating a second JAR file
aureliony f8bb5fb
Level-0
mikolajed 4b6a8b1
Add Level-1: echoing
mikolajed 66187d7
Add Level-2: adding, listing
mikolajed e62452e
Add Level-3: marking
mikolajed 934dabd
Add minor code quality changes
mikolajed 5089664
Add new Task derived class
mikolajed 266dda4
Add Level-4: ToDos, Events, Deadlines
mikolajed 37e3f9b
Improve code quality
mikolajed 97047ec
Add automated testing
mikolajed f4594ed
Minor UI change
mikolajed c7d735b
Add Level-5: error handling
mikolajed 7aead71
Merge branch 'branch-Level-5'
mikolajed 5682555
Add A-Packages
mikolajed 73d4c22
Add Level-6: deleting
mikolajed e117036
Add Level-7: saving
mikolajed a76c9ad
Merge branch 'branch-Level-6'
mikolajed f1a9136
Merge branch 'branch-Level-7'
mikolajed bb903a4
A-MoreOOP: Add TaskList class
mikolajed 7119092
A-MoreOOP: Add Storage
mikolajed bcd72f0
Update pythia.txt
mikolajed 7cb4643
A-MoreOOP: Update Ui
mikolajed a144c7c
A-MoreOOP: Refactor Parser
mikolajed a58e2d5
Add Level-9: finding tasks
mikolajed 6702bb4
Add Level-8
mikolajed 4f34a67
Merge pull request #1 from mikolajed/branch-Level-8
mikolajed acf8c93
Merge pull request #2 from mikolajed/branch-Level-9
mikolajed 44da9b8
Add JavaDoc comments
mikolajed 2c38341
Update README.md
mikolajed ed28a9d
Update README.md
mikolajed 3b5a414
Update README.md
mikolajed 1031f84
Minor bug fix
mikolajed ede59cb
Update README.md
mikolajed cbc2fdc
Update README.md
mikolajed 2f33e97
Update README.md
mikolajed cb9105a
Update README.md
mikolajed 7762786
Update README.md
mikolajed 5e18e76
Bug fix
mikolajed e561e10
Bug fix
mikolajed a754e2e
Merge branch 'add-gradle-support'
mikolajed File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| 1 | swim | ||
| 0 | read a book |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,132 @@ | ||
| package pythia; | ||
|
|
||
| import pythia.io.Parser; | ||
| import pythia.io.IO; | ||
| import pythia.task.Task; | ||
| import pythia.task.ToDo; | ||
| import pythia.task.Deadline; | ||
| import pythia.task.Event; | ||
| import pythia.exceptions.PythiaException; | ||
| import java.util.ArrayList; | ||
| import java.io.BufferedWriter; | ||
| import java.io.FileWriter; | ||
| import java.io.IOException; | ||
|
|
||
| public class Pythia { | ||
| private static String botName = "Pythia"; | ||
| private static String dataPath = "data/pythia.txt"; | ||
| private static String logo = | ||
| "____ _ _ _ \n" + | ||
| "| _ \\ _ _| |_| |__ (_) __ _ \n" + | ||
| "| |_) | | | | __| '_ \\| |/ _` |\n" + | ||
| "| __/| |_| | |_| | | | | (_| |\n" + | ||
| "|_| \\__, |\\__|_| |_|_|\\__,_|\n" + | ||
| " |___/ "; | ||
|
|
||
| private static boolean isByeSaid = false; | ||
| private static ArrayList<Task> taskList = new ArrayList<Task>(); | ||
| private static int remainingTasks = 0; | ||
|
|
||
| public static int getNumberOfRemainingTasks() { | ||
| return remainingTasks; | ||
| } | ||
|
|
||
| public static void greet() { | ||
| String helloMsg = "Welcome, seeker. I am " + botName + ".\n" + | ||
| "What brings you here?"; | ||
| IO.printResponse(helloMsg); | ||
| } | ||
|
|
||
| public static void echo(String text) { | ||
| IO.printResponse(text); | ||
| } | ||
|
|
||
| public static void sayBye() { | ||
| String byeMsg = "Your path is set. Until we meet again."; | ||
| IO.printResponse(byeMsg); | ||
| isByeSaid = true; | ||
| } | ||
|
|
||
| public static void listTasks() { | ||
| IO.printTaskList(taskList); | ||
| } | ||
|
|
||
| public static void addTask(String taskName) { | ||
| taskList.add(new Task(taskName)); | ||
| remainingTasks++; | ||
| IO.printAddedTask("added: " + taskName); | ||
| saveTasksToTxt(); | ||
| } | ||
|
|
||
| public static void addToDo(String todoName) { | ||
| taskList.add(new ToDo(todoName)); | ||
| remainingTasks++; | ||
| IO.printAddedTask("added: " + todoName); | ||
| saveTasksToTxt(); | ||
| } | ||
|
|
||
| public static void addDeadline(String deadlineName, String dueDate) { | ||
| taskList.add(new Deadline(deadlineName, dueDate)); | ||
| remainingTasks++; | ||
| IO.printAddedTask("added: " + deadlineName); | ||
| saveTasksToTxt(); | ||
| } | ||
|
|
||
| public static void addEvent(String eventName, String startDate, String endDate) { | ||
| taskList.add(new Event(eventName, startDate, endDate)); | ||
| remainingTasks++; | ||
| IO.printAddedTask("added: " + eventName); | ||
| saveTasksToTxt(); | ||
| } | ||
|
|
||
| public static void markTask(Integer taskNumber) { | ||
| if (taskNumber <= taskList.size()) { | ||
| taskList.get(taskNumber - 1).markAsDone(); | ||
| String msg = "Nice! I've marked this task as done:\n\t" + taskList.get(taskNumber - 1).toString(); | ||
| IO.printResponse(msg); | ||
| saveTasksToTxt(); | ||
| } else { | ||
| IO.printResponse("There is no such task :("); | ||
| } | ||
| remainingTasks--; | ||
| } | ||
|
|
||
| public static void deleteTask(Integer taskNumber) { | ||
| if (taskNumber <= taskList.size()) { | ||
| String msg = "Nice! I've deleted this task:\n\t" + taskList.get(taskNumber - 1).toString(); | ||
| taskList.remove(taskNumber - 1); | ||
| IO.printResponse(msg); | ||
| } else { | ||
| IO.printResponse("There is no such task :("); | ||
| } | ||
| remainingTasks--; | ||
| } | ||
|
|
||
| public static void saveTasksToTxt() { | ||
| try (BufferedWriter writer = new BufferedWriter(new FileWriter(dataPath, false))) { | ||
| for (Task task : taskList) { | ||
| writer.write(task.toTxt()); | ||
| writer.newLine(); | ||
| } | ||
| } catch (IOException e) { | ||
| System.err.println("Error writing to file: " + e.getMessage()); | ||
| } | ||
| } | ||
|
|
||
|
|
||
| public static void main(String[] args) { | ||
| IO.init(); | ||
| greet(); | ||
| Parser parser = new Parser(); | ||
|
|
||
| while (!isByeSaid) { | ||
| try { | ||
| String request = IO.getRequest(); | ||
| parser.parse(request); | ||
| parser.execute(); | ||
| } catch (PythiaException e) { | ||
| IO.printResponse(e.getUserMessage()); | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| package pythia.exceptions; | ||
|
|
||
| public class PythiaException extends RuntimeException { | ||
| private String userMessage; | ||
| public PythiaException(String message, String userMessage) { | ||
| super(message); | ||
| this.userMessage = userMessage; | ||
| } | ||
|
|
||
| public String getUserMessage() { | ||
| return userMessage; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| package pythia.io; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.Scanner; | ||
| import pythia.task.Task; | ||
| import pythia.Pythia; | ||
|
|
||
| public class IO { | ||
| private static Scanner scanner;; | ||
|
|
||
| public static void init() { | ||
| scanner = new Scanner(System.in); | ||
| } | ||
|
|
||
| public static void printResponse(String text) { | ||
| String lineSeparator = "____________________________________________________________"; | ||
| text = lineSeparator + "\n" + text + "\n" + lineSeparator + "\n"; | ||
| String formattedText = text.replaceAll("(?m)^", "\t"); | ||
| System.out.print(formattedText); | ||
| } | ||
|
|
||
| public static String getRequest() { | ||
| return scanner.nextLine(); | ||
| } | ||
|
|
||
| public static void printAddedTask(String text) { | ||
| printResponse(text.toLowerCase()); | ||
| } | ||
|
|
||
| public static void printTaskList(ArrayList<Task> taskList) { | ||
| StringBuilder taskListString = new StringBuilder(); | ||
| for (int i = 0; i < taskList.size(); i++) { | ||
| taskListString.append(i + 1).append(". "); | ||
| taskListString.append(taskList.get(i).toString()); | ||
| taskListString.append("\n"); | ||
| } | ||
|
|
||
| int remainingTasks = Pythia.getNumberOfRemainingTasks(); | ||
| taskListString.append("Now you have ").append(remainingTasks); | ||
| if (remainingTasks == 1) { | ||
| taskListString.append(" pythia.task in the list."); | ||
| } else { | ||
| taskListString.append(" tasks in the list."); | ||
| } | ||
| printResponse(taskListString.toString()); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,171 @@ | ||
| package pythia.io; | ||
|
|
||
| import java.util.ArrayList; | ||
| import java.util.regex.Matcher; | ||
| import java.util.regex.Pattern; | ||
| import java.util.Collections; | ||
| import pythia.exceptions.PythiaException; | ||
| import pythia.Pythia; | ||
|
|
||
| public class Parser { | ||
| private String commandType = null; | ||
| private ArrayList<String> argumentList = null; | ||
| private String parsingErrorMessage = "Parsing of add command unsuccessful."; | ||
|
|
||
|
|
||
| public Parser() { | ||
|
|
||
| } | ||
|
|
||
| private String parseCommandType(String rawText) { | ||
| int spaceIndex = rawText.indexOf(' '); | ||
|
|
||
| String firstPart; | ||
| if (spaceIndex != -1) { | ||
| firstPart = rawText.substring(0, spaceIndex); | ||
| } else { | ||
| firstPart = rawText; | ||
| } | ||
| return firstPart; | ||
| } | ||
|
|
||
| private void parseBye(String rawText) {} | ||
|
|
||
| private void parseList(String rawText) {} | ||
|
|
||
| private void parseAdd(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("add\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "I am not sure what to add."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what); | ||
| } | ||
|
|
||
| private void parseMark(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("mark\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "Please specify what should I mark as done."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what); | ||
| } | ||
|
|
||
| private void parseToDo(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("todo\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "Todo should have a description."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what); | ||
| } | ||
|
|
||
| private void parseDeadline(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("deadline\\s(.+)\\s/by\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
| String byWhen = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| byWhen = matcher.group(2); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "Deadline should have description and a date."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what, byWhen); | ||
| } | ||
|
|
||
| private void parseEvent(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("event\\s(.+)\\s/from\\s(.+)\\s/to\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
| String fromWhen = ""; | ||
| String toWhen = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| fromWhen = matcher.group(2); | ||
| toWhen = matcher.group(3); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "Event should have description and from and to dates."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what, fromWhen, toWhen); | ||
| } | ||
|
|
||
| public void parseDelete(String rawText) throws PythiaException { | ||
| Pattern pattern = Pattern.compile("delete\\s(.+)"); | ||
| Matcher matcher = pattern.matcher(rawText); | ||
|
|
||
| String what = ""; | ||
|
|
||
| if (matcher.find()) { | ||
| what = matcher.group(1); | ||
| } else { | ||
| throw new PythiaException(parsingErrorMessage, "Please specify what should I delete."); | ||
| } | ||
|
|
||
| argumentList.clear(); | ||
| Collections.addAll(argumentList, what); | ||
| } | ||
|
|
||
| public void parse(String rawText) throws PythiaException { | ||
| commandType = parseCommandType(rawText); | ||
| argumentList = new ArrayList<>(); | ||
|
|
||
| switch (commandType) { | ||
|
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. Note there is no indentation for case clauses. |
||
| case "bye" -> parseBye(rawText); | ||
| case "list" -> parseList(rawText); | ||
| case "add" -> parseAdd(rawText); | ||
| case "mark" -> parseMark(rawText); | ||
| case "todo" -> parseToDo(rawText); | ||
| case "deadline" -> parseDeadline(rawText); | ||
| case "event" -> parseEvent(rawText); | ||
| case "delete" -> parseDelete(rawText); | ||
| } | ||
| } | ||
|
|
||
| public void execute() throws PythiaException { | ||
| if (commandType == null) { | ||
| throw new PythiaException(parsingErrorMessage, "Sorry, I am busy."); | ||
| } | ||
|
|
||
| switch (commandType) { | ||
| case "bye" -> Pythia.sayBye(); | ||
| case "list" -> Pythia.listTasks(); | ||
| case "add" -> Pythia.addTask(argumentList.get(0)); | ||
| case "mark" -> Pythia.markTask(Integer.parseInt(argumentList.get(0))); | ||
| case "todo" -> Pythia.addToDo(argumentList.get(0)); | ||
| case "deadline" -> Pythia.addDeadline(argumentList.get(0), argumentList.get(1)); | ||
| case "event" -> Pythia.addEvent(argumentList.get(0), argumentList.get(1), argumentList.get(2)); | ||
| case "delete" -> Pythia.deleteTask(Integer.parseInt(argumentList.get(0))); | ||
| default -> throw new PythiaException(parsingErrorMessage, "Hmm. I am not sure what you mean."); | ||
| } | ||
| } | ||
| } | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Try to minimise duplicated codes to improve code quality.