Skip to content

Commit

Permalink
Merge pull request #77 from volleyballkickedme/nric-indexing
Browse files Browse the repository at this point in the history
Nric indexing
  • Loading branch information
reidenong authored Nov 1, 2024
2 parents 1eb9963 + 34af9d3 commit dcccb18
Show file tree
Hide file tree
Showing 23 changed files with 503 additions and 346 deletions.
49 changes: 29 additions & 20 deletions src/main/java/seedu/address/logic/commands/AppointmentCommand.java
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
package seedu.address.logic.commands;

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

import java.util.List;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Appointment;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;

/**
Expand All @@ -23,43 +25,49 @@ public class AppointmentCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Sets an appointment for the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer), a/APPOINTMENT (in the format DD-MM-YYYY HH:MM)\n"
+ "Parameters: NRIC, a/APPOINTMENT (in the format DD-MM-YYYY HH:MM)\n"
+ "Example: " + COMMAND_WORD + " 1 a/25-12-2024 14:30";

public static final String MESSAGE_SET_APPOINTMENT_SUCCESS = "Set Appointment for Person: %1$s";
public static final String MESSAGE_INVALID_APPOINTMENT_FORMAT = "Please use DD-MM-YYYY HH:MM.";
public static final String MESSAGE_NO_APPOINTMENT_PROVIDED = "Please provide a valid appointment date.";

private final Index index;
//private final Index index;
private final Nric nric;
private final String appointmentString; // Store appointment string to convert to LocalDateTime

/**
* Constructor to create an AppointmentCommand.
*
* @param targetIndex Index of the person to set the appointment for.
* @param targetNric NRIC of the person to set the appointment for.
* @param appointmentString String of the appointment in the format a/DD-MM-YYYY HH:MM.
*/
public AppointmentCommand(Index targetIndex, String appointmentString) {
this.index = targetIndex;
this.appointmentString = appointmentString.trim();
public AppointmentCommand(Nric targetNric, String appointmentString) {
this.nric = targetNric;
this.appointmentString = appointmentString;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (this.index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
Optional<Person> personWithMatchingNric = lastShownList.stream()
.filter(person -> nric.equals(person.getNric()))
.findFirst();

if (personWithMatchingNric.isPresent()) {
Person personToEdit = personWithMatchingNric.get();
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(),
personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(),
new Appointment(this.appointmentString), personToEdit.getLogEntries());
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult("Set appointment for " + personToEdit.getName() + " on " + this.appointmentString);
} else {
throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND);
}

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(),
personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(),
new Appointment(this.appointmentString), personToEdit.getLogEntries());
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult("Set appointment for " + personToEdit.getName() + " on " + this.appointmentString);
}

@Override
Expand All @@ -73,14 +81,15 @@ public boolean equals(Object other) {
}

AppointmentCommand otherCommand = (AppointmentCommand) other;
return index.equals(otherCommand.index)
return nric.equals(otherCommand.nric)
&& appointmentString.equals(otherCommand.appointmentString);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", index)
//.add("targetIndex", index)
.add("targetNric", nric)
.add("appointmentString", appointmentString)
.toString();
}
Expand Down
31 changes: 18 additions & 13 deletions src/main/java/seedu/address/logic/commands/DeleteCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@
import static java.util.Objects.requireNonNull;

import java.util.List;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;

/**
Expand All @@ -23,29 +24,33 @@ public class DeleteCommand extends Command {

public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Parameters: NRIC\n"
+ "Example: " + COMMAND_WORD + " 1";

public static final String MESSAGE_DELETE_PERSON_SUCCESS = "Deleted Person: %1$s";

private final Index targetIndex;
private final Nric targetNric;

public DeleteCommand(Index targetIndex) {
this.targetIndex = targetIndex;
public DeleteCommand(Nric targetNric) {
this.targetNric = targetNric;
}

@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (targetIndex.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Optional<Person> personWithMatchingNric = lastShownList.stream()
.filter(person -> targetNric.equals(person.getNric()))
.findFirst();

Person personToDelete = lastShownList.get(targetIndex.getZeroBased());
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
if (personWithMatchingNric.isPresent()) {
Person personToDelete = personWithMatchingNric.get();
model.deletePerson(personToDelete);
return new CommandResult(String.format(MESSAGE_DELETE_PERSON_SUCCESS, Messages.format(personToDelete)));
} else {
throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND);
}
}

@Override
Expand All @@ -60,13 +65,13 @@ public boolean equals(Object other) {
}

DeleteCommand otherDeleteCommand = (DeleteCommand) other;
return targetIndex.equals(otherDeleteCommand.targetIndex);
return targetNric.equals(otherDeleteCommand.targetNric);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("targetIndex", targetIndex)
.add("targetNric", targetNric)
.toString();
}
}
49 changes: 27 additions & 22 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
import java.util.Optional;
import java.util.Set;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.CollectionUtil;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
Expand Down Expand Up @@ -45,17 +44,17 @@ public class EditCommand extends Command {


public static final String MESSAGE_USAGE = COMMAND_WORD + ": Edits the details of the person identified "
+ "by the index number used in the displayed person list. "
+ "by the patient's NRIC. "
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "Parameters: NRIC "
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
+ "[" + PREFIX_EMAIL + "EMAIL] "
+ "[" + PREFIX_NRIC + "NRIC] "
+ "[" + PREFIX_ADDRESS + "ADDRESS] "
+ "[" + PREFIX_TAG + "TAG]..."
+ "[" + PREFIX_APPOINTMENT + "APPOINTMENT]\n"
+ "Example: " + COMMAND_WORD + " 1 "
+ "Example: " + COMMAND_WORD + " S1234567A "
+ PREFIX_PHONE + "91234567 "
+ PREFIX_EMAIL + "[email protected]"
+ PREFIX_NRIC + "S1231231D";
Expand All @@ -64,18 +63,20 @@ public class EditCommand extends Command {
public static final String MESSAGE_NOT_EDITED = "At least one field to edit must be provided.";
public static final String MESSAGE_DUPLICATE_PERSON = "This person already exists in the address book.";

private final Index index;
//private final Index index;
private final Nric nric;
private final EditPersonDescriptor editPersonDescriptor;

/**
* @param index of the person in the filtered person list to edit
* @param nric of the person in the filtered person list to edit
* @param editPersonDescriptor details to edit the person with
*/
public EditCommand(Index index, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(index);
public EditCommand(Nric nric, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(nric);
requireNonNull(editPersonDescriptor);

this.index = index;
//this.index = index;
this.nric = nric;
this.editPersonDescriptor = new EditPersonDescriptor(editPersonDescriptor);
}

Expand All @@ -84,20 +85,24 @@ public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Optional<Person> personWithMatchingNric = lastShownList.stream()
.filter(person -> nric.equals(person.getNric()))
.findFirst();

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);
if (personWithMatchingNric.isPresent()) {
Person personToEdit = personWithMatchingNric.get();
Person editedPerson = createEditedPerson(personToEdit, editPersonDescriptor);

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}
if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
} else {
throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND);
}
}

/**
Expand Down Expand Up @@ -133,14 +138,14 @@ public boolean equals(Object other) {
}

EditCommand otherEditCommand = (EditCommand) other;
return index.equals(otherEditCommand.index)
return nric.equals(otherEditCommand.nric)
&& editPersonDescriptor.equals(otherEditCommand.editPersonDescriptor);
}

@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("nric", nric)
.add("editPersonDescriptor", editPersonDescriptor)
.toString();
}
Expand Down
45 changes: 25 additions & 20 deletions src/main/java/seedu/address/logic/commands/LogCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,14 @@
import static seedu.address.model.Model.PREDICATE_SHOW_ALL_PERSONS;

import java.util.List;
import java.util.Optional;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.util.ToStringBuilder;
import seedu.address.logic.Messages;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Log;
import seedu.address.model.person.Nric;
import seedu.address.model.person.Person;

/**
Expand All @@ -19,22 +20,22 @@
public class LogCommand extends Command {
public static final String COMMAND_WORD = "log";
public static final String MESSAGE_USAGE = COMMAND_WORD + ": Logs information of a patient.\n"
+ "Parameters: INDEX (must be a positive integer), "
+ "Parameters: NRIC, "
+ "TIMESTAMP (in the format DD-MM-YYYY HH:MM),"
+ "INFO (non-empty)\n"
+ "Example: " + COMMAND_WORD + " 1 25-12-2024 14:30 Attended appointment";
+ "Example: " + COMMAND_WORD + " S1234567A 25-12-2024 14:30 Attended appointment";

private final Index index;
private final Nric nric;
private final Log log;

/**
* Creates a LogCommand to add the specified {@code Log} to the person at the specified {@code Index}.
*
* @param targetIndex The index of the person in the filtered person list.
* @param targetNric The nric of the person in the filtered person list.
* @param log The log entry to add.
*/
public LogCommand(Index targetIndex, Log log) {
this.index = targetIndex;
public LogCommand(Nric targetNric, Log log) {
this.nric = targetNric;
this.log = log;
}

Expand All @@ -51,18 +52,22 @@ public CommandResult execute(Model model) throws CommandException {

List<Person> lastShownList = model.getFilteredPersonList();

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
}
Optional<Person> personWithMatchingNric = lastShownList.stream()
.filter(person -> nric.equals(person.getNric()))
.findFirst();

Person personToEdit = lastShownList.get(index.getZeroBased());
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(),
personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(),
personToEdit.getAppointment(), personToEdit.getLogEntries().addLog(log));
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult("Log added to " + personToEdit.getName());
if (personWithMatchingNric.isPresent()) {
Person personToEdit = personWithMatchingNric.get();
Person editedPerson = new Person(
personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(), personToEdit.getNric(),
personToEdit.getAddress(), personToEdit.getRemark(), personToEdit.getTags(),
personToEdit.getAppointment(), personToEdit.getLogEntries().addLog(log));
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
return new CommandResult("Log added to " + personToEdit.getName());
} else {
throw new CommandException(Messages.MESSAGE_NO_PERSON_FOUND);
}
}

/**
Expand All @@ -75,7 +80,7 @@ public CommandResult execute(Model model) throws CommandException {
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof LogCommand // instanceof handles nulls
&& index.equals(((LogCommand) other).index)
&& nric.equals(((LogCommand) other).nric)
&& log.equals(((LogCommand) other).log));
}

Expand All @@ -87,7 +92,7 @@ public boolean equals(Object other) {
@Override
public String toString() {
return new ToStringBuilder(this)
.add("index", index)
.add("nric", nric)
.add("log", log)
.toString();
}
Expand Down
Loading

0 comments on commit dcccb18

Please sign in to comment.