Skip to content

Commit

Permalink
Merge pull request #144 from tanveersingh10/master
Browse files Browse the repository at this point in the history
Modify EditCmmd, RemarkCmmd, AddApptCommand
  • Loading branch information
tanveersingh10 committed Nov 2, 2023
2 parents ca0d87c + ae4b669 commit a1dd082
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 64 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,34 +55,35 @@ public AddAppointmentCommand(Appointment appointment) {
@Override
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);

Patient chosenPatient = findPatient(model);
Doctor chosenDoctor = findDoctor(model);
checkPatientAndDoctor(chosenPatient, chosenDoctor);
checkValidAppointment(chosenPatient, chosenDoctor, toAdd);
chosenPatient.addAppointment(toAdd);
chosenDoctor.addAppointment(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

private void checkValidAppointment(Patient chosenPatient, Doctor chosenDoctor, Appointment toAdd)
throws CommandException {
if (chosenPatient.hasAppointmentAt(toAdd.getAppointmentTime())) {
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT_PATIENT);
}
if (chosenDoctor.hasAppointmentAt(toAdd.getAppointmentTime())) {
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT_DOCTOR);
}
}
private void checkPatientAndDoctor(Patient chosenPatient, Doctor chosenDoctor) throws CommandException {
if (chosenPatient == null) {
throw new CommandException(MESSAGE_INVALID_PATIENT);
}
if (chosenDoctor == null) {
throw new CommandException(MESSAGE_INVALID_DOCTOR);
}

// check that patient and doctor are not the same person
if (chosenPatient.isSamePerson(chosenDoctor)) {
throw new CommandException(MESSAGE_SAME_DOCTOR_AND_PATIENT);
}

// check that the patient and doctor do not have appointment scheduled at the same time
if (chosenPatient.hasAppointmentAt(toAdd.getAppointmentTime())) {
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT_PATIENT);
}
if (chosenDoctor.hasAppointmentAt(toAdd.getAppointmentTime())) {
throw new CommandException(MESSAGE_DUPLICATE_APPOINTMENT_DOCTOR);
}
// add appointment to the specified doctor's appointment set
// add appointment to the specified patient's appointment set
chosenPatient.addAppointment(toAdd);
chosenDoctor.addAppointment(toAdd);
model.addAppointment(toAdd);
return new CommandResult(String.format(MESSAGE_SUCCESS, toAdd));
}

private Patient findPatient(Model model) {
Expand Down
29 changes: 15 additions & 14 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,6 @@ public class EditCommand extends Command {
private static final Logger logger = LogsCenter.getLogger(EditCommand.class.getName());
private final Ic nric;
private final EditPersonDescriptor editPersonDescriptor;
private String personRole;

/**
* @param nric of the person in the filtered person list to edit
Expand All @@ -94,53 +93,55 @@ public EditCommand(Ic nric, EditPersonDescriptor editPersonDescriptor) {
public CommandResult execute(Model model) throws CommandException {
requireNonNull(model);
// combine doctor list and patient list
Person personToEdit = getPersonToEdit(model);
Person editedPerson = getEditedPerson(model, personToEdit);

model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
logger.info("Successfully edited person");
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
}

private Person getPersonToEdit(Model model) throws CommandException {
List<Person> lastShownList = new ArrayList<>();
lastShownList.addAll(model.getFilteredDoctorList());
lastShownList.addAll(model.getFilteredPatientList());

List<Person> personToEditList = lastShownList.stream()
.filter(x -> x.getIc().equals(nric))
.collect(Collectors.toList());

if (personToEditList.size() == 0) {
logger.warning("Could not edit - person isn't in adressbook");
throw new CommandException(MESSAGE_DOESNT_EXIST);
}

//developer assumption - can't have 2 people with same IC
assert personToEditList.size() < 2;

Person personToEdit = personToEditList.get(0);
Person editedPerson;
return personToEdit;
}

private Person getEditedPerson(Model model, Person personToEdit) throws CommandException {
Person editedPerson;
if (personToEdit instanceof Patient) {
personRole = "patient";
editedPerson = createEditedPatient((Patient) personToEdit, editPersonDescriptor);
} else {
assert personToEdit.isDoctor();
if (editPersonDescriptor.getCondition().isPresent() || editPersonDescriptor.getBloodType().isPresent()) {
logger.warning("Error thrown - tried to edit condition / bloodtype of doctor");
throw new CommandException("Doctors cannot have Condition or BloodType fields.");
}
personRole = "doctor";
editedPerson = createEditedDoctor((Doctor) personToEdit, editPersonDescriptor);
}

if (!personToEdit.isSamePerson(editedPerson) && model.hasPerson(editedPerson)) {
logger.warning("Edited Person and orignal person are the same");
throw new CommandException(MESSAGE_DUPLICATE_PERSON);
}
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS);
logger.info("Successfully edited person");
return new CommandResult(String.format(MESSAGE_EDIT_PERSON_SUCCESS, Messages.format(editedPerson)));
return editedPerson;
}

/**
* Creates and returns a {@code Person} with the details of {@code personToEdit}
* edited with {@code editPersonDescriptor}.
*/

private static Doctor createEditedDoctor(Doctor personToEdit, EditPersonDescriptor editPersonDescriptor) {
requireNonNull(personToEdit);
requireNonNull(personToEdit);
Expand Down
75 changes: 45 additions & 30 deletions src/main/java/seedu/address/logic/commands/RemarkCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,14 @@

import java.util.ArrayList;
import java.util.List;
import java.util.logging.Logger;
import java.util.stream.Collectors;

import seedu.address.commons.core.index.Index;
import seedu.address.logic.Messages;
import seedu.address.commons.core.LogsCenter;
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.model.Model;
import seedu.address.model.person.Doctor;
import seedu.address.model.person.Ic;
import seedu.address.model.person.Patient;
import seedu.address.model.person.Person;
import seedu.address.model.person.Remark;
Expand All @@ -33,56 +35,69 @@ public class RemarkCommand extends Command {
+ "r/ Likes to swim.";

public static final String MESSAGE_ADD_REMARK_SUCCESS = "Added remark to Person: %1$s";
public static final String MESSAGE_DOESNT_EXIST = "This person hasn't been saved";
public static final String MESSAGE_DELETE_REMARK_SUCCESS = "Removed remark from Person: %1$s";

private final Index index;
private static final Logger logger = LogsCenter.getLogger(RemarkCommand.class);
private final Ic nric;
private final Remark remark;

/**
* @param index of the person in the filtered person list to edit the remark
* @param nric of the person in the filtered person list to edit the remark
* @param remark of the person to be updated to
*/
public RemarkCommand(Index index, Remark remark) {
requireAllNonNull(index, remark);
public RemarkCommand(Ic nric, Remark remark) {
requireAllNonNull(nric, remark);

this.index = index;
this.nric = nric;
this.remark = remark;
}

@Override
public CommandResult execute(Model model) throws CommandException {
// combine doctor list and patient list
Person personToEdit = getPersonToEdit(model);
Person editedPerson = getEditedPerson(model, personToEdit);
model.setPerson(personToEdit, editedPerson);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
logger.info("Successfully added remark to person");
return new CommandResult(generateSuccessMessage(editedPerson));
}
private Person getPersonToEdit(Model model) throws CommandException {
List<Person> lastShownList = new ArrayList<>();
lastShownList.addAll(model.getFilteredDoctorList());
lastShownList.addAll(model.getFilteredPatientList());

if (index.getZeroBased() >= lastShownList.size()) {
throw new CommandException(Messages.MESSAGE_INVALID_PERSON_DISPLAYED_INDEX);
List<Person> personToEditList = lastShownList.stream()
.filter(x -> x.getIc().equals(nric))
.collect(Collectors.toList());
if (personToEditList.size() == 0) {
logger.warning("Could not edit - person isn't in adressbook");
throw new CommandException(MESSAGE_DOESNT_EXIST);
}
//developer assumption - can't have 2 people with same IC
assert personToEditList.size() < 2;
Person personToEdit = personToEditList.get(0);
return personToEdit;
}

Person personToEdit = lastShownList.get(index.getZeroBased());
private Person getEditedPerson(Model model, Person personToEdit) {
requireAllNonNull(model, personToEdit);
Person editedPerson;
if (personToEdit.isDoctor()) {
Doctor editedDoctor = new Doctor(personToEdit.getName(), personToEdit.getPhone(), personToEdit.getEmail(),
personToEdit.getAddress(), remark, personToEdit.getGender(),
personToEdit.getIc(), personToEdit.getAppointments(), personToEdit.getTags());
model.setPerson(personToEdit, editedDoctor);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);
editedPerson = editedDoctor;
} else {
assert personToEdit.isPatient();
Patient patientToEdit = (Patient) personToEdit;
Patient editedPatient = new Patient(patientToEdit.getName(), patientToEdit.getPhone(),
patientToEdit.getEmergencyContact(), patientToEdit.getEmail(), patientToEdit.getAddress(),
patientToEdit.getRemark(), patientToEdit.getGender(), patientToEdit.getIc(),
patientToEdit.getCondition(), patientToEdit.getBloodType(), patientToEdit.getAppointments(),
patientToEdit.getTags());
editedPerson = editedPatient;
}
@SuppressWarnings("unchecked") //Since Person is abstract, every Person is either a Patient or a Doctor
Patient editedPerson = (Patient) personToEdit;
Patient editedPatient = new Patient(editedPerson.getName(), editedPerson.getPhone(),
editedPerson.getEmergencyContact(), editedPerson.getEmail(), editedPerson.getAddress(),
editedPerson.getRemark(), editedPerson.getGender(), editedPerson.getIc(),
editedPerson.getCondition(), editedPerson.getBloodType(), editedPerson.getAppointments(),
editedPerson.getTags());

model.setPerson(personToEdit, editedPatient);
model.updateFilteredPersonList(Model.PREDICATE_SHOW_ALL_PERSONS);

return new CommandResult(generateSuccessMessage(editedPatient));

return editedPerson;
}

/**
* Generates a command execution success message based on whether
* the remark is added to or removed from
Expand All @@ -105,7 +120,7 @@ public boolean equals(Object other) {
}

RemarkCommand e = (RemarkCommand) other;
return index.equals(e.index)
return nric.equals(e.nric)
&& remark.equals(e.remark);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@
import static seedu.address.logic.Messages.MESSAGE_INVALID_COMMAND_FORMAT;
import static seedu.address.logic.parser.CliSyntax.PREFIX_REMARK;

import seedu.address.commons.core.index.Index;
import seedu.address.commons.exceptions.IllegalValueException;
import seedu.address.logic.commands.RemarkCommand;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.person.Ic;
import seedu.address.model.person.Remark;

/**
Expand All @@ -29,9 +29,9 @@ public RemarkCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap = ArgumentTokenizer.tokenize(args,
PREFIX_REMARK);

Index index;
Ic nric;
try {
index = ParserUtil.parseIndex(argMultimap.getPreamble());
nric = ParserUtil.parseIc(argMultimap.getPreamble());
} catch (IllegalValueException ive) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
RemarkCommand.MESSAGE_USAGE), ive);
Expand All @@ -40,6 +40,6 @@ public RemarkCommand parse(String args) throws ParseException {
String remarkString = argMultimap.getValue(PREFIX_REMARK).orElse("");
Remark remark = new Remark(remarkString);

return new RemarkCommand(index, remark);
return new RemarkCommand(nric, remark);
}
}

0 comments on commit a1dd082

Please sign in to comment.