diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index 45e69b521934..b4a9770204d8 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -22,7 +22,9 @@ import seedu.address.model.ModelManager; import seedu.address.model.ReadOnlyAddressBook; import seedu.address.model.ReadOnlyHealthWorkerBook; +import seedu.address.model.ReadOnlyRequestBook; import seedu.address.model.ReadOnlyUserPrefs; +import seedu.address.model.RequestBook; import seedu.address.model.UserPrefs; import seedu.address.model.util.SampleDataUtil; import seedu.address.storage.AddressBookStorage; @@ -88,12 +90,20 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) { Optional healthWorkerBookOptional; ReadOnlyAddressBook initialAddressBook; ReadOnlyHealthWorkerBook initialHealthWorkerBook; + ReadOnlyRequestBook initialRequestBook; + Optional requestBookOptional; try { addressBookOptional = storage.readAddressBook(); + requestBookOptional = storage.readRequestBook(); if (!addressBookOptional.isPresent()) { logger.info("Data file not found. Will be starting with a sample AddressBook"); } + + if (!requestBookOptional.isPresent()) { + logger.info("Request file not found. Will be starting with sample RequestBook"); + } + initialRequestBook = new RequestBook(); initialAddressBook = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook); healthWorkerBookOptional = storage.readHealthWorkerBook(); if (!addressBookOptional.isPresent()) { @@ -106,13 +116,16 @@ private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) { logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook"); initialAddressBook = new AddressBook(); initialHealthWorkerBook = new HealthWorkerBook(); + initialRequestBook = new RequestBook(); } catch (IOException e) { logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook"); initialAddressBook = new AddressBook(); initialHealthWorkerBook = new HealthWorkerBook(); + initialRequestBook = new RequestBook(); } - return new ModelManager(initialAddressBook, initialHealthWorkerBook, userPrefs); + return new ModelManager(initialAddressBook, initialHealthWorkerBook, initialRequestBook, + userPrefs); } private void initLogging(Config config) { diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index 5cb24a617beb..9d33be424674 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -65,7 +65,6 @@ public CommandResult execute(String commandText) throws CommandException, ParseE return commandResult; } - @Override public ReadOnlyAddressBook getAddressBook() { return model.getAddressBook(); diff --git a/src/main/java/seedu/address/logic/commands/request/CreateCommand.java b/src/main/java/seedu/address/logic/commands/request/CreateRequestCommand.java similarity index 53% rename from src/main/java/seedu/address/logic/commands/request/CreateCommand.java rename to src/main/java/seedu/address/logic/commands/request/CreateRequestCommand.java index 431cf97f7d3c..e3345dd002f0 100644 --- a/src/main/java/seedu/address/logic/commands/request/CreateCommand.java +++ b/src/main/java/seedu/address/logic/commands/request/CreateRequestCommand.java @@ -8,6 +8,7 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_PHONE; import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.AddCommand; import seedu.address.logic.commands.CommandResult; import seedu.address.logic.commands.exceptions.CommandException; import seedu.address.model.Model; @@ -16,30 +17,30 @@ /** * Creates a new request to the request book. */ -public class CreateCommand extends RequestCommand { +public class CreateRequestCommand extends AddCommand { - public static final String COMMAND_WORD = "create"; + public static final String COMMAND_WORD = "request"; - public static final String MESSAGE_USAGE = RequestCommand.COMMAND_WORD + " " + COMMAND_WORD - + ": Creates a new request in the request book. " + "Parameters: " - + PREFIX_NAME + "NAME " - + PREFIX_PHONE + "PHONE " - + PREFIX_ADDRESS + "ADDRESS " - + PREFIX_DATE + "DATETIME" - + PREFIX_CONDITION + "CONDITION...\n" - + "Example: " + RequestCommand.COMMAND_WORD + " " + COMMAND_WORD + " " - + PREFIX_NAME + "John Doe " - + PREFIX_PHONE + "81234567 " - + PREFIX_ADDRESS + "123, Sengkang Ave 3, #04-12, 214632 " - + PREFIX_DATE + "01-01-2019 08:00:00 " - + PREFIX_CONDITION + "Physiotherapy"; + public static final String MESSAGE_USAGE = AddCommand.COMMAND_WORD + " " + COMMAND_WORD + + ": Creates a new request in the request book. " + "Parameters: " + + PREFIX_NAME + "NAME " + + PREFIX_PHONE + "PHONE " + + PREFIX_ADDRESS + "ADDRESS " + + PREFIX_DATE + "DATETIME" + + PREFIX_CONDITION + "CONDITION...\n" + + "Example: " + RequestCommand.COMMAND_WORD + " " + COMMAND_WORD + " " + + PREFIX_NAME + "John Doe " + + PREFIX_PHONE + "81234567 " + + PREFIX_ADDRESS + "123, Sengkang Ave 3, #04-12, 214632 " + + PREFIX_DATE + "01-01-2019 08:00:00 " + + PREFIX_CONDITION + "Physiotherapy"; public static final String MESSAGE_SUCCESS = "Created new request successfully: %1$s"; public static final String MESSAGE_DUPLICATE_REQUEST = "This request already exists."; private final Request newRequest; - public CreateCommand(Request newRequest) { + public CreateRequestCommand(Request newRequest) { requireNonNull(newRequest); this.newRequest = newRequest; } @@ -55,12 +56,24 @@ public CreateCommand(Request newRequest) { @Override public CommandResult execute(Model model, CommandHistory history) throws CommandException { requireNonNull(model); - // TODO write created request into the JSON file - // if (model.hasRequest(this.newRequest)) { - // throw new CommandException(MESSAGE_DUPLICATE_REQUEST); - // } - // model.addRequest(newRequest); - // model.commitRequestBook(); + if (model.hasRequest(this.newRequest)) { + throw new CommandException(MESSAGE_DUPLICATE_REQUEST); + } + model.addRequest(newRequest); + model.commitRequestBook(); return new CommandResult(String.format(MESSAGE_SUCCESS, newRequest)); } + + @Override + public void add(Model model, Object toAdd) { + model.addRequest((Request) toAdd); + model.commitRequestBook(); + } + + @Override + public boolean equals(Object other) { + return other == this + || (other instanceof CreateRequestCommand + && newRequest.equals(((CreateRequestCommand) other).newRequest)); + } } diff --git a/src/main/java/seedu/address/logic/parser/AddCommandParser.java b/src/main/java/seedu/address/logic/parser/AddCommandParser.java index 472bec9f038f..262338624192 100644 --- a/src/main/java/seedu/address/logic/parser/AddCommandParser.java +++ b/src/main/java/seedu/address/logic/parser/AddCommandParser.java @@ -2,6 +2,8 @@ import static seedu.address.commons.core.Messages.MESSAGE_INVALID_COMMAND_FORMAT; import static seedu.address.logic.parser.CliSyntax.PREFIX_ADDRESS; +import static seedu.address.logic.parser.CliSyntax.PREFIX_CONDITION; +import static seedu.address.logic.parser.CliSyntax.PREFIX_DATE; import static seedu.address.logic.parser.CliSyntax.PREFIX_EMAIL; import static seedu.address.logic.parser.CliSyntax.PREFIX_NAME; import static seedu.address.logic.parser.CliSyntax.PREFIX_NRIC; @@ -11,11 +13,13 @@ import static seedu.address.logic.parser.CliSyntax.PREFIX_TAG; import java.util.Set; +import java.util.UUID; import java.util.stream.Stream; import seedu.address.logic.commands.AddCommand; import seedu.address.logic.commands.AddHealthWorkerCommand; import seedu.address.logic.commands.AddPersonCommand; +import seedu.address.logic.commands.request.CreateRequestCommand; import seedu.address.logic.parser.exceptions.ParseException; import seedu.address.model.person.Address; import seedu.address.model.person.Email; @@ -25,6 +29,9 @@ import seedu.address.model.person.Phone; import seedu.address.model.person.healthworker.HealthWorker; import seedu.address.model.person.healthworker.Organization; +import seedu.address.model.request.Request; +import seedu.address.model.request.RequestDate; +import seedu.address.model.request.RequestStatus; import seedu.address.model.tag.Skills; import seedu.address.model.tag.Tag; @@ -42,25 +49,42 @@ public AddCommand parse(String args) throws ParseException { CommandMode commandMode = ArgumentTokenizer.checkMode(args); if (commandMode == CommandMode.HEALTH_WORKER) { return parseAddHealthWorker(ArgumentTokenizer.trimMode(args)); + } else if (commandMode == CommandMode.REQUEST) { + return parseAddRequest(args); } - ArgumentMultimap argMultimap = - ArgumentTokenizer.tokenize(args, PREFIX_NAME, PREFIX_PHONE, PREFIX_EMAIL, PREFIX_ADDRESS, PREFIX_TAG); + Person person = getPersonFromArgs(args); - if (!arePrefixesPresent(argMultimap, PREFIX_NAME, PREFIX_ADDRESS, PREFIX_PHONE, PREFIX_EMAIL) - || !argMultimap.getPreamble().isEmpty()) { - throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPersonCommand.MESSAGE_USAGE)); + return new AddPersonCommand(person); + } + + /** + * Parses the given {@code String} of arguments in the context of the CreateRequestCommand + * and returns an CreateReqeustCommand object for execution. + * @throws ParseException if the suer input does not conform the expected format + */ + private CreateRequestCommand parseAddRequest(String args) throws ParseException { + UUID uuid = UUID.randomUUID(); + String requestId = uuid.toString(); + + Person patient = getPersonFromArgs(args); + + ArgumentMultimap argumentMultimap = ArgumentTokenizer.tokenize(args, PREFIX_DATE, + PREFIX_CONDITION); + + if (!arePrefixesPresent(argumentMultimap, PREFIX_DATE, PREFIX_CONDITION)) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, + CreateRequestCommand.MESSAGE_USAGE)); } - Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); - Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()); - Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()); - Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); - Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); + RequestDate requestDate = + ParserUtil.parseRequestDate(argumentMultimap.getValue(PREFIX_DATE).get()); - Person person = new Person(name, phone, email, address, tagList); + Set conditions = ParserUtil.parseTags(argumentMultimap.getAllValues(PREFIX_CONDITION)); + + return new CreateRequestCommand(new Request(requestId, patient, null, requestDate, conditions, + new RequestStatus("PENDING"))); - return new AddPersonCommand(person); } /** @@ -99,6 +123,28 @@ private AddHealthWorkerCommand parseAddHealthWorker(String args) throws ParseExc return new AddHealthWorkerCommand(healthWorker); } + /** + * Extracts a Person object from the given object and returns it. + * @throws ParseException if there are invalid/unfilled fields. + */ + private Person getPersonFromArgs(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) + || !argMultimap.getPreamble().isEmpty()) { + throw new ParseException(String.format(MESSAGE_INVALID_COMMAND_FORMAT, AddPersonCommand.MESSAGE_USAGE)); + } + + Name name = ParserUtil.parseName(argMultimap.getValue(PREFIX_NAME).get()); + Phone phone = ParserUtil.parsePhone(argMultimap.getValue(PREFIX_PHONE).get()); + Email email = ParserUtil.parseEmail(argMultimap.getValue(PREFIX_EMAIL).get()); + Address address = ParserUtil.parseAddress(argMultimap.getValue(PREFIX_ADDRESS).get()); + Set tagList = ParserUtil.parseTags(argMultimap.getAllValues(PREFIX_TAG)); + + return new Person(name, phone, email, address, tagList); + } + /** * Returns true if none of the prefixes contains empty {@code Optional} values in the given * {@code ArgumentMultimap}. diff --git a/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java b/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java index ee4451bc7704..dac8a8f5bf21 100644 --- a/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java +++ b/src/main/java/seedu/address/logic/parser/ArgumentTokenizer.java @@ -7,6 +7,7 @@ import java.util.ArrayList; import java.util.Arrays; +import java.util.Comparator; import java.util.List; import java.util.Map; import java.util.stream.Collectors; @@ -135,7 +136,7 @@ private static int findPrefixPosition(String argsString, String prefix, int from private static ArgumentMultimap extractArguments(String argsString, List prefixPositions) { // Sort by start position - prefixPositions.sort((prefix1, prefix2) -> prefix1.getStartPosition() - prefix2.getStartPosition()); + prefixPositions.sort(Comparator.comparingInt(PrefixPosition::getStartPosition)); // Insert a PrefixPosition to represent the preamble PrefixPosition preambleMarker = new PrefixPosition(new Prefix(""), 0); diff --git a/src/main/java/seedu/address/logic/parser/CliSyntax.java b/src/main/java/seedu/address/logic/parser/CliSyntax.java index 48e0c7145f8f..6f968f364efe 100644 --- a/src/main/java/seedu/address/logic/parser/CliSyntax.java +++ b/src/main/java/seedu/address/logic/parser/CliSyntax.java @@ -15,7 +15,8 @@ public class CliSyntax { public static final Prefix PREFIX_DATE = new Prefix("dt/"); public static final Prefix PREFIX_HEALTHWORKER = new Prefix("hw/"); public static final Prefix PREFIX_STATUS = new Prefix("st/"); - public static final Prefix PREFIX_CONDITION = new Prefix("o/"); + public static final Prefix PREFIX_PATIENT = new Prefix("pt/"); + public static final Prefix PREFIX_CONDITION = new Prefix("c/"); // Additional prefixes for HealthWorker and Patient class public static final Prefix PREFIX_ORGANIZATION = new Prefix("o/"); diff --git a/src/main/java/seedu/address/logic/parser/ParserUtil.java b/src/main/java/seedu/address/logic/parser/ParserUtil.java index 2f017bed5e44..1dd455d985f1 100644 --- a/src/main/java/seedu/address/logic/parser/ParserUtil.java +++ b/src/main/java/seedu/address/logic/parser/ParserUtil.java @@ -15,6 +15,7 @@ import seedu.address.model.person.Nric; import seedu.address.model.person.Phone; import seedu.address.model.person.healthworker.Organization; +import seedu.address.model.request.RequestDate; import seedu.address.model.tag.Skills; import seedu.address.model.tag.Specialisation; import seedu.address.model.tag.Tag; @@ -54,6 +55,16 @@ public static Name parseName(String name) throws ParseException { return new Name(trimmedName); } + /** + * Parses a {@code String id} and returns it's respective index in the addressbook patient list. + * @return + */ + public static int parsePatientIndex(String id) { + requireNonNull(id); + String trimmedId = id.trim(); + return Integer.parseInt(trimmedId); + } + /** * Parses a {@code String phone} into a {@code Phone}. * Leading and trailing whitespaces will be trimmed. @@ -190,4 +201,20 @@ public static Skills parseSpecialisations(Collection specialisations) } return skills; } + + /** + * Parses {@code String date} into a {@code RequestDate}. + * Leading and trailing whitespaces will be trimmed. + * + * @throws ParseException if the given {@code date} is invalid. + */ + public static RequestDate parseRequestDate(String date) throws ParseException { + requireNonNull(date); + String trimmedDate = date.trim(); + if (!RequestDate.isValidDate(trimmedDate)) { + throw new ParseException(RequestDate.MESSAGE_DATE_CONSTRAINTS); + } + + return new RequestDate(trimmedDate); + } } diff --git a/src/main/java/seedu/address/model/AddressBook.java b/src/main/java/seedu/address/model/AddressBook.java index c64a596b467b..92ef14d852cb 100644 --- a/src/main/java/seedu/address/model/AddressBook.java +++ b/src/main/java/seedu/address/model/AddressBook.java @@ -117,6 +117,23 @@ protected void indicateModified() { invalidationListenerManager.callListeners(this); } + //// util methods + /** + * Returns an unmodifiable view of the healthworkers list. + * This list will not contain any duplicate healthworkers + */ + public ObservableList getHealthWorkerList() { + return healthWorkers.asUnmodifiableObservableList(); + } + + public Person getPersonAt(int index) { + return this.persons.getAt(index); + } + + public HealthWorker getHealthWorkerAt(int index) { + return this.healthWorkers.getAt(index); + } + @Override public String toString() { return persons.asUnmodifiableObservableList().size() + " persons"; diff --git a/src/main/java/seedu/address/model/Model.java b/src/main/java/seedu/address/model/Model.java index 732d2b0954a9..0c2d1f442f97 100644 --- a/src/main/java/seedu/address/model/Model.java +++ b/src/main/java/seedu/address/model/Model.java @@ -222,4 +222,6 @@ public interface Model { * request in the request book. */ void setRequest(Request target, Request editedRequest); + + void commitRequestBook(); } diff --git a/src/main/java/seedu/address/model/ModelManager.java b/src/main/java/seedu/address/model/ModelManager.java index ee2436b0b095..2edc9636f330 100644 --- a/src/main/java/seedu/address/model/ModelManager.java +++ b/src/main/java/seedu/address/model/ModelManager.java @@ -29,18 +29,23 @@ public class ModelManager implements Model { private final VersionedAddressBook versionedAddressBook; private final VersionedHealthWorkerBook versionedHealthWorkerBook; + private final VersionedRequestBook versionedRequestBook; + private final UserPrefs userPrefs; + private final FilteredList filteredPersons; private final FilteredList filteredHealthWorkers; // TODO make the relevant changes to the model manager // TODO get versionedAddressBook tests to pass - // private final FilteredList filteredRequests; + private final FilteredList filteredRequests; private final SimpleObjectProperty selectedPerson = new SimpleObjectProperty<>(); - private final UserPrefs userPrefs; + + private final SimpleObjectProperty selectedRequest = new SimpleObjectProperty<>(); /** * Initializes a ModelManager with the given addressBook and userPrefs. */ - public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyHealthWorkerBook healthWorkerBook, + public ModelManager(ReadOnlyAddressBook addressBook, + ReadOnlyHealthWorkerBook healthWorkerBook, ReadOnlyRequestBook requestBook, ReadOnlyUserPrefs userPrefs) { super(); requireAllNonNull(addressBook, userPrefs); @@ -49,16 +54,19 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyHealthWorkerBook he versionedAddressBook = new VersionedAddressBook(addressBook); versionedHealthWorkerBook = new VersionedHealthWorkerBook(healthWorkerBook); + versionedRequestBook = new VersionedRequestBook(requestBook); this.userPrefs = new UserPrefs(userPrefs); filteredPersons = new FilteredList<>(versionedAddressBook.getPersonList()); filteredHealthWorkers = new FilteredList<>(versionedHealthWorkerBook.getHealthWorkerList()); + filteredRequests = new FilteredList<>(versionedRequestBook.getRequestList()); filteredPersons.addListener(this::ensureSelectedPersonIsValid); // TODO: listener for healthworker filteredHealthWorkers.addListener(this::ensureSelectedPersonIsValid); + filteredRequests.addListener(this::ensureSelectedRequestIsValid); } public ModelManager() { - this(new AddressBook(), new HealthWorkerBook(), new UserPrefs()); + this(new AddressBook(), new HealthWorkerBook(), new RequestBook(), new UserPrefs()); } //=========== UserPrefs ================================================================================== @@ -324,6 +332,41 @@ public void setRequest(Request target, Request editedRequest) { } + @Override + public void commitRequestBook() { + versionedRequestBook.commit(); + } + + /** + * Ensures {@code selectedRequest} is a valid request in {@code filteredRequests}. + */ + private void ensureSelectedRequestIsValid(ListChangeListener.Change change) { + while (change.next()) { + if (selectedRequest.getValue() == null) { + return; + } + + boolean wasSelectedRequestReplaced = + change.wasReplaced() && change.getAddedSize() == change.getRemovedSize() + && change.getRemoved().contains(selectedRequest.getValue()); + + if (wasSelectedRequestReplaced) { + // Update selectedRequest to its new value + int index = change.getRemoved().indexOf(selectedRequest.getValue()); + selectedRequest.setValue(change.getAddedSubList().get(index)); + continue; + } + + boolean wasSelectedRequestRemoved = + change.getRemoved().stream().anyMatch(removedRequest -> selectedRequest.getValue() + .isSameRequest(removedRequest)); + if (wasSelectedRequestRemoved) { + selectedRequest.setValue(change.getFrom() > 0 + ? change.getList().get(change.getFrom() - 1) : null); + } + } + } + /** * Ensures {@code selectedPerson} is a valid person in {@code filteredPersons}. */ diff --git a/src/main/java/seedu/address/model/UserPrefs.java b/src/main/java/seedu/address/model/UserPrefs.java index c65895656ced..6ca7c28b61eb 100644 --- a/src/main/java/seedu/address/model/UserPrefs.java +++ b/src/main/java/seedu/address/model/UserPrefs.java @@ -15,9 +15,11 @@ public class UserPrefs implements ReadOnlyUserPrefs { private GuiSettings guiSettings = new GuiSettings(); private Path addressBookFilePath = Paths.get("data" , "addressbook.json"); + private Path requestBookFilePath = Paths.get("data" , "requestbook.json"); private Path healthWorkerBookFilePath = Paths.get("data" , "healthworkerbook.json"); + /** * Creates a {@code UserPrefs} with default values. */ @@ -60,10 +62,6 @@ public void setAddressBookFilePath(Path addressBookFilePath) { this.addressBookFilePath = addressBookFilePath; } - public Path getRequestBookFilePath() { - return requestBookFilePath; - } - public void setRequestBookFilePath(Path requestBookFilePath) { requireNonNull(requestBookFilePath); this.requestBookFilePath = requestBookFilePath; @@ -90,7 +88,8 @@ public boolean equals(Object other) { UserPrefs o = (UserPrefs) other; return guiSettings.equals(o.guiSettings) - && addressBookFilePath.equals(o.addressBookFilePath); + && addressBookFilePath.equals(o.addressBookFilePath) + && requestBookFilePath.equals(o.requestBookFilePath); } @Override @@ -108,4 +107,7 @@ public String toString() { return sb.toString(); } + public Path getRequestBookFilePath() { + return this.requestBookFilePath; + } } diff --git a/src/main/java/seedu/address/model/person/UniquePersonList.java b/src/main/java/seedu/address/model/person/UniquePersonList.java index 0fee4fe57e6b..0aee2fae4c17 100644 --- a/src/main/java/seedu/address/model/person/UniquePersonList.java +++ b/src/main/java/seedu/address/model/person/UniquePersonList.java @@ -121,6 +121,11 @@ public int hashCode() { return internalList.hashCode(); } + public Person getAt(int index) { + assert(index < this.internalList.size() - 1); + return this.internalList.get(index); + } + /** * Returns true if {@code persons} contains only unique persons. */ diff --git a/src/main/java/seedu/address/model/person/healthworker/UniqueHealthWorkerList.java b/src/main/java/seedu/address/model/person/healthworker/UniqueHealthWorkerList.java index 95f5fe932d70..3166bb397df3 100644 --- a/src/main/java/seedu/address/model/person/healthworker/UniqueHealthWorkerList.java +++ b/src/main/java/seedu/address/model/person/healthworker/UniqueHealthWorkerList.java @@ -142,4 +142,10 @@ private boolean workersAreUnique(List workers) { } return true; } + + public HealthWorker getAt(int index) { + assert(index < this.internalList.size() - 1); + return this.internalList.get(index); + } + } diff --git a/src/test/java/seedu/address/TestApp.java b/src/test/java/seedu/address/TestApp.java index 988be9e7d18c..daf00fad8f28 100644 --- a/src/test/java/seedu/address/TestApp.java +++ b/src/test/java/seedu/address/TestApp.java @@ -14,6 +14,7 @@ import seedu.address.model.Model; import seedu.address.model.ModelManager; import seedu.address.model.ReadOnlyAddressBook; +import seedu.address.model.RequestBook; import seedu.address.model.UserPrefs; import seedu.address.storage.JsonAddressBookStorage; import seedu.address.storage.UserPrefsStorage; @@ -94,7 +95,8 @@ public Path getStorageSaveLocation() { */ public Model getModel() { // TODO: Fix HealthWorkerBook implementation - Model copy = new ModelManager((model.getAddressBook()), new HealthWorkerBook(), new UserPrefs()); + Model copy = new ModelManager((model.getAddressBook()), new HealthWorkerBook(), + new RequestBook(), new UserPrefs()); ModelHelper.setFilteredList(copy, model.getFilteredPersonList()); return copy; } diff --git a/src/test/java/seedu/address/logic/LogicManagerTest.java b/src/test/java/seedu/address/logic/LogicManagerTest.java index 7fbafea6238d..612defe26fc1 100644 --- a/src/test/java/seedu/address/logic/LogicManagerTest.java +++ b/src/test/java/seedu/address/logic/LogicManagerTest.java @@ -8,6 +8,7 @@ import static seedu.address.logic.commands.CommandTestUtil.NAME_DESC_AMY; import static seedu.address.logic.commands.CommandTestUtil.PHONE_DESC_AMY; import static seedu.address.testutil.TypicalPersons.AMY; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import java.io.IOException; import java.nio.file.Path; @@ -30,6 +31,7 @@ import seedu.address.model.UserPrefs; import seedu.address.model.person.Person; import seedu.address.storage.JsonAddressBookStorage; +import seedu.address.storage.JsonRequestBookStorage; import seedu.address.storage.JsonUserPrefsStorage; import seedu.address.storage.StorageManager; import seedu.address.testutil.PersonBuilder; @@ -51,7 +53,9 @@ public class LogicManagerTest { public void setUp() throws Exception { JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(temporaryFolder.newFile().toPath()); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath()); - StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage); + JsonRequestBookStorage requestBookStorage = + new JsonRequestBookStorage(temporaryFolder.newFile().toPath()); + StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage, requestBookStorage); logic = new LogicManager(model, storage); } @@ -82,7 +86,9 @@ public void execute_storageThrowsIoException_throwsCommandException() throws Exc JsonAddressBookStorage addressBookStorage = new JsonAddressBookIoExceptionThrowingStub(temporaryFolder.newFile().toPath()); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath()); - StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage); + JsonRequestBookStorage requestBookStorage = + new JsonRequestBookStorage(temporaryFolder.newFile().toPath()); + StorageManager storage = new StorageManager(addressBookStorage, userPrefsStorage, requestBookStorage); logic = new LogicManager(model, storage); // Execute add command @@ -133,7 +139,8 @@ private void assertCommandException(String inputCommand, String expectedMessage) * @see #assertCommandBehavior(Class, String, String, Model) */ private void assertCommandFailure(String inputCommand, Class expectedException, String expectedMessage) { - Model expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), new UserPrefs()); + Model expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); assertCommandBehavior(expectedException, inputCommand, expectedMessage, expectedModel); } diff --git a/src/test/java/seedu/address/logic/commands/AddPersonCommandIntegrationTest.java b/src/test/java/seedu/address/logic/commands/AddPersonCommandIntegrationTest.java index ccb92465f55d..8a392382bcd4 100644 --- a/src/test/java/seedu/address/logic/commands/AddPersonCommandIntegrationTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPersonCommandIntegrationTest.java @@ -4,6 +4,7 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.TypicalHealthWorkers.getTypicalHealthWorkerBook; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Before; import org.junit.Test; @@ -25,14 +26,16 @@ public class AddPersonCommandIntegrationTest { @Before public void setUp() { - model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); + model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); } @Test public void execute_newPerson_success() { Person validPerson = new PersonBuilder().build(); - Model expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), new UserPrefs()); + Model expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); expectedModel.addPerson(validPerson); expectedModel.commitAddressBook(); diff --git a/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java b/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java index 13c8f684761a..5f80b70882d3 100644 --- a/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/AddPersonCommandTest.java @@ -252,8 +252,7 @@ public void setSelectedPerson(Person person) { */ @Override public Path getRequestBookFilePath() { - // TODO - return null; + throw new AssertionError("This method should not be called."); } /** @@ -263,7 +262,7 @@ public Path getRequestBookFilePath() { */ @Override public void setRequestBookFilePath(Path requestBookFilePath) { - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -273,7 +272,7 @@ public void setRequestBookFilePath(Path requestBookFilePath) { */ @Override public void setRequestBook(ReadOnlyRequestBook requestBook) { - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -281,8 +280,7 @@ public void setRequestBook(ReadOnlyRequestBook requestBook) { */ @Override public ReadOnlyRequestBook getRequestBook() { - return null; - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -293,8 +291,7 @@ public ReadOnlyRequestBook getRequestBook() { */ @Override public boolean hasRequest(Request request) { - return false; - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -305,7 +302,7 @@ public boolean hasRequest(Request request) { */ @Override public void deleteRequest(Request target) { - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -316,7 +313,7 @@ public void deleteRequest(Request target) { */ @Override public void addRequest(Request request) { - // TODO + throw new AssertionError("This method should not be called."); } /** @@ -330,7 +327,12 @@ public void addRequest(Request request) { */ @Override public void setRequest(Request target, Request editedRequest) { - // TODO + throw new AssertionError("This method should not be called."); + } + + @Override + public void commitRequestBook() { + throw new AssertionError("This method should not be called."); } } diff --git a/src/test/java/seedu/address/logic/commands/ClearCommandTest.java b/src/test/java/seedu/address/logic/commands/ClearCommandTest.java index 862375579fa7..69f9aa4f9ba0 100644 --- a/src/test/java/seedu/address/logic/commands/ClearCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ClearCommandTest.java @@ -3,6 +3,7 @@ import static seedu.address.logic.commands.CommandTestUtil.assertCommandSuccess; import static seedu.address.testutil.TypicalHealthWorkers.getTypicalHealthWorkerBook; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Test; @@ -27,8 +28,10 @@ public void execute_emptyAddressBook_success() { @Test public void execute_nonEmptyAddressBook_success() { - Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); - Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); + Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); + Model expectedModel = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); expectedModel.setAddressBook(new AddressBook()); expectedModel.commitAddressBook(); diff --git a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java index 27d2f578d087..05582830e9c5 100644 --- a/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/DeleteCommandTest.java @@ -10,6 +10,7 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Test; @@ -27,7 +28,8 @@ */ public class DeleteCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); @Test @@ -37,8 +39,8 @@ public void execute_validIndexUnfilteredList_success() { String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, personToDelete); - ModelManager expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), - new UserPrefs()); + ModelManager expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); expectedModel.deletePerson(personToDelete); expectedModel.commitAddressBook(); @@ -62,7 +64,8 @@ public void execute_validIndexFilteredList_success() { String expectedMessage = String.format(DeleteCommand.MESSAGE_DELETE_PERSON_SUCCESS, personToDelete); - Model expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), new UserPrefs()); + Model expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); expectedModel.deletePerson(personToDelete); expectedModel.commitAddressBook(); showNoPerson(expectedModel); @@ -87,7 +90,8 @@ public void execute_invalidIndexFilteredList_throwsCommandException() { public void executeUndoRedo_validIndexUnfilteredList_success() throws Exception { Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON); - Model expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), new UserPrefs()); + Model expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); expectedModel.deletePerson(personToDelete); expectedModel.commitAddressBook(); @@ -126,7 +130,8 @@ public void executeUndoRedo_invalidIndexUnfilteredList_failure() { @Test public void executeUndoRedo_validIndexFilteredList_samePersonDeleted() throws Exception { DeleteCommand deleteCommand = new DeleteCommand(INDEX_FIRST_PERSON); - Model expectedModel = new ModelManager(model.getAddressBook(), model.getHealthWorkerBook(), new UserPrefs()); + Model expectedModel = new ModelManager(model.getAddressBook(), + model.getHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); showPersonAtIndex(model, INDEX_SECOND_PERSON); Person personToDelete = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); diff --git a/src/test/java/seedu/address/logic/commands/EditPersonCommandTest.java b/src/test/java/seedu/address/logic/commands/EditPersonCommandTest.java index 09f76cdaa173..26c50d4e62f2 100644 --- a/src/test/java/seedu/address/logic/commands/EditPersonCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/EditPersonCommandTest.java @@ -15,6 +15,7 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Test; @@ -37,7 +38,8 @@ */ public class EditPersonCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); @Test @@ -49,7 +51,7 @@ public void execute_allFieldsSpecifiedUnfilteredList_success() { String expectedMessage = String.format(EditPersonCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); expectedModel.commitAddressBook(); @@ -72,7 +74,7 @@ public void execute_someFieldsSpecifiedUnfilteredList_success() { String expectedMessage = String.format(EditPersonCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); expectedModel.setPerson(lastPerson, editedPerson); expectedModel.commitAddressBook(); @@ -87,7 +89,7 @@ public void execute_noFieldSpecifiedUnfilteredList_success() { String expectedMessage = String.format(EditPersonCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); expectedModel.commitAddressBook(); assertCommandSuccess(editPersonCommand, model, commandHistory, expectedMessage, expectedModel); @@ -105,7 +107,7 @@ public void execute_filteredList_success() { String expectedMessage = String.format(EditPersonCommand.MESSAGE_EDIT_PERSON_SUCCESS, editedPerson); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); expectedModel.setPerson(model.getFilteredPersonList().get(0), editedPerson); expectedModel.commitAddressBook(); @@ -168,7 +170,7 @@ public void executeUndoRedo_validIndexUnfilteredList_success() throws Exception EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(editedPerson).build(); EditPersonCommand editPersonCommand = new EditPersonCommand(INDEX_FIRST_PERSON, descriptor); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); expectedModel.setPerson(personToEdit, editedPerson); expectedModel.commitAddressBook(); @@ -212,7 +214,7 @@ public void executeUndoRedo_validIndexFilteredList_samePersonEdited() throws Exc EditPersonDescriptor descriptor = new EditPersonDescriptorBuilder(editedPerson).build(); EditPersonCommand editPersonCommand = new EditPersonCommand(INDEX_FIRST_PERSON, descriptor); Model expectedModel = new ModelManager(new AddressBook(model.getAddressBook()), - new HealthWorkerBook(model.getHealthWorkerBook()), new UserPrefs()); + new HealthWorkerBook(model.getHealthWorkerBook()), getTypicalRequestBook(), new UserPrefs()); showPersonAtIndex(model, INDEX_SECOND_PERSON); Person personToEdit = model.getFilteredPersonList().get(INDEX_FIRST_PERSON.getZeroBased()); diff --git a/src/test/java/seedu/address/logic/commands/FindCommandTest.java b/src/test/java/seedu/address/logic/commands/FindCommandTest.java index d5a28a6b7537..8ff8e1756f28 100644 --- a/src/test/java/seedu/address/logic/commands/FindCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/FindCommandTest.java @@ -10,6 +10,7 @@ import static seedu.address.testutil.TypicalPersons.ELLE; import static seedu.address.testutil.TypicalPersons.FIONA; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import java.util.Arrays; import java.util.Collections; @@ -26,9 +27,10 @@ * Contains integration tests (interaction with the Model) for {@code FindCommand}. */ public class FindCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); - private Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); @Test diff --git a/src/test/java/seedu/address/logic/commands/ListCommandTest.java b/src/test/java/seedu/address/logic/commands/ListCommandTest.java index fc64ed42957a..7925f8b8bae7 100644 --- a/src/test/java/seedu/address/logic/commands/ListCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/ListCommandTest.java @@ -5,6 +5,7 @@ import static seedu.address.testutil.TypicalHealthWorkers.getTypicalHealthWorkerBook; import static seedu.address.testutil.TypicalIndexes.INDEX_FIRST_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Before; import org.junit.Test; @@ -25,8 +26,10 @@ public class ListCommandTest { @Before public void setUp() { - model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); - expectedModel = new ModelManager(model.getAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); + model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); + expectedModel = new ModelManager(model.getAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); } @Test diff --git a/src/test/java/seedu/address/logic/commands/RedoCommandTest.java b/src/test/java/seedu/address/logic/commands/RedoCommandTest.java index 94ed37f8d376..794d2e9139b6 100644 --- a/src/test/java/seedu/address/logic/commands/RedoCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/RedoCommandTest.java @@ -5,6 +5,7 @@ import static seedu.address.logic.commands.CommandTestUtil.deleteFirstPerson; import static seedu.address.testutil.TypicalHealthWorkers.getTypicalHealthWorkerBook; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Before; import org.junit.Test; @@ -16,10 +17,10 @@ public class RedoCommandTest { - private final Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); - private final Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); + private final Model model = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); + private final Model expectedModel = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); private final CommandHistory commandHistory = new CommandHistory(); @Before diff --git a/src/test/java/seedu/address/logic/commands/SelectCommandTest.java b/src/test/java/seedu/address/logic/commands/SelectCommandTest.java index ae31bc995734..8e573cae0c6b 100644 --- a/src/test/java/seedu/address/logic/commands/SelectCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/SelectCommandTest.java @@ -10,6 +10,7 @@ import static seedu.address.testutil.TypicalIndexes.INDEX_SECOND_PERSON; import static seedu.address.testutil.TypicalIndexes.INDEX_THIRD_PERSON; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Test; @@ -24,9 +25,10 @@ * Contains integration tests (interaction with the Model) for {@code SelectCommand}. */ public class SelectCommandTest { - private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), new UserPrefs()); - private Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); + private Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), + getTypicalRequestBook(), new UserPrefs()); + private Model expectedModel = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); private CommandHistory commandHistory = new CommandHistory(); @Test diff --git a/src/test/java/seedu/address/logic/commands/UndoCommandTest.java b/src/test/java/seedu/address/logic/commands/UndoCommandTest.java index d1b3b7b968bf..70fff3c0db53 100644 --- a/src/test/java/seedu/address/logic/commands/UndoCommandTest.java +++ b/src/test/java/seedu/address/logic/commands/UndoCommandTest.java @@ -5,6 +5,7 @@ import static seedu.address.logic.commands.CommandTestUtil.deleteFirstPerson; import static seedu.address.testutil.TypicalHealthWorkers.getTypicalHealthWorkerBook; import static seedu.address.testutil.TypicalPersons.getTypicalAddressBook; +import static seedu.address.testutil.TypicalRequests.getTypicalRequestBook; import org.junit.Before; import org.junit.Test; @@ -16,10 +17,10 @@ public class UndoCommandTest { - private final Model model = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); - private final Model expectedModel = new ModelManager(getTypicalAddressBook(), getTypicalHealthWorkerBook(), - new UserPrefs()); + private final Model model = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); + private final Model expectedModel = new ModelManager(getTypicalAddressBook(), + getTypicalHealthWorkerBook(), getTypicalRequestBook(), new UserPrefs()); private final CommandHistory commandHistory = new CommandHistory(); @Before diff --git a/src/test/java/seedu/address/logic/commands/request/CreateRequestCommandTest.java b/src/test/java/seedu/address/logic/commands/request/CreateRequestCommandTest.java new file mode 100644 index 000000000000..86ffa7271cc5 --- /dev/null +++ b/src/test/java/seedu/address/logic/commands/request/CreateRequestCommandTest.java @@ -0,0 +1,332 @@ +package seedu.address.logic.commands.request; + +import static java.util.Objects.requireNonNull; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertFalse; +import static org.junit.jupiter.api.Assertions.assertTrue; + +import java.nio.file.Path; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.function.Predicate; + +import org.junit.Rule; +import org.junit.jupiter.api.Test; +import org.junit.rules.ExpectedException; + +import javafx.beans.property.ReadOnlyProperty; +import javafx.collections.ObservableList; +import seedu.address.commons.core.GuiSettings; +import seedu.address.logic.CommandHistory; +import seedu.address.logic.commands.CommandResult; +import seedu.address.logic.commands.exceptions.CommandException; +import seedu.address.model.Model; +import seedu.address.model.ReadOnlyAddressBook; +import seedu.address.model.ReadOnlyHealthWorkerBook; +import seedu.address.model.ReadOnlyRequestBook; +import seedu.address.model.ReadOnlyUserPrefs; +import seedu.address.model.RequestBook; +import seedu.address.model.person.Person; +import seedu.address.model.person.healthworker.HealthWorker; +import seedu.address.model.request.Request; +import seedu.address.testutil.Assert; +import seedu.address.testutil.RequestBuilder; +import seedu.address.testutil.TypicalRequests; + +class CreateRequestCommandTest { + + protected static final CommandHistory EMPTY_COMMAND_HISTORY = new CommandHistory(); + + @Rule + public ExpectedException thrown = ExpectedException.none(); + + protected CommandHistory commandHistory = new CommandHistory(); + + @Test + public void constructor_nullRequest_throwsNullPointerException() { + Assert.assertThrows(NullPointerException.class, () -> new CreateRequestCommand(null)); + } + + @Test + public void execute_requestAcceptedByModel_addSuccessful() throws Exception { + ModelStubAcceptingRequestAdded modelStub = new ModelStubAcceptingRequestAdded(); + Request validRequest = new RequestBuilder().build(); + + CommandResult commandResult = new CreateRequestCommand(validRequest).execute(modelStub, + commandHistory); + + assertEquals(String.format(CreateRequestCommand.MESSAGE_SUCCESS, validRequest), + commandResult.getFeedbackToUser()); + assertEquals(Arrays.asList(validRequest), modelStub.requestsAdded); + assertEquals(EMPTY_COMMAND_HISTORY, commandHistory); + } + + @Test + public void execute_duplicateRequest_throwsCommandException() { + Request validRequest = new RequestBuilder().build(); + CreateRequestCommand createRequestCommand = new CreateRequestCommand(validRequest); + ModelStub modelStub = new ModelStubWithRequest(validRequest); + + Assert.assertThrows(CommandException.class, + CreateRequestCommand.MESSAGE_DUPLICATE_REQUEST, () -> createRequestCommand.execute + (modelStub, commandHistory)); + } + + @Test + public void equals() { + + CreateRequestCommand addAliceRequest = new CreateRequestCommand(TypicalRequests.ALICE_REQUEST); + CreateRequestCommand addBensonRequest = new CreateRequestCommand(TypicalRequests.BENSON_REQUEST); + + // same object -> returns true + assertTrue(addAliceRequest.equals(addAliceRequest)); + + // same values -> returns true + CreateRequestCommand addAliceRequestCopy = + new CreateRequestCommand(TypicalRequests.ALICE_REQUEST); + assertTrue(addAliceRequest.equals(addAliceRequestCopy)); + + // different types -> returns false + assertFalse(addAliceRequest.equals(1)); + + // null -> returns false + assertFalse(addAliceRequest.equals(null)); + + // differnt request -> returns false + assertFalse(addAliceRequest.equals(addBensonRequest)); + } + + protected class ModelStub implements Model { + + @Override + public void setUserPrefs(ReadOnlyUserPrefs userPrefs) { + throw new AssertionError("This method should not be called."); + } + + @Override + public ReadOnlyUserPrefs getUserPrefs() { + throw new AssertionError("This method should not be called."); + } + + @Override + public GuiSettings getGuiSettings() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setGuiSettings(GuiSettings guiSettings) { + throw new AssertionError("This method should not be called."); + } + + @Override + public Path getAddressBookFilePath() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setAddressBookFilePath(Path addressBookFilePath) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setAddressBook(ReadOnlyAddressBook addressBook) { + throw new AssertionError("This method should not be called."); + } + + @Override + public ReadOnlyAddressBook getAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public ReadOnlyHealthWorkerBook getHealthWorkerBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public boolean hasPerson(Person person) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void deletePerson(Person target) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void addPerson(Person person) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setPerson(Person target, Person editedPerson) { + throw new AssertionError("This method should not be called."); + } + + @Override + public boolean hasHealthWorker(HealthWorker healthWorker) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void deleteHealthWorker(HealthWorker target) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void addHealthWorker(HealthWorker healthWorker) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setHealthWorker(HealthWorker target, HealthWorker editedWorker) { + throw new AssertionError("This method should not be called."); + } + + @Override + public ObservableList getFilteredHealthWorkerList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updateFilteredHealthWorkerList(Predicate predicate) { + throw new AssertionError("This method should not be called."); + } + + @Override + public ObservableList getFilteredPersonList() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void updateFilteredPersonList(Predicate predicate) { + throw new AssertionError("This method should not be called."); + } + + @Override + public boolean canUndoAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public boolean canRedoAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void undoAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void redoAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void commitAddressBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public ReadOnlyProperty selectedPersonProperty() { + throw new AssertionError("This method should not be called."); + } + + @Override + public Person getSelectedPerson() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setSelectedPerson(Person person) { + throw new AssertionError("This method should not be called."); + } + + @Override + public Path getRequestBookFilePath() { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setRequestBookFilePath(Path requestBookFilePath) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setRequestBook(ReadOnlyRequestBook requestBook) { + throw new AssertionError("This method should not be called."); + } + + @Override + public ReadOnlyRequestBook getRequestBook() { + throw new AssertionError("This method should not be called."); + } + + @Override + public boolean hasRequest(Request request) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void deleteRequest(Request target) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void addRequest(Request request) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void setRequest(Request target, Request editedRequest) { + throw new AssertionError("This method should not be called."); + } + + @Override + public void commitRequestBook() { + throw new AssertionError("This method should not be called."); + } + } + + private class ModelStubWithRequest extends ModelStub { + private final Request request; + + ModelStubWithRequest(Request request) { + requireNonNull(request); + this.request = request; + } + + @Override + public boolean hasRequest(Request request) { + requireNonNull(request); + return this.request.isSameRequest(request); + } + } + + private class ModelStubAcceptingRequestAdded extends ModelStub { + final ArrayList requestsAdded = new ArrayList<>(); + + @Override + public boolean hasRequest(Request request) { + requireNonNull(request); + return requestsAdded.stream().anyMatch(request::isSameRequest); + } + + @Override + public void addRequest(Request request) { + requireNonNull(request); + requestsAdded.add(request); + } + + @Override + public void commitRequestBook() { + // called by {@code CreateRequestCommand#execute()} + } + + @Override + public ReadOnlyRequestBook getRequestBook() { + return new RequestBook(); + } + } +} diff --git a/src/test/java/seedu/address/model/ModelManagerTest.java b/src/test/java/seedu/address/model/ModelManagerTest.java index bcf4b9dfcbbe..141a4f793a0f 100644 --- a/src/test/java/seedu/address/model/ModelManagerTest.java +++ b/src/test/java/seedu/address/model/ModelManagerTest.java @@ -9,6 +9,7 @@ import static seedu.address.testutil.TypicalHealthWorkers.BETTY; import static seedu.address.testutil.TypicalPersons.ALICE; import static seedu.address.testutil.TypicalPersons.BENSON; +import static seedu.address.testutil.TypicalRequests.ALICE_REQUEST; import java.nio.file.Path; import java.nio.file.Paths; @@ -28,6 +29,7 @@ import seedu.address.testutil.Assert; import seedu.address.testutil.HealthWorkerBookBuilder; import seedu.address.testutil.PersonBuilder; +import seedu.address.testutil.RequestBookBuilder; public class ModelManagerTest { @Rule @@ -221,11 +223,13 @@ public void equals() { HealthWorkerBook healthWorkerBook = new HealthWorkerBookBuilder().withHealthWorker(ANDY) .withHealthWorker(BETTY).build(); AddressBook differentAddressBook = new AddressBook(); + RequestBook requestBook = new RequestBookBuilder().withRequest(ALICE_REQUEST).build(); UserPrefs userPrefs = new UserPrefs(); // same values -> returns true - modelManager = new ModelManager(addressBook, healthWorkerBook, userPrefs); - ModelManager modelManagerCopy = new ModelManager(addressBook, healthWorkerBook, userPrefs); + modelManager = new ModelManager(addressBook, healthWorkerBook, requestBook, userPrefs); + ModelManager modelManagerCopy = new ModelManager(addressBook, healthWorkerBook, + requestBook, userPrefs); assertTrue(modelManager.equals(modelManagerCopy)); // same object -> returns true @@ -238,12 +242,14 @@ public void equals() { assertFalse(modelManager.equals(5)); // different addressBook -> returns false - assertFalse(modelManager.equals(new ModelManager(differentAddressBook, healthWorkerBook, userPrefs))); + assertFalse(modelManager.equals(new ModelManager(differentAddressBook, healthWorkerBook, + requestBook, userPrefs))); // different filteredList -> returns false String[] keywords = ALICE.getName().fullName.split("\\s+"); modelManager.updateFilteredPersonList(new NameContainsKeywordsPredicate(Arrays.asList(keywords))); - assertFalse(modelManager.equals(new ModelManager(addressBook, healthWorkerBook, userPrefs))); + assertFalse(modelManager.equals(new ModelManager(addressBook, healthWorkerBook, + requestBook, userPrefs))); // resets modelManager to initial state for upcoming tests modelManager.updateFilteredPersonList(PREDICATE_SHOW_ALL_PERSONS); @@ -251,6 +257,7 @@ public void equals() { // different userPrefs -> returns false UserPrefs differentUserPrefs = new UserPrefs(); differentUserPrefs.setAddressBookFilePath(Paths.get("differentFilePath")); - assertFalse(modelManager.equals(new ModelManager(addressBook, healthWorkerBook, differentUserPrefs))); + assertFalse(modelManager.equals(new ModelManager(addressBook, healthWorkerBook, + requestBook, differentUserPrefs))); } } diff --git a/src/test/java/seedu/address/storage/StorageManagerTest.java b/src/test/java/seedu/address/storage/StorageManagerTest.java index b56a68ded0b3..dc0266ffa8da 100644 --- a/src/test/java/seedu/address/storage/StorageManagerTest.java +++ b/src/test/java/seedu/address/storage/StorageManagerTest.java @@ -27,7 +27,10 @@ public class StorageManagerTest { public void setUp() { JsonAddressBookStorage addressBookStorage = new JsonAddressBookStorage(getTempFilePath("ab")); JsonUserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(getTempFilePath("prefs")); - storageManager = new StorageManager(addressBookStorage, userPrefsStorage); + JsonRequestBookStorage requestBookStorage = new JsonRequestBookStorage(getTempFilePath( + "rb")); + storageManager = new StorageManager(addressBookStorage, userPrefsStorage, + requestBookStorage); } private Path getTempFilePath(String fileName) { diff --git a/src/test/java/seedu/address/ui/MainWindowCloseTest.java b/src/test/java/seedu/address/ui/MainWindowCloseTest.java index e63967f1b926..4a5abd3ba6bc 100644 --- a/src/test/java/seedu/address/ui/MainWindowCloseTest.java +++ b/src/test/java/seedu/address/ui/MainWindowCloseTest.java @@ -18,6 +18,7 @@ import seedu.address.logic.LogicManager; import seedu.address.model.ModelManager; import seedu.address.storage.JsonAddressBookStorage; +import seedu.address.storage.JsonRequestBookStorage; import seedu.address.storage.JsonUserPrefsStorage; import seedu.address.storage.StorageManager; @@ -36,7 +37,10 @@ public class MainWindowCloseTest extends GuiUnitTest { public void setUp() throws Exception { JsonAddressBookStorage jsonAddressBookStorage = new JsonAddressBookStorage(temporaryFolder.newFile().toPath()); JsonUserPrefsStorage jsonUserPrefsStorage = new JsonUserPrefsStorage(temporaryFolder.newFile().toPath()); - StorageManager storageManager = new StorageManager(jsonAddressBookStorage, jsonUserPrefsStorage); + JsonRequestBookStorage jsonRequestBookStorage = + new JsonRequestBookStorage(temporaryFolder.newFile().toPath()); + StorageManager storageManager = new StorageManager(jsonAddressBookStorage, + jsonUserPrefsStorage, jsonRequestBookStorage); FxToolkit.setupStage(stage -> { this.stage = stage; mainWindow = new MainWindow(stage, new LogicManager(new ModelManager(), storageManager));