Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY1819S2#71 from kthSim/codeMods
Browse files Browse the repository at this point in the history
[v1.2] Stats, Sort, & Find
  • Loading branch information
kthSim authored Mar 17, 2019
2 parents 73806a8 + 0cdeabe commit b98d511
Show file tree
Hide file tree
Showing 36 changed files with 1,325 additions and 61 deletions.
16 changes: 16 additions & 0 deletions src/main/java/seedu/address/logic/commands/CommandResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@ public class CommandResult {
/** The application should exit. */
private final boolean exit;

/** Stat information should be shown to the user. */
private boolean showStat;

/**
* Constructs a {@code CommandResult} with the specified fields.
*/
Expand All @@ -34,6 +37,15 @@ public CommandResult(String feedbackToUser) {
this(feedbackToUser, false, false);
}

/**
* Constructs a {@code CommandResult} with the specified {@code feedbackToUser} and {@code showStat},
* and other fields set to their default value.
*/
public CommandResult(String feedbackToUser, boolean showStat) {
this(feedbackToUser, false, false);
this.showStat = showStat;
}

public String getFeedbackToUser() {
return feedbackToUser;
}
Expand All @@ -42,6 +54,10 @@ public boolean isShowHelp() {
return showHelp;
}

public boolean isShowStat() {
return showStat;
}

public boolean isExit() {
return exit;
}
Expand Down
17 changes: 9 additions & 8 deletions src/main/java/seedu/address/logic/commands/FindCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import seedu.address.commons.core.Messages;
import seedu.address.logic.CommandHistory;
import seedu.address.model.Model;
import seedu.address.model.person.NameContainsKeywordsPredicate;
import seedu.address.model.util.predicate.ContainsKeywordsPredicate;

/**
* Finds and lists all persons in address book whose name contains any of the argument keywords.
Expand All @@ -15,21 +15,22 @@ public class FindCommand extends Command {

public static final String COMMAND_WORD = "find";

public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose names contain any of "
+ "the specified keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " alice bob charlie";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Finds all persons whose particulars contain any of "
+ "the specified parameter's keywords (case-insensitive) and displays them as a list with index numbers.\n"
+ "Parameters: prefix/KEYWORD [MORE_KEYWORDS]...\n"
+ "Example: " + COMMAND_WORD + " n/alice bob charlie";

private final NameContainsKeywordsPredicate predicate;
private ContainsKeywordsPredicate predicate;

public FindCommand(NameContainsKeywordsPredicate predicate) {
public FindCommand(ContainsKeywordsPredicate predicate) {
this.predicate = predicate;
}


@Override
public CommandResult execute(Model model, CommandHistory history) {
requireNonNull(model);
model.updateFilteredPersonList(predicate);
model.updateFilteredPersonList(this.predicate);
return new CommandResult(
String.format(Messages.MESSAGE_PERSONS_LISTED_OVERVIEW, model.getFilteredPersonList().size()));
}
Expand Down
63 changes: 63 additions & 0 deletions src/main/java/seedu/address/logic/commands/SortCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.Comparator;

import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.patient.Patient;

/**
* Sorts data in accordance to the given parameter
*/
public class SortCommand extends Command {

public static final String COMMAND_WORD = "sort";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ " : Sorts the displayed list of patients OR a patient's records according to the desired parameter.\n"
+ "Parameters: [record] PARAMETER \n"
+ "Example 1: " + COMMAND_WORD + " name\n"
+ "Example 2: " + COMMAND_WORD + " record procedure\n";

public static final String MESSAGE_SUCCESS = "List has been sorted by %1$s!";

public final Comparator<Patient> attrCompare;
private final String paraType;
private final boolean isReverse;

public SortCommand(Comparator<Patient> chosenCompare, String paraType, boolean sortOrder) {
requireNonNull(chosenCompare);
this.attrCompare = chosenCompare;
this.paraType = paraType;
this.isReverse = sortOrder;
}

/**
* Sets default sortOrder to ascending
*/
public SortCommand(Comparator<Patient> chosenCompare, String paraType) {
this(chosenCompare, paraType, false);
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
//TODO Implement sorting for records
model.sortAddressBook(this.attrCompare, this.isReverse);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
model.commitAddressBook();
return new CommandResult(String.format(MESSAGE_SUCCESS, paraType));
}

@Override
public boolean equals(Object other) {
return other == this
|| (other instanceof SortCommand
&& attrCompare.equals(((SortCommand) other).attrCompare)
&& paraType.equals(((SortCommand) other).paraType)
&& isReverse == ((SortCommand) other).isReverse);
}
}
80 changes: 80 additions & 0 deletions src/main/java/seedu/address/logic/commands/StatsCommand.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;

import java.util.List;

import seedu.address.commons.core.Messages;
import seedu.address.commons.core.index.Index;
import seedu.address.logic.CommandHistory;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.patient.Patient;
import seedu.address.model.person.Person;
import seedu.address.ui.StatWindow;


/**
* Shows statistics for the given patient
*/
public class StatsCommand extends Command {
public static final String COMMAND_WORD = "stats";

public static final String MESSAGE_USAGE = COMMAND_WORD
+ " : Shows statistics for the identified patient. Patient can be identified either by the index number in "
+ "the displayed person list OR by keyword.\n"
+ "Parameters: INDEX (must be positive integer) or KEYWORD \n"
+ "Example 1: " + COMMAND_WORD + " 3\n"
+ "Example 2: " + COMMAND_WORD + " alice\n";

public static final String MESSAGE_SUCCESS = "Statistic for patient %1$s printed!";

private Patient toStat;
private Index index;
private boolean isIndex;

public StatsCommand(Patient person) {
requireNonNull(person);
toStat = person;
this.isIndex = false;
}

public StatsCommand(Index index) {
requireNonNull(index);
this.index = index;
this.isIndex = true;
}

@Override
public CommandResult execute(Model model, CommandHistory history) throws CommandException {
//TODO: Implement stat execution for keyword
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index != null) {
Patient patientToStat = extractPatientFromIndex(lastShownList);
this.toStat = patientToStat;
}
StatWindow.setStatPatient(this.toStat);

return new CommandResult(String.format(MESSAGE_SUCCESS, toStat.getName()), true);
}

/**
* Returns the patient who corresponds to the inputted index.
*/
private Patient extractPatientFromIndex(List<Person> lastShownList) throws CommandException {
if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
return (Patient) lastShownList.get(index.getZeroBased());
}


@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof StatsCommand // instanceof handles nulls
&& toStat.equals(((StatsCommand) other).toStat));
}
}
109 changes: 109 additions & 0 deletions src/main/java/seedu/address/logic/comparators/PatientComparator.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
package seedu.address.logic.comparators;

import java.util.Comparator;

import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.patient.Patient;

/**
* Contains comparators for each patient attribute
*/
public class PatientComparator {

/**
* Comparator to sort via Name
*/
private static Comparator<Patient> compPatientName = new Comparator<Patient>() {
@Override
public int compare(Patient p1, Patient p2) {
return p1.getName().toString().compareTo(p2.getName().toString());
}
};

/**
* Comparator to sort via Phone number
*/
private static Comparator<Patient> compPatientPhone = new Comparator<Patient>() {
@Override
public int compare(Patient p1, Patient p2) {
return p1.getPhone().toString().compareTo(p2.getPhone().toString());
}
};

/**
* Comparator to sort via Email.
*/
private static Comparator<Patient> compPatientEmail = new Comparator<Patient>() {
@Override
public int compare(Patient p1, Patient p2) {
return p1.getEmail().toString().compareTo(p2.getEmail().toString());
}
};

/**
* Comparator to sort via Address.
*/
private static Comparator<Patient> compPatientAddress = new Comparator<Patient>() {
@Override
public int compare(Patient p1, Patient p2) {
return p1.getAddress().toString().compareTo(p2.getAddress().toString());
}
};

/**
* Comparator to sort via Nric.
*/
private static Comparator<Patient> compPatientNric = new Comparator<Patient>() {
@Override
public int compare(Patient p1, Patient p2) {
return p1.getNric().toString().compareTo(p2.getNric().toString());
}
};

/**
* Comparator to sort via Date of Birth.
*/
private static Comparator<Patient> compPatientDob = new Comparator<Patient>() {
//TODO: Tweak so that it's date based instead of string based.
@Override
public int compare(Patient p1, Patient p2) {
return p1.getDateOfBirth().getDate().compareTo(p2.getDateOfBirth().getDate());
}
};

public static Comparator<Patient> getPatientComparator(String parameterType) throws ParseException {
Comparator<Patient> paComp;
switch (parameterType.trim()) {

case "name":
paComp = compPatientName;
break;

case "phone":
paComp = compPatientPhone;
break;

case "email":
paComp = compPatientEmail;
break;

case "address":
paComp = compPatientAddress;
break;

case "nric":
paComp = compPatientNric;
break;

case "dob":
paComp = compPatientDob;
break;

default:
throw new ParseException("");
}

return paComp;
}
//TODO: Implement reverse sorting
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
package seedu.address.logic.comparators;

/**
* Contains comparators for each record attribute
*/
public class RecordComparator {

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
import seedu.address.logic.commands.RedoCommand;
import seedu.address.logic.commands.SaveCommand;
import seedu.address.logic.commands.SelectCommand;
import seedu.address.logic.commands.SortCommand;
import seedu.address.logic.commands.StatsCommand;
import seedu.address.logic.commands.TaskAddCommand;
import seedu.address.logic.commands.TaskDeleteCommand;
import seedu.address.logic.commands.TaskEditCommand;
Expand Down Expand Up @@ -91,6 +93,12 @@ public Command parseCommand(String userInput) throws ParseException {
case RedoCommand.COMMAND_WORD:
return new RedoCommand();

case StatsCommand.COMMAND_WORD:
return new StatsCommandParser().parse(arguments);

case SortCommand.COMMAND_WORD:
return new SortCommandParser().parse(arguments);

case CopyCommand.COMMAND_WORD:
return new CopyCommandParser().parse(arguments);

Expand Down
Loading

0 comments on commit b98d511

Please sign in to comment.