Skip to content

Commit

Permalink
Merge branch 'master' into branch-RefactorTag
Browse files Browse the repository at this point in the history
  • Loading branch information
JYL27 authored Nov 7, 2024
2 parents 8f33f66 + 9d8c3ee commit 962ca35
Show file tree
Hide file tree
Showing 18 changed files with 307 additions and 21 deletions.
19 changes: 19 additions & 0 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();
Person toDelete = null;
boolean PersonDetailsDeleted = false;

for (Person person : lastShownList) {
if (person.getStudentId().equals(studentId)) {
Expand All @@ -96,10 +97,28 @@ public CommandResult execute(Model model) throws CommandException {
}

model.deleteModule(toDelete, moduleToDelete);
if (toDelete.isSamePerson(model.getPersonToDisplay())) {
for (Person person : lastShownList) {
if (person.getStudentId().equals(studentId)) {
toDelete = person;
break;
}
}
model.setPersonToDisplay(toDelete);
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete), true);
}
return new CommandResult(String.format(MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete));
}

if (toDelete.isSamePerson(model.getPersonToDisplay())) {
model.setPersonToDisplay(null);
PersonDetailsDeleted = true;
}

model.deletePerson(toDelete);
if (PersonDetailsDeleted) {
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(toDelete)), true);
}
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(toDelete)));
}

Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,14 @@ public CommandResult execute(Model model) throws CommandException {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}


model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);

if (personToEdit.isSamePerson(model.getPersonToDisplay())) {
model.setPersonToDisplay(editedPerson);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)), true);
}
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

Expand All @@ -118,12 +124,12 @@ private static Person createEditedPerson(Person personToEdit, EditPersonDescript
Module newModule = editPersonDescriptor.newModule;

boolean isModuleRenamed = false;
if (updatedModules.stream().anyMatch(m -> m.value.equals(newModule.value))) {
if (updatedModules.stream().anyMatch(m -> m.value.toUpperCase().equals(newModule.value.toUpperCase()))) {
throw new CommandException(EditCommand.MESSAGE_DUPLICATE_MODULE);
}

for (int i = 0; i < updatedModules.size(); i++) {
if (updatedModules.get(i).value.equals(oldModule.value)) {
if (updatedModules.get(i).value.toUpperCase().equals(oldModule.value.toUpperCase())) {
Module updatedModule = new Module(newModule.value);
if (updatedModules.get(i).hasGrade()) {
updatedModule.setGrade(updatedModules.get(i).getGrade());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,7 @@ public CommandResult execute(Model model) {
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}

@Override
public boolean equals(Object other) {
if (other == this) {
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/GradeCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,10 @@ public CommandResult execute(Model model) throws CommandException {
Person updatedPerson = person.setModuleGrade(module, grade);
model.setPerson(person, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
if (updatedPerson.isSamePerson(model.getPersonToDisplay())) {
model.setPersonToDisplay(updatedPerson);
return new CommandResult(String.format(MESSAGE_SUCCESS, module), true);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, module));
}

Expand Down
4 changes: 4 additions & 0 deletions src/main/java/seedu/address/logic/commands/ModuleCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,10 @@ public CommandResult execute(Model model) throws CommandException {
Person updatedPerson = person.addModule(module);
model.setPerson(person, updatedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
if (updatedPerson.isSamePerson(model.getPersonToDisplay())) {
model.setPersonToDisplay(updatedPerson);
return new CommandResult(String.format(MESSAGE_SUCCESS, studentId), true);
}
return new CommandResult(String.format(MESSAGE_SUCCESS, studentId));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,8 @@ public EditCommand parse(String args) throws ParseException {
if (modules.length != 2) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
}
editPersonDescriptor.setModuleChanges(new Module(modules[0].toUpperCase()), new Module(modules[1]));
editPersonDescriptor.setModuleChanges(new Module(modules[0].toUpperCase()),
new Module(modules[1].toUpperCase()));
} else {
if (argMultimap.getValue(PREFIX_NAME).isPresent()) {
editPersonDescriptor.setName(ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ public ModuleCommand parse(String args) throws ParseException {
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_MODULE);
argMultimap.verifyNoDuplicateStudentId(args);
StudentId studentId = ParserUtil.parseStudentId(preamble);
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get().toUpperCase());
Module module = ParserUtil.parseModule(argMultimap.getValue(PREFIX_MODULE).get());

return new ModuleCommand(studentId, module);
}
Expand Down
43 changes: 43 additions & 0 deletions src/main/java/seedu/address/logic/parser/ParserUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ public static Name parseName(String name) throws ParseException {
if (!Name.isValidName(trimmedName)) {
throw new ParseException(Name.MESSAGE_CONSTRAINTS);
}
trimmedName = makeCapitalise(trimmedName);
return new Name(trimmedName);
}

Expand Down Expand Up @@ -96,6 +97,7 @@ public static Address parseAddress(String address) throws ParseException {
if (!Address.isValidAddress(trimmedAddress)) {
throw new ParseException(Address.MESSAGE_CONSTRAINTS);
}
trimmedAddress = makeUppercase(trimmedAddress);
return new Address(trimmedAddress);
}

Expand All @@ -111,6 +113,7 @@ public static Email parseEmail(String email) throws ParseException {
if (!Email.isValidEmail(trimmedEmail)) {
throw new ParseException(Email.MESSAGE_CONSTRAINTS);
}
trimmedEmail = makeLowercase(trimmedEmail);
return new Email(trimmedEmail);
}

Expand All @@ -126,6 +129,7 @@ public static Role parseRole(String role) throws ParseException {
if (!Role.isValidRole(trimmedRole)) {
throw new ParseException(Role.MESSAGE_CONSTRAINTS);
}
trimmedRole = makeCapitalise(trimmedRole);
return new Role(trimmedRole);
}

Expand Down Expand Up @@ -153,6 +157,7 @@ public static Course parseCourse(String course) throws ParseException {
if (!Course.isValidCourse(trimmedCourse)) {
throw new ParseException(Course.MESSAGE_CONSTRAINTS);
}
trimmedCourse = makeCapitalise(trimmedCourse);
return new Course(trimmedCourse);
}

Expand All @@ -168,6 +173,7 @@ public static Module parseModule(String module) throws ParseException {
if (!Module.isValidModule(trimmedModule)) {
throw new ParseException(Module.MESSAGE_CONSTRAINTS);
}
trimmedModule = makeUppercase(trimmedModule);
return new Module(trimmedModule);
}

Expand All @@ -183,6 +189,43 @@ public static Grade parseGrade(String grade) throws ParseException {
if (!Grade.isValidGrade(trimmedGrade)) {
throw new ParseException(Grade.MESSAGE_CONSTRAINTS);
}
trimmedGrade = makeUppercase(trimmedGrade);
return new Grade(trimmedGrade);
}

/**
* Capitalizes the first letter of each word in the given input string
* and converts all other letters to lowercase.
*
* This method splits the input string by spaces, then processes each word
* to ensure that the first character is uppercase while the remaining characters
* are lowercase. The modified words are then joined back into a single string.
*
* @param input The input string to be capitalized. Each word's first letter will be capitalized,
* and the remaining letters will be in lowercase.
* @return A string with the first letter of each word capitalized and the rest in lowercase.
* Leading and trailing whitespace is removed.
*/
public static String makeCapitalise(String input) {
String[] words = input.split(" ");
StringBuilder capitalised = new StringBuilder();

for (String word : words) {
if (!word.isEmpty()) {
capitalised.append(Character.toUpperCase(word.charAt(0)))
.append(word.substring(1).toLowerCase())
.append(" ");
}
}

return capitalised.toString().trim();
}

public static String makeUppercase(String input) {
return input.toUpperCase();
}

public static String makeLowercase(String input) {
return input.toLowerCase();
}
}
1 change: 0 additions & 1 deletion src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,6 @@ public void setPerson(Person target, Person editedPerson) {
requireAllNonNull(target, editedPerson);

eduContacts.setPerson(target, editedPerson);
setPersonToDisplay(editedPerson);
}

//=========== Filtered Person List Accessors =============================================================
Expand Down
6 changes: 6 additions & 0 deletions src/main/java/seedu/address/ui/PersonDetails.java
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,12 @@ public void updateDetails(Person person) {
role.setText("");
course.setText("");
module.setText("");
profileImage.setVisible(false);
studentIdHeader.setVisible(false);
contactHeader.setVisible(false);
addressHeader.setVisible(false);
emailHeader.setVisible(false);
moduleHeader.setVisible(false);
}
}
}
1 change: 1 addition & 0 deletions src/main/java/seedu/address/ui/PersonListPanel.java
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ protected void updateItem(Person person, boolean empty) {
if (empty || person == null) {
setGraphic(null);
setText(null);
setStyle("-fx-background-color: white;");
} else {
setGraphic(new PersonCard(person, getIndex() + 1).getRoot());
}
Expand Down
40 changes: 40 additions & 0 deletions src/test/java/seedu/address/logic/commands/DeleteCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,45 @@ public void execute_invalidStudentIdWithModule_throwsCommandException() {
deleteCommand, model, String.format(DeleteCommand.MESSAGE_PERSON_NOT_FOUND, invalidStudentId));
}

@Test
public void execute_deleteModuleWhenPersonDisplayed_success() {
Person personToDisplay = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
model.setPersonToDisplay(personToDisplay);

Module moduleToDelete = new Module("CS2103T");
Person updatedPerson = personToDisplay.addModule(moduleToDelete);
model.setPerson(personToDisplay, updatedPerson);

DeleteCommand deleteCommand = new DeleteCommand(personToDisplay.getStudentId(), moduleToDelete);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete.toString());

ModelManager expectedModel = new ModelManager(model.getEduContacts(), new UserPrefs(), personToDisplay);
expectedModel.deleteModule(updatedPerson, moduleToDelete);
assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_deleteModuleWhenPersonNotDisplayed_success() {
Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Person personToDisplay = model.getFilteredPersonList().get(INDEX_SECOND_PERSON.getZeroBased());

model.setPersonToDisplay(personToDisplay);

Module moduleToDelete = new Module("CS2103T");
Person updatedPerson = personToDelete.addModule(moduleToDelete);
model.setPerson(personToDelete, updatedPerson);

DeleteCommand deleteCommand = new DeleteCommand(personToDelete.getStudentId(), moduleToDelete);

String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_MODULE_SUCCESS, moduleToDelete.toString());

ModelManager expectedModel = new ModelManager(model.getEduContacts(), new UserPrefs(), personToDisplay);
expectedModel.deleteModule(updatedPerson, moduleToDelete);

assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_deleteGradedModule_success() {
Person personWithModule = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
Expand All @@ -200,6 +239,7 @@ public void execute_deleteGradedModule_success() {
ModelManager expectedModel = new ModelManager(model.getEduContacts(), new UserPrefs());
expectedModel.deleteModule(personWithModule, moduleToDelete);


assertCommandSuccess(deleteCommand, model, expectedMessage, expectedModel);
}

Expand Down
42 changes: 29 additions & 13 deletions src/test/java/seedu/address/logic/commands/EditCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -175,19 +175,6 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() {
assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_noFieldSpecifiedUnfilteredList_success() {
StudentId studentId = model.getFilteredPersonList().get(0).getStudentId();
EditCommand editCommand = new EditCommand(studentId, new EditPersonDescriptor());
Person editedPerson = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));

Model expectedModel = new ModelManager(new EduContacts(model.getEduContacts()), new UserPrefs(), editedPerson);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}

@Test
public void execute_filteredList_success() {
showPersonAtIndex(model, INDEX_FIRST_PERSON);
Expand Down Expand Up @@ -231,6 +218,35 @@ public void execute_invalidPersonIndexFilteredList_failure() {
assertCommandFailure(editCommand, model, Messages.MESSAGE_INVALID_PERSON_DISPLAYED_STUDENTID);
}

@Test
public void execute_personDisplayedEdited_success() throws CommandException {

Person personToDisplay = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased());
model.setPersonToDisplay(personToDisplay);

EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder()
.withName("Edited Name")
.withPhone("98765432")
.withEmail("[email protected]")
.build();

EditCommand editCommand = new EditCommand(personToDisplay.getStudentId(), descriptor);

Person editedPerson = new PersonBuilder(personToDisplay)
.withName("Edited Name")
.withPhone("98765432")
.withEmail("[email protected]")
.build();

String expectedMessage = String.format(EditCommand.MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson));

Model expectedModel = new ModelManager(model.getEduContacts(), new UserPrefs());
expectedModel.setPerson(personToDisplay, editedPerson);
expectedModel.setPersonToDisplay(editedPerson);

assertCommandSuccess(editCommand, model, expectedMessage, expectedModel);
}

@Test
public void equals() {
StudentId firstPersonId = model.getFilteredPersonList().get(0).getStudentId();
Expand Down
29 changes: 27 additions & 2 deletions src/test/java/seedu/address/logic/commands/FilterCommandTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,29 @@ public void execute_zeroKeywords_noPersonFound() {
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void executeNoKeywordsCourseFilter() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
CourseContainsKeywordsPredicate predicate =
new CourseContainsKeywordsPredicate(Collections.singletonList(" "));
FilterCommand command = new FilterCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}

@Test
public void executeNoKeywordsModuleFilter() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 0);
ModuleContainsKeywordsPredicate predicate =
new ModuleContainsKeywordsPredicate(" ");
FilterCommand command = new FilterCommand(predicate);
expectedModel.updateFilteredPersonList(predicate);
assertCommandSuccess(command, model, expectedMessage, expectedModel);
assertEquals(Collections.emptyList(), model.getFilteredPersonList());
}


@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_PERSONS_LISTED_OVERVIEW, 3);
Expand All @@ -78,7 +101,8 @@ public void execute_multipleKeywords_multiplePersonsFound() {

@Test
public void toStringMethod_namePredicate() {
NameContainsKeywordsPredicate predicate = new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
NameContainsKeywordsPredicate predicate =
new NameContainsKeywordsPredicate(Arrays.asList("keyword"));
FilterCommand filterCommand = new FilterCommand(predicate);
String expected = FilterCommand.class.getCanonicalName()
+ "{namePredicate="
Expand All @@ -90,7 +114,8 @@ public void toStringMethod_namePredicate() {

@Test
public void toStringMethod_modulePredicate() {
ModuleContainsKeywordsPredicate modulePredicate = new ModuleContainsKeywordsPredicate("CS2103T");
ModuleContainsKeywordsPredicate modulePredicate =
new ModuleContainsKeywordsPredicate("CS2103T");
FilterCommand filterCommand = new FilterCommand(modulePredicate);
String expected = FilterCommand.class.getCanonicalName()
+ "{namePredicate=null, modulePredicate="
Expand Down
Loading

0 comments on commit 962ca35

Please sign in to comment.