forked from nus-cs2103-AY1819S2/addressbook-level4
-
Notifications
You must be signed in to change notification settings - Fork 0
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 nus-cs2103-AY1819S2#71 from kthSim/codeMods
[v1.2] Stats, Sort, & Find
- Loading branch information
Showing
36 changed files
with
1,325 additions
and
61 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
63 changes: 63 additions & 0 deletions
63
src/main/java/seedu/address/logic/commands/SortCommand.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.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
80
src/main/java/seedu/address/logic/commands/StatsCommand.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,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
109
src/main/java/seedu/address/logic/comparators/PatientComparator.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,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 | ||
} |
8 changes: 8 additions & 0 deletions
8
src/main/java/seedu/address/logic/comparators/RecordComparator.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,8 @@ | ||
package seedu.address.logic.comparators; | ||
|
||
/** | ||
* Contains comparators for each record attribute | ||
*/ | ||
public class RecordComparator { | ||
|
||
} |
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
Oops, something went wrong.