Skip to content

Commit

Permalink
Merge pull request #201 from ReflectiveObsidian/improve-editattribute…
Browse files Browse the repository at this point in the history
…-command

Make edit attribute command parser same logic as add attribute command
  • Loading branch information
ReflectiveObsidian authored Apr 4, 2024
2 parents ca37d8d + 67255aa commit 8afe97d
Showing 1 changed file with 36 additions and 39 deletions.
75 changes: 36 additions & 39 deletions src/main/java/seedu/address/logic/parser/EditCommandParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -21,55 +21,52 @@ public class EditCommandParser implements Parser<EditAttributeCommand> {
* Examples:
* editAttribute /4000 /Name John Doe /Phone 12345678
*/
public EditAttributeCommand parse(String args) throws ParseException {
args = args.trim();

// Split the arguments string on spaces after the first "/"
String[] parts = args.split(" ", 3);

// Validate command structure
if (parts.length < 3) {
throw new ParseException(EditAttributeCommand.MESSAGE_USAGE);
}

// The second part should be the UUID prefixed with "/"
if (!parts[1].startsWith("/")) {
public EditAttributeCommand parse(String userInput) throws ParseException {
userInput = userInput.trim();
int firstSlashIndex = userInput.indexOf('/');
if (firstSlashIndex == -1) {
throw new ParseException(Messages.MESSAGE_MISSING_UUID + "\n" + EditAttributeCommand.MESSAGE_USAGE);
}
String uuid = parts[1].substring(1); // Remove the leading "/"

if (parts.length < 2) {
throw new ParseException(Messages.MESSAGE_MISSING_ATTRIBUTES + "\n" + EditAttributeCommand.MESSAGE_USAGE);
}
// Find the index of the start of attributes section (the second slash)
int secondSlashIndex = userInput.indexOf('/', firstSlashIndex + 1);
// If there's no second slash, the end of the UUID is the end of the string
int endOfUuidIndex = (secondSlashIndex == -1) ? userInput.length() : secondSlashIndex;

// The rest of the string will be attribute/value pairs split by "/"
String[] attributePairs = parts[2].split("/", -1);
Map<String, String> attributesToEdit = new HashMap<>();
for (String pair : attributePairs) {
pair = pair.trim();
if (pair.isEmpty()) {
continue;
}
// Extract UUID, which is between the first slash and the start of the second segment
String uuid = userInput.substring(firstSlashIndex + 1, endOfUuidIndex).trim();
if (uuid.contains(" ") || uuid.length() < 2) {
throw new ParseException(Messages.MESSAGE_INVALID_UUID_FORMAT + "\n" + EditAttributeCommand.MESSAGE_USAGE);
}

// Split each pair by the first space
String[] keyValue = pair.split(" ", 2);
if (keyValue.length < 2) {
throw new ParseException(Messages.MESSAGE_INVALID_ATTRIBUTE_FORMAT + "\n"
+ EditAttributeCommand.MESSAGE_USAGE);
}
String attributeName = keyValue[0].trim();
String attributeValue = keyValue[1].trim();
// Attributes part starts after the second slash, if there is one
String attributesPart = (secondSlashIndex != -1) ? userInput.substring(secondSlashIndex + 1) : "";

// Check that the attribute name isn't empty and the attribute value isn't just whitespace
if (attributeName.isEmpty() || attributeValue.trim().isEmpty()) {
throw new ParseException(Messages.MESSAGE_INVALID_ATTRIBUTE_FORMAT + "\n"
+ EditAttributeCommand.MESSAGE_USAGE);
Map<String, String> attributes = new HashMap<>();
if (!attributesPart.isEmpty()) {
// Now, split the remaining part by "/" to separate attributes, considering additional slashes
String[] attributeParts = attributesPart.split("/", -1);
for (String part : attributeParts) {
part = part.trim();
if (!part.isEmpty()) {
String[] keyValue = part.split(" ", 2);
if (keyValue.length < 2) {
throw new ParseException("Invalid attribute format. Each attribute must have a value.");
}
// Extract the attribute name and value
String attributeName = keyValue[0].trim();
String attributeValue = keyValue[1].trim();
attributes.put(attributeName, attributeValue);
}
}
}

attributesToEdit.put(attributeName, attributeValue);
if (attributes.isEmpty()) {
// You may decide to remove this check if attributes are optional
throw new ParseException(Messages.MESSAGE_MISSING_ATTRIBUTES + "\n" + EditAttributeCommand.MESSAGE_USAGE);
}

return new EditAttributeCommand(uuid, attributesToEdit);
return new EditAttributeCommand(uuid, attributes);
}

}

0 comments on commit 8afe97d

Please sign in to comment.