Skip to content

Commit

Permalink
Merge pull request nus-cs2103-AY2223S2#70 from hingen/update-model
Browse files Browse the repository at this point in the history
Partial model update
  • Loading branch information
shaowi authored Mar 10, 2023
2 parents 1843463 + b09189b commit 668409b
Show file tree
Hide file tree
Showing 30 changed files with 1,307 additions and 24 deletions.
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public void init() throws Exception {

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getTrackerFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);

initLogging(config);
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/seedu/address/model/Model.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,20 @@

import javafx.collections.ObservableList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;

/**
* The API of the Model component.
*/
public interface Model {
// TODO: Remove this
/** {@code Predicate} that always evaluate to true */
Predicate<Person> PREDICATE_SHOW_ALL_PERSONS = unused -> true;

/** {@code Predicate} that always evaluate to true */
Predicate<Module> PREDICATE_SHOW_ALL_MODULES = unused -> true;

/**
* Replaces user prefs data with the data in {@code userPrefs}.
*/
Expand All @@ -34,51 +39,103 @@ public interface Model {
*/
void setGuiSettings(GuiSettings guiSettings);

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

// TODO: Replace this
/**
* Sets the user prefs' address book file path.
*/
void setAddressBookFilePath(Path addressBookFilePath);

/**
* Replaces tracker data with the data in {@code tracker}.
*/
void setTracker(ReadOnlyTracker tracker);

/**
* Returns the Tracker.
*/
ReadOnlyTracker getTracker();

/**
* Returns true if a module with the same code as {@code module} exists in the tracker.
*/
boolean hasModule(Module module);

/**
* Deletes the given module.
* The module must exist in the tracker.
*/
void deleteModule(Module target);

/**
* Adds the given module.
*/
void addModule(Module module);

/**
* Replaces the given module {@code target} with {@code editedModule}.
* {@code target} must exist in the tracker.
* The module code of {@code editedPerson} must not be the same as another existing module in the tracker.
*/
void setModule(Module target, Module editedModule);

/** Returns an unmodifiable view of the filtered module list */
ObservableList<Module> getFilteredModuleList();

/**
* Updates the filter of the filtered module list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
*/
void updateFilteredModuleList(Predicate<Module> predicate);

// TODO: Remove this
/**
* Replaces address book data with the data in {@code addressBook}.
*/
void setAddressBook(ReadOnlyAddressBook addressBook);

// TODO: Remove this
/** Returns the AddressBook */
ReadOnlyAddressBook getAddressBook();

// TODO: Remove this
/**
* Returns true if a person with the same identity as {@code person} exists in the address book.
*/
boolean hasPerson(Person person);

// TODO: Remove this
/**
* Deletes the given person.
* The person must exist in the address book.
*/
void deletePerson(Person target);

// TODO: Remove this
/**
* Adds the given person.
* {@code person} must not already exist in the address book.
*/
void addPerson(Person person);

// TODO: Remove this
/**
* Replaces the given person {@code target} with {@code editedPerson}.
* {@code target} must exist in the address book.
* The person identity of {@code editedPerson} must not be the same as another existing person in the address book.
*/
void setPerson(Person target, Person editedPerson);

// TODO: Remove this
/** Returns an unmodifiable view of the filtered person list */
ObservableList<Person> getFilteredPersonList();

// TODO: Remove this
/**
* Updates the filter of the filtered person list to filter by the given {@code predicate}.
* @throws NullPointerException if {@code predicate} is null.
Expand Down
65 changes: 60 additions & 5 deletions src/main/java/seedu/address/model/ModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
import javafx.collections.transformation.FilteredList;
import seedu.address.commons.core.GuiSettings;
import seedu.address.commons.core.LogsCenter;
import seedu.address.model.module.Module;
import seedu.address.model.person.Person;

/**
Expand All @@ -19,10 +20,12 @@
public class ModelManager implements Model {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);

private final AddressBook addressBook;
private final Tracker tracker;
private final UserPrefs userPrefs;
private final FilteredList<Person> filteredPersons;
private final FilteredList<Module> filteredModules;

private final AddressBook addressBook; // TODO: Remove this
private final FilteredList<Person> filteredPersons; // TODO: Remove this
/**
* Initializes a ModelManager with the given addressBook and userPrefs.
*/
Expand All @@ -31,8 +34,10 @@ public ModelManager(ReadOnlyAddressBook addressBook, ReadOnlyUserPrefs userPrefs

logger.fine("Initializing with address book: " + addressBook + " and user prefs " + userPrefs);

this.tracker = new Tracker(); //TODO: assign this from constructor arguments
this.addressBook = new AddressBook(addressBook);
this.userPrefs = new UserPrefs(userPrefs);
filteredModules = new FilteredList<>(this.tracker.getModuleList());
filteredPersons = new FilteredList<>(this.addressBook.getPersonList());
}

Expand Down Expand Up @@ -66,13 +71,61 @@ public void setGuiSettings(GuiSettings guiSettings) {

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

@Override
public void setAddressBookFilePath(Path addressBookFilePath) {
requireNonNull(addressBookFilePath);
userPrefs.setAddressBookFilePath(addressBookFilePath);
userPrefs.setTrackerFilePath(addressBookFilePath);
}

//=========== Tracker ====================================================================================

@Override
public void setTracker(ReadOnlyTracker tracker) {
this.tracker.resetData(tracker);
}

@Override
public ReadOnlyTracker getTracker() {
return tracker;
}

@Override
public boolean hasModule(Module module) {
return tracker.hasModule(module);
}

@Override
public void deleteModule(Module target) {
tracker.removeModule(target);
}

@Override
public void addModule(Module module) {
tracker.addModule(module);
updateFilteredModuleList(PREDICATE_SHOW_ALL_MODULES);
}

@Override
public void setModule(Module target, Module editedModule) {
requireAllNonNull(target, editedModule);

tracker.setModule(target, editedModule);
}

//=========== Filtered Module List Accessors =============================================================

@Override
public ObservableList<Module> getFilteredModuleList() {
return filteredModules;
}

@Override
public void updateFilteredModuleList(Predicate<Module> predicate) {
requireNonNull(predicate);
filteredModules.setPredicate(predicate);
}

//=========== AddressBook ================================================================================
Expand Down Expand Up @@ -144,7 +197,9 @@ public boolean equals(Object obj) {
ModelManager other = (ModelManager) obj;
return addressBook.equals(other.addressBook)
&& userPrefs.equals(other.userPrefs)
&& filteredPersons.equals(other.filteredPersons);
&& filteredPersons.equals(other.filteredPersons)
&& tracker.equals(other.tracker)
&& filteredModules.equals(other.filteredModules);
}

}
15 changes: 15 additions & 0 deletions src/main/java/seedu/address/model/ReadOnlyTracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package seedu.address.model;

import javafx.collections.ObservableList;

/**
* Unmodifiable view of a tracker
*/
public interface ReadOnlyTracker {

/**
* Returns an unmodifiable view of the module list.
* This list will not contain any duplicate modules.
*/
ObservableList<seedu.address.model.module.Module> getModuleList();
}
2 changes: 1 addition & 1 deletion src/main/java/seedu/address/model/ReadOnlyUserPrefs.java
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ public interface ReadOnlyUserPrefs {

GuiSettings getGuiSettings();

Path getAddressBookFilePath();
Path getTrackerFilePath();

}
119 changes: 119 additions & 0 deletions src/main/java/seedu/address/model/Tracker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
package seedu.address.model;

import static java.util.Objects.requireNonNull;

import java.util.List;

import javafx.collections.ObservableList;
import seedu.address.model.module.Module;
import seedu.address.model.module.UniqueModuleList;

/**
* Wraps all data at the tracker level
* Duplicate modules are not allowed (by .isSameModule comparison)
*/
public class Tracker implements ReadOnlyTracker {
private final UniqueModuleList modules;

/*
* The 'unusual' code block below is a non-static initialization block, sometimes used to avoid duplication
* between constructors. See https://docs.oracle.com/javase/tutorial/java/javaOO/initial.html
*
* Note that non-static init blocks are not recommended to use. There are other ways to avoid duplication
* among constructors.
*/
{
modules = new UniqueModuleList();
}

public Tracker() {}

/**
* Creates a Tracker using the Modules in the {@code toBeCopied}
*/
public Tracker(ReadOnlyTracker toBeCopied) {
this();
resetData(toBeCopied);
}

//// list overwrite operations

/**
* Replaces the contents of the module list with {@code modules}.
* {@code modules} must not contain duplicate modules.
*/
public void setModules(List<Module> modules) {
this.modules.setModules(modules);
}

/**
* Resets the existing data of this {@code Tracker} with {@code newData}.
*/
public void resetData(ReadOnlyTracker newData) {
requireNonNull(newData);

setModules(newData.getModuleList());
}

//// module-level operations

/**
* Returns true if a module with the same code as {@code module} exists in the tracker.
*/
public boolean hasModule(Module module) {
requireNonNull(module);
return modules.contains(module);
}

/**
* Adds a module to the tracker.
* The module must not already exist in the tracker.
*/
public void addModule(Module module) {
modules.add(module);
}

/**
* Replaces the given module {@code target} in the list with {@code editedModule}.
* {@code target} must exist in the tracker.
* The module of {@code editedModule} must not be the same as another existing module in the tracker.
*/
public void setModule(Module target, Module editedModule) {
requireNonNull(editedModule);

modules.setModule(target, editedModule);
}

/**
* Removes {@code key} from this {@code Tracker}.
* {@code key} must exist in the tracker.
*/
public void removeModule(Module key) {
modules.remove(key);
}

//// util methods

@Override
public String toString() {
return modules.asUnmodifiableObservableList().size() + " modules";
// TODO: refine later
}

@Override
public ObservableList<Module> getModuleList() {
return modules.asUnmodifiableObservableList();
}

@Override
public boolean equals(Object other) {
return other == this // short circuit if same object
|| (other instanceof Tracker // instanceof handles nulls
&& modules.equals(((Tracker) other).modules));
}

@Override
public int hashCode() {
return modules.hashCode();
}
}
Loading

0 comments on commit 668409b

Please sign in to comment.