-
Notifications
You must be signed in to change notification settings - Fork 434
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #128 from szejiancheng/branch-view-profile
View-profile functionality
- Loading branch information
Showing
7 changed files
with
166 additions
and
5 deletions.
There are no files selected for viewing
This file contains 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 contains 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
94 changes: 94 additions & 0 deletions
94
src/main/java/seedu/address/logic/commands/ViewProfileCommand.java
This file contains 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,94 @@ | ||
package seedu.address.logic.commands; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; | ||
|
||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.commons.core.Messages; | ||
import seedu.address.logic.commands.exceptions.CommandException; | ||
import seedu.address.model.Model; | ||
import seedu.address.model.student.Student; | ||
import seedu.address.model.tag.Tag; | ||
|
||
/** | ||
* Prints student profiles with or without name-matching | ||
*/ | ||
public class ViewProfileCommand extends Command { | ||
public static final String COMMAND_WORD = "view-profile"; | ||
|
||
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Prints the profile of the student that matches " | ||
+ "the specified name.\n" | ||
+ "Parameters: \n" | ||
+ PREFIX_NAME + "STUDENT_NAME \n" | ||
+ "Example: " + COMMAND_WORD + " " + PREFIX_NAME + "John Doe"; | ||
private static final Predicate<Student> SHOW_ALL_STUDENTS = student -> true; | ||
private static final String SEPERATOR = "--------------------------------------------------\n"; | ||
|
||
private final List<String> names; | ||
private final Predicate<Student> namePredicate; | ||
|
||
/** | ||
* public constructor for a ViewProfileCommand | ||
* @param names | ||
* @param namePredicate | ||
*/ | ||
public ViewProfileCommand(List<String> names, Predicate<Student> namePredicate) { | ||
this.names = names; | ||
this.namePredicate = namePredicate; | ||
} | ||
|
||
@Override | ||
public CommandResult execute(Model model) throws CommandException { | ||
requireNonNull(model); | ||
model.updateFilteredStudentList(PREDICATE_SHOW_ALL_STUDENTS); | ||
|
||
StringBuilder nonExistNames = new StringBuilder(); | ||
for (String name : names) { | ||
if (model.noSuchStudent(name)) { | ||
nonExistNames.append(name).append(", "); | ||
} | ||
} | ||
if (nonExistNames.length() != 0) { | ||
nonExistNames = new StringBuilder(nonExistNames.substring(0, nonExistNames.length() - 2)); | ||
throw new CommandException(String.format(Messages.MESSAGE_NO_SUCH_STUDENT, nonExistNames)); | ||
} | ||
StringBuilder dupNames = new StringBuilder(); | ||
for (String name : names) { | ||
if (model.hasDuplicateName(name)) { | ||
dupNames.append(name).append(", "); | ||
} | ||
} | ||
if (dupNames.length() != 0) { | ||
dupNames = new StringBuilder(dupNames.substring(0, dupNames.length() - 2)); | ||
throw new CommandException(String.format(Messages.MESSAGE_HAS_DUPLICATE_NAMES, dupNames)); | ||
} | ||
model.updateFilteredStudentList(namePredicate); | ||
|
||
List<Student> studentList = model.getFilteredStudentList(); | ||
|
||
int numberOfStudents = studentList.size(); | ||
StringBuilder sb = new StringBuilder(); | ||
sb.append(SEPERATOR); | ||
|
||
// Loop through each student and add their lesson to the string builder | ||
for (Student student : studentList) { | ||
sb.append(student.getName().fullName).append(":\n"); | ||
sb.append("Phone: ").append(student.getPhone()).append("\n"); | ||
sb.append("Address: ").append(student.getAddress().toString()).append("\n"); | ||
sb.append("Email: ").append(student.getEmail()).append("\n"); | ||
sb.append("Tags: "); | ||
for (Tag tag : student.getTags()) { | ||
sb.append(tag); | ||
} | ||
sb.append("\n"); | ||
|
||
sb.append(SEPERATOR); | ||
} | ||
|
||
return new CommandResult( | ||
String.format(Messages.MESSAGE_STUDENTS_LISTED_OVERVIEW, numberOfStudents, sb)); | ||
} | ||
} |
This file contains 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 contains 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
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/address/logic/parser/ViewProfileCommandParser.java
This file contains 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,63 @@ | ||
package seedu.address.logic.parser; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; | ||
import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; | ||
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_STUDENTS; | ||
|
||
import java.util.ArrayList; | ||
import java.util.List; | ||
import java.util.function.Predicate; | ||
|
||
import seedu.address.logic.commands.Command; | ||
import seedu.address.logic.commands.ViewProfileCommand; | ||
import seedu.address.logic.parser.exceptions.ParseException; | ||
import seedu.address.model.student.NamePredicate; | ||
import seedu.address.model.student.Student; | ||
|
||
|
||
|
||
/** | ||
* Parses input arguments and creates a new ViewProfileCommand object | ||
*/ | ||
public class ViewProfileCommandParser implements Parser { | ||
@Override | ||
public Command parse(String args) throws ParseException { | ||
requireNonNull(args); | ||
|
||
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args, PREFIX_NAME); | ||
|
||
if (!argMultimap.getPreamble().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
ViewProfileCommand.MESSAGE_USAGE)); | ||
} | ||
|
||
Predicate<Student> namePredicate; | ||
List<String> nameList = new ArrayList<>(); | ||
boolean defaultPredicateFlag; | ||
|
||
// If name is present, create a predicate to filter by name | ||
if (argMultimap.getValue(PREFIX_NAME).isPresent()) { | ||
|
||
List<String> nameKeywords = argMultimap.getAllValues(PREFIX_NAME); | ||
// for all the names, trim the name and only take the first word | ||
for (int i = 0; i < nameKeywords.size(); i++) { | ||
String name = nameKeywords.get(i); | ||
name = name.trim(); | ||
if (name.trim().isEmpty()) { | ||
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, | ||
ViewProfileCommand.MESSAGE_USAGE)); | ||
} | ||
int spaceIndex = name.indexOf(" "); | ||
nameKeywords.set(i, name); | ||
} | ||
nameList = nameKeywords; | ||
namePredicate = new NamePredicate(nameKeywords); | ||
} else { | ||
namePredicate = PREDICATE_SHOW_ALL_STUDENTS; | ||
} | ||
|
||
|
||
return new ViewProfileCommand(nameList, namePredicate); | ||
} | ||
} |
This file contains 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