Skip to content

Commit

Permalink
Merge pull request #128 from szejiancheng/branch-view-profile
Browse files Browse the repository at this point in the history
View-profile functionality
  • Loading branch information
Yufannnn authored Mar 31, 2023
2 parents 8aadb97 + 46f2561 commit f99d969
Show file tree
Hide file tree
Showing 7 changed files with 166 additions and 5 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/commons/core/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ public class Messages {
public static final String MESSAGE_UNKNOWN_COMMAND = "Unknown command";
public static final String MESSAGE_INVALID_COMMAND_FORMAT = "Invalid command format! \n%1$s";
public static final String MESSAGE_INVALID_STUDENT_DISPLAYED_INDEX = "The student index provided is invalid";
public static final String MESSAGE_STUDENTS_LISTED_OVERVIEW = "%1$d students listed!";
public static final String MESSAGE_STUDENTS_LISTED_OVERVIEW = "%d students listed!\n%s";
public static final String MESSAGE_HOMEWORK_ADDED_SUCCESS = "New homework added:\n%s\n"
+ "To the following students:\n%s";
public static final String MESSAGE_HOMEWORK_LISTED_OVERVIEW = "%d homework from %d student listed:\n%s";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ public CommandResult execute(Model model) {
requireNonNull(model);
model.updateFilteredStudentList(predicate);
return new CommandResult(
String.format(Messages.MESSAGE_STUDENTS_LISTED_OVERVIEW, model.getFilteredStudentList().size()));
String.format(Messages.MESSAGE_STUDENTS_LISTED_OVERVIEW, model.getFilteredStudentList().size(), ""));
}

@Override
Expand Down
94 changes: 94 additions & 0 deletions src/main/java/seedu/address/logic/commands/ViewProfileCommand.java
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));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
public class ViewLessonCommand extends Command {
public static final String COMMAND_WORD = "view-lesson";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all exams filtered by\n"
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all lessons filtered by\n"
+ "* name of student (case-insensitive) and/or\n"
+ "* date and/or\n"
+ "* subject and/or\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.logic.commands.FindCommand;
import seedu.address.logic.commands.HelpCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ViewProfileCommand;
import seedu.address.logic.commands.exam.CreateExamCommand;
import seedu.address.logic.commands.exam.DeleteExamCommand;
import seedu.address.logic.commands.exam.UpdateExamCommand;
Expand Down Expand Up @@ -141,6 +142,9 @@ public Command parseCommand(String userInput) throws ParseException {
case UpdateExamCommand.COMMAND_WORD:
return new UpdateExamCommandParser().parse(arguments);

case ViewProfileCommand.COMMAND_WORD:
return new ViewProfileCommandParser().parse(arguments);

default:
throw new ParseException(MESSAGE_UNKNOWN_COMMAND);
}
Expand Down
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);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void equals() {

@Test
public void execute_zeroKeywords_noPersonFound() {
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 0);
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 0, "");
NameContainsKeywordsPredicate predicate = preparePredicate(" ");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredStudentList(predicate);
Expand All @@ -66,7 +66,7 @@ public void execute_zeroKeywords_noPersonFound() {

@Test
public void execute_multipleKeywords_multiplePersonsFound() {
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 3);
String expectedMessage = String.format(MESSAGE_STUDENTS_LISTED_OVERVIEW, 3, "");
NameContainsKeywordsPredicate predicate = preparePredicate("Kurz Elle Kunz");
FindCommand command = new FindCommand(predicate);
expectedModel.updateFilteredStudentList(predicate);
Expand Down

0 comments on commit f99d969

Please sign in to comment.