From e6ebc11db72ca1a2db6538ba83ed510d42e542af Mon Sep 17 00:00:00 2001 From: Ruth Lim <110249543+ruth-lim@users.noreply.github.com> Date: Mon, 12 Aug 2024 21:35:37 +0800 Subject: [PATCH 1/2] Added answers for conclusion --- tutorials/ab3TracingCode.md | 217 ++++++++++++++++++++++++++++++++++-- 1 file changed, 205 insertions(+), 12 deletions(-) diff --git a/tutorials/ab3TracingCode.md b/tutorials/ab3TracingCode.md index f5ce945d..553b599f 100644 --- a/tutorials/ab3TracingCode.md +++ b/tutorials/ab3TracingCode.md @@ -281,31 +281,224 @@ Here are some quick questions you can try to answer based on your execution path instead? What exceptions do you think will be thrown (if any), where will the exceptions be thrown and where will they be handled? - 1. `redit 1 n/Alice Yu` + - 2. `edit 0 n/Alice Yu` +* Exception Thrown: ParseException +* Reason: Unknown command as the command word `redit` is not recognized. +* Where the exception is thrown: `AddressBookParser#parseCommand()` - 3. `edit 1 n/Alex Yeoh` + ```java {.line-numbers highlight-lines="7"} + public Command parseCommand(String userInput) throws ParseException { + ... + switch (commandWord) { + ... + default: + logger.finer("This user input caused a ParseException: " + userInput); + throw new ParseException(MESSAGE_UNKNOWN_COMMAND); + } + } + ``` + +* Where the exception is handled: `MainWindow#executeCommand()` + + ```java {.line-numbers highlight-lines="3"} + private CommandResult executeCommand(String commandText) throws CommandException, ParseException { + ... + } catch (CommandException | ParseException e) { + logger.info("An error occurred while executing command: " + commandText); + resultDisplay.setFeedbackToUser(e.getMessage()); + throw e; + } + } + ``` + + + + +* Exception Thrown: ParseException +* Reason: Invalid command format as index 0 is not a non-zero unsigned integer. +* Where the exception is thrown: `EditCommandParser#parse()` + + ```java {.line-numbers highlight-lines="6"} + public EditCommand parse(String args) throws ParseException { + ... + try { + index = ParserUtil.parseIndex(argMultimap.getPreamble()); + } catch (ParseException pe) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE), pe); + } + ... + } + ``` + +* Where the exception is handled: `MainWindow#executeCommand()` + - 4. `edit 1` + - 5. `edit 1 n/アリス ユー` +* No exception thrown. +* Reason: "1" is a valid index in the person list. The command is correctly formatted and will edit the name of the person at index 1 to "Alex Yeoh". + - 6. `edit 1 t/one t/two t/three t/one` + + +* Exception Thrown: ParseException +* Reason: At least one field to edit must be provided. +* Where the exception is thrown: `EditCommandParser#parse()` + + ```java {.line-numbers highlight-lines="4"} + public EditCommand parse(String args) throws ParseException { + ... + if (!editPersonDescriptor.isAnyFieldEdited()) { + throw new ParseException(EditCommand.MESSAGE_NOT_EDITED); + } + ... + } + ``` + +* Where the exception is handled: `MainWindow#executeCommand()` + + + + +* Exception Thrown: ParseException +* Reason: Names should only contain alphanumeric characters and spaces, and it should not be blank. +* Where the exception is thrown: `ParserUtil#parseName()` + + ```java {.line-numbers highlight-lines="4"} + public static Name parseName(String name) throws ParseException { + ... + if (!Name.isValidName(trimmedName)) { + throw new ParseException(Name.MESSAGE_CONSTRAINTS); + } + ... + } + ``` + +* Where the exception is handled: `MainWindow#executeCommand()` + + + + +* No exception thrown. +* Reason: The command is correctly formatted and will edit the tags of the person at index 1 to "one", "two" and "three". + + + + Duplicate values are handled by the `ParserUtil#parseTags()` method because tags are added to a HashSet. The HashSet class inherently handles duplicates by not allowing any equal elements to be added. Therefore, any duplicate tags are not added.
+ Read more on the `add` method of the HashSet class here. +
+ +
+ +
2. What components will you have to modify to perform the following enhancements to the application? - 1. Make command words case-insensitive + - 2. Allow `delete` to remove more than one index at a time +1. Modify `AddressBookParser#parseCommand()` to convert command words to lowercase before parsing. - 3. Save the address book in the CSV format instead + ```java {.line-numbers highlight-lines="3['.toLowerCase()']"} + public Command parseCommand(String userInput) throws ParseException { + ... + final String commandWord = matcher.group("commandWord").toLowerCase(); + final String arguments = matcher.group("arguments"); + } + ``` + + + - 4. Add a new command +1. Modify `DeleteCommandParser` to parse a list of indices. +2. Update `DeleteCommand` to take in a list of indices. + + + + Remember to update other usages of `DeleteCommand` class to handle the change in type of Index. + + - 5. Add a new field to `Person` + - 6. Add a new entity to the address book +1. Import the following classes: + ```java + import java.io.FileWriter; + import java.io.PrintWriter; + ``` +1. Modify the `JsonAddressBookStorage#saveAddressBook()` method + ```java + public void saveAddressBook(ReadOnlyAddressBook addressBook, Path filePath) throws IOException { + requireNonNull(addressBook); + requireNonNull(filePath); + + FileUtil.createIfMissing(filePath); + + try (PrintWriter out = new PrintWriter(new FileWriter(filePath.toFile()))) { + out.println("Name,Phone,Email,Address,Tags"); // CSV header + addressBook.getPersonList().forEach(person -> { + out.println( + escapeField(person.getName().toString()) + "," + + escapeField(person.getPhone().toString()) + "," + + escapeField(person.getEmail().toString()) + "," + + escapeField(person.getAddress().toString()) + "," + + escapeField(person.getTags().toString()) + ); + }); + } catch (IOException e) { + logger.severe("Failed to save address book to " + filePath + ": " + e.getMessage()); + throw e; + } + } + ``` +1. Add a helper method to handle special characters in the fields for Person. + ```java + private String escapeField(String field) { + if (field.contains(",") || field.contains("\"")) { + field = field.replace("\"", "\"\""); + field = "\"" + field + "\""; + } + return field; + } + ``` + + + + +1. Add a class for your new command in the [`logic.commands`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/logic/commands) package. +1. Add a class for your parser to parse the new command in the [`seedu.address.logic.parser`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/logic/parser) package. +1. Update `AddressBookParser` to use the new parser. + + + For a more detailed explanation, refer to [AB3 Tutorial: Adding a Command] + + + + + +1. Add a new class for the field in [`seedu.address.model.person`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/model/person). +2. Update the [`Person`](https://github.com/se-edu/addressbook-level3/blob/master/src/main/java/seedu/address/model/person/Person.java) class to include the new field. +3. Update the [`JsonAdaptedPerson`](https://github.com/se-edu/addressbook-level3/blob/master/src/main/java/seedu/address/storage/JsonAdaptedPerson.java) class in [`seedu.address.storage`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/storage) to include the new field. + + + + Remember to update other usages of `Person` class to handle the new field. + + + + + +For instance, if we are adding `Event` as the new entity to the address book: +1. Add a new class `Event` in `seedu.address.model.event` to represent an Event entity. +1. Add a new class `AddEventCommand` in [`seedu.address.logic.commands`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/logic/commands) that is similar to AddCommand. +1. Implement a `AddEventCommandParser` parser in [`seedu.address.logic.parser`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/logic/parser) to parse the relevant arguments. +1. Update the [`Model`](https://github.com/se-edu/addressbook-level3/blob/master/src/main/java/seedu/address/model/Model.java) interface to add in new methods such as `addEvent` and `hasEvent`. +1. Update the [`ModelManager`](https://github.com/se-edu/addressbook-level3/blob/master/src/main/java/seedu/address/model/ModelManager.java) to implement these new methods. +1. Update the [`AddressBook`](https://github.com/se-edu/addressbook-level3/blob/master/src/main/java/seedu/address/model/AddressBook.java) class to create methods like `setEvent` or `getEventList` etc. +1. Create a `UniqueEventList` class in `seedu.address.model.event` to handle a list of unique events. +1. Implement a `JsonAdaptedEvent` in [`seedu.address.storage`](https://github.com/se-edu/addressbook-level3/tree/master/src/main/java/seedu/address/storage) to save the data in JSON format. + +
[:fas-arrow-up: **ToC**](ab3.md) | **++What's next?++** [:fas-arrow-right: **Adding a Command**](ab3AddRemark.md) \ No newline at end of file From 1646b1109ec9d378b5687b51d44ad47ff952d940 Mon Sep 17 00:00:00 2001 From: Ruth Lim <110249543+ruth-lim@users.noreply.github.com> Date: Sun, 25 Aug 2024 14:52:08 +0800 Subject: [PATCH 2/2] Updated phrasing, Changed panel to info box --- tutorials/ab3TracingCode.md | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tutorials/ab3TracingCode.md b/tutorials/ab3TracingCode.md index 553b599f..babf2d9f 100644 --- a/tutorials/ab3TracingCode.md +++ b/tutorials/ab3TracingCode.md @@ -383,11 +383,11 @@ Here are some quick questions you can try to answer based on your execution path * No exception thrown. * Reason: The command is correctly formatted and will edit the tags of the person at index 1 to "one", "two" and "three". - + - Duplicate values are handled by the `ParserUtil#parseTags()` method because tags are added to a HashSet. The HashSet class inherently handles duplicates by not allowing any equal elements to be added. Therefore, any duplicate tags are not added.
- Read more on the `add` method of the HashSet class here. -
+ Duplicate values are handled by the `ParserUtil#parseTags()` method when tags are added to a HashSet. The HashSet class inherently handles duplicates by not allowing any equal elements to be added. Therefore, any duplicate tags are not added.
+ Read more on the `add` method of the HashSet class [here](https://docs.oracle.com/javase/8/docs/api/java/util/HashSet.html#add-E-). + @@ -416,7 +416,7 @@ Here are some quick questions you can try to answer based on your execution path - Remember to update other usages of `DeleteCommand` class to handle the change in type of Index. + Remember to update other usages of `DeleteCommand` class to handle the change in type of argument.