-
Notifications
You must be signed in to change notification settings - Fork 141
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
[W5.11r][F11-B1]Sheikh Umar #755
base: master
Are you sure you want to change the base?
Changes from all commits
3060d8d
f7fc7d3
5a4c737
1b1c595
f3545b9
8cc0449
8f1c0c0
0d13337
7f8742b
f50cf24
01997fb
a8d5b7e
68db7a3
29188e0
40208ee
733224c
1d6df7b
e14d382
9f7cb53
ccb86ac
8ba31f1
b96dc5c
1b67d4d
ee9d207
db1d746
81c8d72
ecf7c7c
b54df6c
ff9e06a
7edfb2a
fa1ed45
339a1ec
98b9723
2052e30
4c5a231
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,108 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.common.Messages; | ||
import seedu.addressbook.data.exception.IllegalValueException; | ||
import seedu.addressbook.data.person.Address; | ||
import seedu.addressbook.data.person.Email; | ||
import seedu.addressbook.data.person.Name; | ||
import seedu.addressbook.data.person.Person; | ||
import seedu.addressbook.data.person.Phone; | ||
import seedu.addressbook.data.person.ReadOnlyPerson; | ||
import seedu.addressbook.data.person.UniquePersonList.PersonNotFoundException; | ||
import seedu.addressbook.parser.Parser.Attribute; | ||
|
||
/** | ||
* Edits the attribute of the person identified | ||
* based on the last-displayed index of the person in the address book | ||
* by updating his/her atttribute with the new value. | ||
*/ | ||
|
||
//Solution below adapted from https://github.com/nus-cs2103-AY1718S2/addressbook-level3/pull/749/files | ||
public class EditCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "edit"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD | ||
+ ": Edits an attribute of a person identified by an index number " | ||
+ "used in the last find/last call.\n" | ||
+ "Parameters: INDEX ATTRIBUTE NEW_VALUE\n" | ||
+ "Example: " + COMMAND_WORD + "2 name Tom"; | ||
|
||
public static final String MESSAGE_EDIT_PERSON_SUCCESS = "Edit is successful!\n" | ||
+ "Original Person: %1$s\n" | ||
+ ", Edited Person: %2$s"; | ||
|
||
private final Attribute attribute; | ||
|
||
private final String newValue; | ||
|
||
public EditCommand(int targetIndex, Attribute attribute, String newValue) { | ||
super(targetIndex); | ||
this.attribute = attribute; | ||
this.newValue = newValue; | ||
} | ||
|
||
@Override | ||
public CommandResult execute() { | ||
|
||
try { | ||
final ReadOnlyPerson target = getTargetPerson(); | ||
if(isNewValueSameAsOld(target)) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_NEW_VALUE); | ||
} | ||
Person updatedPerson = updateExistingPerson(target); | ||
addressBook.removePerson(target); | ||
addressBook.addPerson(updatedPerson); | ||
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, | ||
target.getAsTextHidePrivate(), | ||
updatedPerson.getAsTextHidePrivate())); | ||
} catch(IndexOutOfBoundsException ie) { | ||
return new CommandResult(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX); | ||
} catch(PersonNotFoundException pnfe) { | ||
return new CommandResult(Messages.MESSAGE_PERSON_NOT_IN_ADDRESSBOOK); | ||
} catch(IllegalValueException ive) { | ||
return new CommandResult(ive.getMessage()); | ||
} | ||
} | ||
|
||
private boolean isNewValueSameAsOld(ReadOnlyPerson target) { | ||
|
||
switch(attribute) { | ||
case ADDRESS: | ||
return target.getAddress().toString().equals(newValue); | ||
case EMAIL: | ||
return target.getEmail().toString().equals(newValue); | ||
case NAME: | ||
return target.getName().toString().equals(newValue); | ||
case PHONE: | ||
return target.getPhone().toString().equals(newValue); | ||
default: | ||
return true; | ||
} | ||
} | ||
|
||
private Person updateExistingPerson(ReadOnlyPerson existingPerson) throws IllegalValueException { | ||
Address address = existingPerson.getAddress(); | ||
Email email = existingPerson.getEmail(); | ||
Name name = existingPerson.getName(); | ||
Phone phone = existingPerson.getPhone(); | ||
|
||
switch(attribute) { | ||
case ADDRESS: | ||
address = new Address(newValue, address.isPrivate()); | ||
break; | ||
case EMAIL: | ||
email = new Email(newValue, email.isPrivate()); | ||
break; | ||
case NAME: | ||
name = new Name(newValue); | ||
break; | ||
case PHONE: | ||
phone = new Phone(newValue, phone.isPrivate()); | ||
break; | ||
} | ||
|
||
return new Person(name, phone, email, address, existingPerson.getTags()); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
package seedu.addressbook.commands; | ||
|
||
import seedu.addressbook.data.person.Person; | ||
|
||
import java.util.List; | ||
|
||
/** | ||
* Lists all persons in the address book in a sorted order based on the first name of the person | ||
*/ | ||
public class SortCommand extends Command { | ||
|
||
public static final String COMMAND_WORD = "sort"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ":\n" + "Lists all persons in the address book in a sorted order " | ||
+ "based on the first name of the person with index numbers.\n\t" | ||
+ "Example: " + COMMAND_WORD; | ||
|
||
@Override | ||
public CommandResult execute() { | ||
List<Person> allPersons = addressBook.getAllPersons().getInternalList(); | ||
allPersons.sort((Person p1, Person p2) -> (p1.getName().toString()).compareTo(p2.getName().toString())); | ||
return new CommandResult(getMessageForPersonListShownSummary(allPersons), allPersons); | ||
} | ||
|
||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -201,6 +201,30 @@ public void execute_list_showsAllPersons() throws Exception { | |
expectedList); | ||
} | ||
|
||
@Test | ||
public void execute_sort_showsAllPerson() throws Exception { | ||
// prepare expectations | ||
TestDataHelper helper = new TestDataHelper(); | ||
Person person1 = helper.generatePersonWithName("John Tan"); | ||
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. Might want to name these variables |
||
Person person2 = helper.generatePersonWithName("Ali Baba"); | ||
Person person3 = helper.generatePersonWithName("Jill Tan"); | ||
Person person4 = helper.generatePersonWithName("Bob Lee"); | ||
|
||
List<Person> expectedList = helper.generatePersonList(person2, person4, person3, person1); | ||
List<Person> unorderedList = helper.generatePersonList(person1, person2, person3, person4); | ||
|
||
AddressBook expectedAB = helper.generateAddressBook(unorderedList); | ||
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. Why is your expectedAB generated from the |
||
|
||
//prepare address book state | ||
helper.addToAddressBook(addressBook, unorderedList); | ||
assertCommandBehavior("sort", | ||
Command.getMessageForPersonListShownSummary(expectedList), | ||
expectedAB, | ||
true, | ||
expectedList); | ||
|
||
} | ||
|
||
@Test | ||
public void execute_view_invalidArgsFormat() throws Exception { | ||
String expectedMessage = String.format(MESSAGE_INVALID_COMMAND_FORMAT, ViewCommand.MESSAGE_USAGE); | ||
|
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.
We do not condone wholesale copying of enhancements for LO submissions. Do not do this again. Since you have the sort command, I will be only reviewing the sort command enhancement.