Skip to content

Commit

Permalink
Merge pull request AY2425S1-CS2103T-W10-2#87 from jayjay19630/jo-an/a…
Browse files Browse the repository at this point in the history
…dd-list-command

Refactor Model/Storage and Add List Command
  • Loading branch information
choiwab authored Oct 17, 2024
2 parents a97bdad + cff49cf commit 5971c53
Show file tree
Hide file tree
Showing 57 changed files with 890 additions and 337 deletions.
6 changes: 4 additions & 2 deletions src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -98,12 +98,14 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
initialPersonsData = new AddressBook();
}
try {
appointmentBookOptional = storage.readAppointmentBook();
appointmentBookOptional = storage.readAppointmentBook(initialPersonsData);
if (!appointmentBookOptional.isPresent()) {
logger.info("Creating a new data file " + storage.getAppointmentBookFilePath()
+ " populated with a sample AppointmentBook.");
initialAppointmentData = SampleDataUtil.getSampleAppointmentBook(initialPersonsData);
} else {
initialAppointmentData = appointmentBookOptional.get();
}
initialAppointmentData = appointmentBookOptional.orElseGet(SampleDataUtil::getSampleAppointmentBook);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAppointmentBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AppointmentBook.");
Expand Down
9 changes: 9 additions & 0 deletions src/main/java/seedu/address/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import seedu.address.logic.commands.exceptions.CommandException;
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.person.Person;

/**
Expand All @@ -33,11 +34,19 @@ public interface Logic {
/** Returns an unmodifiable view of the filtered list of persons */
ObservableList<Person> getFilteredPersonList();

/** Returns an unmodifiable view of the filtered list of appointments */
ObservableList<Appointment> getFilteredAppointmentList();

/**
* Returns the user prefs' address book file path.
*/
Path getAddressBookFilePath();

/**
* Returns the user prefs' address book file path.
*/
Path getAppointmentBookFilePath();

/**
* Returns the user prefs' GUI settings.
*/
Expand Down
12 changes: 12 additions & 0 deletions src/main/java/seedu/address/logic/LogicManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import seedu.address.logic.parser.exceptions.ParseException;
import seedu.address.model.Model;
import seedu.address.model.ReadOnlyAddressBook;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.person.Person;
import seedu.address.storage.Storage;

Expand Down Expand Up @@ -52,6 +53,7 @@ public CommandResult execute(String commandText) throws CommandException, ParseE

try {
storage.saveAddressBook(model.getAddressBook());
storage.saveAppointmentBook(model.getAppointmentBook());
} catch (AccessDeniedException e) {
throw new CommandException(String.format(FILE_OPS_PERMISSION_ERROR_FORMAT, e.getMessage()), e);
} catch (IOException ioe) {
Expand All @@ -71,11 +73,21 @@ public ObservableList<Person> getFilteredPersonList() {
return model.getFilteredPersonList();
}

@Override
public ObservableList<Appointment> getFilteredAppointmentList() {
return model.getFilteredAppointmentList();
}

@Override
public Path getAddressBookFilePath() {
return model.getAddressBookFilePath();
}

@Override
public Path getAppointmentBookFilePath() {
return model.getAppointmentBookFilePath();
}

@Override
public GuiSettings getGuiSettings() {
return model.getGuiSettings();
Expand Down
10 changes: 8 additions & 2 deletions src/main/java/seedu/address/logic/Messages.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import java.util.stream.Stream;

import seedu.address.logic.parser.Prefix;
import seedu.address.model.appointment.Appointment;
import seedu.address.model.appointment.AppointmentDescriptor;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonDescriptor;
Expand Down Expand Up @@ -58,14 +59,19 @@ public static String formatPerson(PersonDescriptor person) {
return builder.toString();
}

/**
* Formats the {@code appointment} for display to the user.
*/
public static String formatAppointment(Appointment appointment) {
return formatAppointment(appointment.getAppointmentDescriptor());
}

/**
* Formats the {@code appointment} for display to the user.
*/
public static String formatAppointment(AppointmentDescriptor appointment) {
final StringBuilder builder = new StringBuilder();
builder.append(appointment.getAppointmentType())
.append("; Id: ")
.append(appointment.getPersonId())
.append("; Date and Time")
.append(appointment.getAppointmentDateTime())
.append("; Sickness: ")
Expand Down
Original file line number Diff line number Diff line change
@@ -1,49 +1,58 @@
package seedu.address.logic.commands;

import static java.util.Objects.requireNonNull;
import static seedu.address.commons.util.CollectionUtil.requireAllNonNull;
import static seedu.address.logic.parser.CliSyntax.PREFIX_APPOINTMENT_TYPE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_DATETIME;
import static seedu.address.logic.parser.CliSyntax.PREFIX_MEDICINE;
import static seedu.address.logic.parser.CliSyntax.PREFIX_PERSON_ID;
import static seedu.address.logic.parser.CliSyntax.PREFIX_SICKNESS;

import java.util.Optional;

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.appointment.AppointmentDescriptor;
import seedu.address.model.person.Person;

/**
* Adds an appointment to the appointment book.
*/
public class AddAppointmentCommand extends AddCommand {
public static final String COMMAND_WORD = "add appt";
public static final String MESSAGE_USAGE = COMMAND_WORD
+ ": Adds an appointment to the appointment book. "
+ ": Adds an appointment to the appointment book. \n"
+ "Parameters: "
+ PREFIX_APPOINTMENT_TYPE + "Appointment_TYPE"
+ PREFIX_PERSON_ID + "PersonId"
+ PREFIX_DATETIME + "Date"
+ PREFIX_SICKNESS + "Sickness"
+ PREFIX_MEDICINE + "Medicine"
+ PREFIX_APPOINTMENT_TYPE + "AppointmentType "
+ PREFIX_PERSON_ID + "PersonId "
+ PREFIX_DATETIME + "Date "
+ PREFIX_SICKNESS + "Sickness "
+ PREFIX_MEDICINE + "Medicine \n"
+ "Example: " + COMMAND_WORD
+ PREFIX_APPOINTMENT_TYPE + "Check up"
+ PREFIX_DATETIME + "16/10/2024 12:00"
+ PREFIX_DATETIME + "2024-10-16 12:30:30"
+ PREFIX_PERSON_ID + "1"
+ PREFIX_SICKNESS + "Common Cold"
+ PREFIX_MEDICINE + "Paracetamol";

public static final String MESSAGE_SUCCESS = "New appointment added: %1$s";
public static final String MESSAGE_DUPLICATE_APPOINTMENT = "This appointment already exists in the address book";
public static final String MESSAGE_DUPLICATE_APPOINTMENT =
"This appointment already exists in the appointment book";
public static final String MESSAGE_PERSON_NOT_FOUND =
"This person ID does not belong to anyone in the address book";

private final AppointmentDescriptor toAdd;
private final int personId;

/**
* Creates an AddAppointmentCommand to add the specified {@code Appointment}
*/
public AddAppointmentCommand(AppointmentDescriptor appointment) {
requireNonNull(appointment);
toAdd = appointment;
public AddAppointmentCommand(AppointmentDescriptor appointmentDescriptor, int personId) {
requireAllNonNull(appointmentDescriptor, personId);
this.toAdd = appointmentDescriptor;
this.personId = personId;
}

/*
Expand All @@ -58,8 +67,12 @@ protected boolean alreadyExists(Model model) {
* Adds the entity to the model.
*/
@Override
protected void addEntity(Model model) {
model.addAppointment(toAdd);
protected void addEntity(Model model) throws CommandException {
Optional<Person> personOptional = model.findPerson(personId);
if (!personOptional.isPresent()) {
throw new CommandException(getPersonIdDoesNotExistMessage());
}
model.addAppointment(personOptional.get(), toAdd);
};

/*
Expand All @@ -78,6 +91,13 @@ protected String getDuplicateEntityMessage() {
return MESSAGE_DUPLICATE_APPOINTMENT;
};

/*
* Returns the message to display when the person ID does not exist.
*/
protected String getPersonIdDoesNotExistMessage() {
return MESSAGE_PERSON_NOT_FOUND;
};

/**
* Formats the entity for displaying in the success message.
*/
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/commands/AddCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ public CommandResult execute(Model model) throws CommandException {
/*
* Adds the entity to the model.
*/
protected abstract void addEntity(Model model);
protected abstract void addEntity(Model model) throws CommandException;

/*
* Returns success message to display upon adding entity.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
public class AddPersonCommand extends AddCommand {

public static final String MESSAGE_USAGE = COMMAND_WORD + " " + "person"
+ ": Adds a person to the address book. "
+ ": Adds a person to the address book. \n"
+ "Parameters: "
+ PREFIX_NAME + "NAME "
+ PREFIX_PHONE + "PHONE "
Expand Down Expand Up @@ -54,7 +54,7 @@ protected boolean alreadyExists(Model model) {

@Override
protected void addEntity(Model model) {
personId = model.addPerson(toAdd);
personId = model.addPerson(toAdd).getPersonId();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
*/
public class DeletePersonCommand extends DeleteCommand {

public static final String MESSAGE_USAGE = COMMAND_WORD
public static final String MESSAGE_USAGE = COMMAND_WORD + " person"
+ ": Deletes the person identified by the index number used in the displayed person list.\n"
+ "Parameters: INDEX (must be a positive integer)\n"
+ "Example: " + COMMAND_WORD + " " + "person" + " " + "1";
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/address/logic/commands/EditCommand.java
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ public abstract class EditCommand extends Command {
+ "Existing values will be overwritten by the input values.\n"
+ "Parameters: INDEX (must be a positive integer) "
+ "ENTITY_TYPE "
+ "INDEX"
+ "ENTITY_ARGUMENTS..."
+ "INDEX "
+ "ENTITY_ARGUMENTS...\n"
+ "Example: " + COMMAND_WORD + " " + "person" + " " + "1"
+ "[" + PREFIX_NAME + "NAME] "
+ "[" + PREFIX_PHONE + "PHONE] "
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import seedu.address.model.person.Email;
import seedu.address.model.person.Name;
import seedu.address.model.person.Person;
import seedu.address.model.person.PersonDescriptor;
import seedu.address.model.person.Phone;
import seedu.address.model.tag.Tag;

Expand Down Expand Up @@ -72,13 +71,14 @@ protected Object createEditedEntity(Model model, Object personToEdit,
EditPersonDescriptor editPersonDescriptorCasted = (EditPersonDescriptor) editPersonDescriptor;
Person personToEditCasted = (Person) personToEdit;

int personId = personToEditCasted.getPersonId();
Name updatedName = editPersonDescriptorCasted.getName().orElse(personToEditCasted.getName());
Phone updatedPhone = editPersonDescriptorCasted.getPhone().orElse(personToEditCasted.getPhone());
Email updatedEmail = editPersonDescriptorCasted.getEmail().orElse(personToEditCasted.getEmail());
Address updatedAddress = editPersonDescriptorCasted.getAddress().orElse(personToEditCasted.getAddress());
Set<Tag> updatedTags = editPersonDescriptorCasted.getTags().orElse(personToEditCasted.getTags());

return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags);
return new Person(updatedName, updatedPhone, updatedEmail, updatedAddress, updatedTags, personId);
}

@Override
Expand All @@ -98,8 +98,8 @@ protected String getInvalidIndexMessage() {

@Override
protected String formatEntity(Object entity) {
assert entity instanceof PersonDescriptor;
return Messages.formatPerson((PersonDescriptor) entity);
assert entity instanceof Person;
return Messages.formatPerson((Person) entity);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package seedu.address.logic.commands;

import static seedu.address.model.Model.PREDICATE_SHOW_ALL_APPOINTMENTS;

import seedu.address.model.Model;

/**
* Lists all persons in the address book.
*/
public class ListAppointmentCommand extends ListCommand {
public static final String MESSAGE_SUCCESS = "Listed all appointments!";

/**
* Lists all appoinments in the appointment book to the user.*
* @param model {@code Model} which the ListAppointmentCommand should operate on.
* */
@Override
protected void listEntity(Model model) {

model.updateFilteredAppointmentList(PREDICATE_SHOW_ALL_APPOINTMENTS);
}

/**
* Returns the message to be displayed after listing all appointments.
*
* @return the message to be displayed after listing all appointments.
*/
@Override
protected String getSuccessMessage() {
return MESSAGE_SUCCESS;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,10 +83,10 @@ public AddCommand parse(String args) throws ParseException {
Sickness sickness = ParserUtil.parseSickness(argMultimap.getValue(PREFIX_SICKNESS).get());
Medicine medicine = ParserUtil.parseMedicine(argMultimap.getValue(PREFIX_MEDICINE).get());

AppointmentDescriptor appointment = new AppointmentDescriptor(
appointmentType, appointmentDateTime, personId, sickness, medicine);
AppointmentDescriptor appointmentDescriptor = new AppointmentDescriptor(
appointmentType, appointmentDateTime, sickness, medicine);

return new AddAppointmentCommand(appointment);
return new AddAppointmentCommand(appointmentDescriptor, personId);
default:
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddCommand.MESSAGE_USAGE));
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/logic/parser/CliSyntax.java
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ public class CliSyntax {

public static final Prefix PREFIX_PERSON_ID = new Prefix("i/");
public static final Prefix PREFIX_DATETIME = new Prefix("d/");
public static final Prefix PREFIX_APPOINTMENT_TYPE = new Prefix("t/");
public static final Prefix PREFIX_APPOINTMENT_TYPE = new Prefix("ty/");
public static final Prefix PREFIX_MEDICINE = new Prefix("m/");
public static final Prefix PREFIX_SICKNESS = new Prefix("s/");
}
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ public EditCommand parse(String args) throws ParseException {
String trimmedArgs = args.trim();
String[] splitArgs = trimmedArgs.split("\\s+");

if (splitArgs.length < 3) {
if (splitArgs.length < 2) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, EditCommand.MESSAGE_USAGE));
}

Expand All @@ -50,10 +50,6 @@ public EditCommand parse(String args) throws ParseException {
ArgumentMultimap argMultimap =
ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL,
PREFIX_ADDRESS, PREFIX_TAG);
if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL)) {
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
EditPersonCommand.MESSAGE_USAGE));
}
Index index = ParserUtil.parseIndex(indexString);
argMultimap.verifyNoDuplicatePrefixesFor(PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import static seedu.address.logic.parser.ParserUtil.APPOINTMENT_ENTITY_STRING;
import static seedu.address.logic.parser.ParserUtil.PERSON_ENTITY_STRING;

import seedu.address.logic.commands.ListAppointmentCommand;
import seedu.address.logic.commands.ListCommand;
import seedu.address.logic.commands.ListPersonCommand;
import seedu.address.logic.parser.exceptions.ParseException;
Expand All @@ -27,7 +28,7 @@ public ListCommand parse(String args) throws ParseException {
case PERSON_ENTITY_STRING:
return new ListPersonCommand();
case APPOINTMENT_ENTITY_STRING:
//TODO: Instantiate and return ListAppointmentCommand
return new ListAppointmentCommand();
default:
throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT,
ListCommand.MESSAGE_USAGE));
Expand Down
Loading

0 comments on commit 5971c53

Please sign in to comment.