Skip to content

Commit

Permalink
Merge pull request #54 from lyuanww/refactor-main-app-to-patient
Browse files Browse the repository at this point in the history
refactor: change main app to new patient system
  • Loading branch information
tanboonkhong authored Oct 17, 2023
2 parents ee89dd6 + c57d354 commit 40ebf14
Show file tree
Hide file tree
Showing 12 changed files with 565 additions and 68 deletions.
88 changes: 44 additions & 44 deletions src/main/java/seedu/cc/MainApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,23 +13,23 @@
import seedu.cc.commons.exceptions.DataLoadingException;
import seedu.cc.commons.util.ConfigUtil;
import seedu.cc.commons.util.StringUtil;
import seedu.cc.logic.Logic;
import seedu.cc.logic.LogicManager;
import seedu.cc.model.AddressBook;
import seedu.cc.model.Model;
import seedu.cc.model.ModelManager;
import seedu.cc.model.ReadOnlyAddressBook;
import seedu.cc.model.ReadOnlyUserPrefs;
import seedu.cc.model.UserPrefs;
import seedu.cc.model.util.SampleDataUtil;
import seedu.cc.storage.AddressBookStorage;
import seedu.cc.storage.JsonAddressBookStorage;
import seedu.cc.storage.JsonUserPrefsStorage;
import seedu.cc.storage.Storage;
import seedu.cc.storage.StorageManager;
import seedu.cc.storage.UserPrefsStorage;
import seedu.cc.logic.ClinicLogic;
import seedu.cc.logic.ClinicLogicManager;
import seedu.cc.model.ClinicBook;
import seedu.cc.model.NewModel;
import seedu.cc.model.NewModelManager;
import seedu.cc.model.NewReadOnlyUserPrefs;
import seedu.cc.model.NewUserPrefs;
import seedu.cc.model.ReadOnlyClinicBook;
import seedu.cc.model.util.NewSampleDataUtil;
import seedu.cc.storage.ClinicBookStorage;
import seedu.cc.storage.ClinicStorage;
import seedu.cc.storage.ClinicStorageManager;
import seedu.cc.storage.JsonClinicBookStorage;
import seedu.cc.storage.JsonNewUserPrefsStorage;
import seedu.cc.storage.NewUserPrefsStorage;
import seedu.cc.ui.NewUiManager;
import seedu.cc.ui.Ui;
import seedu.cc.ui.UiManager;

/**
* Runs the application.
Expand All @@ -41,9 +41,9 @@ public class MainApp extends Application {
private static final Logger logger = LogsCenter.getLogger(MainApp.class);

protected Ui ui;
protected Logic logic;
protected Storage storage;
protected Model model;
protected ClinicLogic logic;
protected ClinicStorage storage;
protected NewModel model;
protected Config config;

@Override
Expand All @@ -55,42 +55,42 @@ public void init() throws Exception {
config = initConfig(appParameters.getConfigPath());
initLogging(config);

UserPrefsStorage userPrefsStorage = new JsonUserPrefsStorage(config.getUserPrefsFilePath());
UserPrefs userPrefs = initPrefs(userPrefsStorage);
AddressBookStorage addressBookStorage = new JsonAddressBookStorage(userPrefs.getAddressBookFilePath());
storage = new StorageManager(addressBookStorage, userPrefsStorage);
NewUserPrefsStorage userPrefsStorage = new JsonNewUserPrefsStorage(config.getUserPrefsFilePath());
NewUserPrefs userPrefs = initPrefs(userPrefsStorage);
ClinicBookStorage addressBookStorage = new JsonClinicBookStorage(userPrefs.getClinicBookFilePath());
storage = new ClinicStorageManager(addressBookStorage, userPrefsStorage);

model = initModelManager(storage, userPrefs);

logic = new LogicManager(model, storage);
logic = new ClinicLogicManager(model, storage);

ui = new UiManager(logic);
ui = new NewUiManager(logic);
}

/**
* Returns a {@code ModelManager} with the data from {@code storage}'s address book and {@code userPrefs}. <br>
* The data from the sample address book will be used instead if {@code storage}'s address book is not found,
* or an empty address book will be used instead if errors occur when reading {@code storage}'s address book.
*/
private Model initModelManager(Storage storage, ReadOnlyUserPrefs userPrefs) {
logger.info("Using data file : " + storage.getAddressBookFilePath());
private NewModel initModelManager(ClinicStorage storage, NewReadOnlyUserPrefs userPrefs) {
logger.info("Using data file : " + storage.getClinicBookFilePath());

Optional<ReadOnlyAddressBook> addressBookOptional;
ReadOnlyAddressBook initialData;
Optional<ReadOnlyClinicBook> addressBookOptional;
ReadOnlyClinicBook initialData;
try {
addressBookOptional = storage.readAddressBook();
addressBookOptional = storage.readClinicBook();
if (!addressBookOptional.isPresent()) {
logger.info("Creating a new data file " + storage.getAddressBookFilePath()
+ " populated with a sample AddressBook.");
logger.info("Creating a new data file " + storage.getClinicBookFilePath()
+ " populated with a sample AddressBook.");
}
initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook);
initialData = addressBookOptional.orElseGet(NewSampleDataUtil::getSampleClinicBook);
} catch (DataLoadingException e) {
logger.warning("Data file at " + storage.getAddressBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
initialData = new AddressBook();
logger.warning("Data file at " + storage.getClinicBookFilePath() + " could not be loaded."
+ " Will be starting with an empty AddressBook.");
initialData = new ClinicBook();
}

return new ModelManager(initialData, userPrefs);
return new NewModelManager(initialData, userPrefs);
}

private void initLogging(Config config) {
Expand Down Expand Up @@ -123,7 +123,7 @@ protected Config initConfig(Path configFilePath) {
initializedConfig = configOptional.orElse(new Config());
} catch (DataLoadingException e) {
logger.warning("Config file at " + configFilePathUsed + " could not be loaded."
+ " Using default config properties.");
+ " Using default config properties.");
initializedConfig = new Config();
}

Expand All @@ -141,21 +141,21 @@ protected Config initConfig(Path configFilePath) {
* or a new {@code UserPrefs} with default configuration if errors occur when
* reading from the file.
*/
protected UserPrefs initPrefs(UserPrefsStorage storage) {
protected NewUserPrefs initPrefs(NewUserPrefsStorage storage) {
Path prefsFilePath = storage.getUserPrefsFilePath();
logger.info("Using preference file : " + prefsFilePath);

UserPrefs initializedPrefs;
NewUserPrefs initializedPrefs;
try {
Optional<UserPrefs> prefsOptional = storage.readUserPrefs();
Optional<NewUserPrefs> prefsOptional = storage.readUserPrefs();
if (!prefsOptional.isPresent()) {
logger.info("Creating new preference file " + prefsFilePath);
}
initializedPrefs = prefsOptional.orElse(new UserPrefs());
initializedPrefs = prefsOptional.orElse(new NewUserPrefs());
} catch (DataLoadingException e) {
logger.warning("Preference file at " + prefsFilePath + " could not be loaded."
+ " Using default preferences.");
initializedPrefs = new UserPrefs();
+ " Using default preferences.");
initializedPrefs = new NewUserPrefs();
}

//Update prefs file in case it was missing to begin with or there are new/unused fields
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/seedu/cc/model/NewModel.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,12 @@ public interface NewModel {
/**
* Replaces user prefs data with the data in {@code userPrefs}.
*/
void setUserPrefs(ReadOnlyUserPrefs userPrefs);
void setUserPrefs(NewReadOnlyUserPrefs newUserPrefs);

/**
* Returns the user prefs.
*/
ReadOnlyUserPrefs getUserPrefs();
NewReadOnlyUserPrefs getUserPrefs();

/**
* Returns the user prefs' GUI settings.
Expand Down
26 changes: 13 additions & 13 deletions src/main/java/seedu/cc/model/NewModelManager.java
Original file line number Diff line number Diff line change
Expand Up @@ -20,59 +20,59 @@ public class NewModelManager implements NewModel {
private static final Logger logger = LogsCenter.getLogger(ModelManager.class);

private final ClinicBook clinicBook;
private final UserPrefs userPrefs;
private final NewUserPrefs newUserPrefs;
private final FilteredList<Patient> filteredPatients;

/**
* Initializes a NewModelManager with the given clinicBook and userPrefs.
*/
public NewModelManager(ReadOnlyClinicBook clinicBook, ReadOnlyUserPrefs userPrefs) {
public NewModelManager(ReadOnlyClinicBook clinicBook, NewReadOnlyUserPrefs userPrefs) {
requireAllNonNull(clinicBook, userPrefs);

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

this.clinicBook = new ClinicBook(clinicBook);
this.userPrefs = new UserPrefs(userPrefs);
this.newUserPrefs = new NewUserPrefs(userPrefs);
this.filteredPatients = new FilteredList<>(this.clinicBook.getPatientList());
}

public NewModelManager() {
this(new ClinicBook(), new UserPrefs());
this(new ClinicBook(), new NewUserPrefs());
}

//=========== UserPrefs ==================================================================================

@Override
public void setUserPrefs(ReadOnlyUserPrefs userPrefs) {
public void setUserPrefs(NewReadOnlyUserPrefs userPrefs) {
requireNonNull(userPrefs);
this.userPrefs.resetData(userPrefs);
this.newUserPrefs.resetData(userPrefs);
}

@Override
public ReadOnlyUserPrefs getUserPrefs() {
return userPrefs;
public NewReadOnlyUserPrefs getUserPrefs() {
return newUserPrefs;
}

@Override
public GuiSettings getGuiSettings() {
return userPrefs.getGuiSettings();
return newUserPrefs.getGuiSettings();
}

@Override
public void setGuiSettings(GuiSettings guiSettings) {
requireNonNull(guiSettings);
userPrefs.setGuiSettings(guiSettings);
newUserPrefs.setGuiSettings(guiSettings);
}

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

@Override
public void setClinicBookFilePath(Path clinicBookFilePath) {
requireNonNull(clinicBookFilePath);
userPrefs.setAddressBookFilePath(clinicBookFilePath);
newUserPrefs.setClinicBookFilePath(clinicBookFilePath);
}

//=========== AddressBook ================================================================================
Expand Down Expand Up @@ -141,7 +141,7 @@ public boolean equals(Object other) {

NewModelManager otherModelManager = (NewModelManager) other;
return clinicBook.equals(otherModelManager.clinicBook)
&& userPrefs.equals(otherModelManager.userPrefs)
&& newUserPrefs.equals(otherModelManager.newUserPrefs)
&& filteredPatients.equals(otherModelManager.filteredPatients);
}

Expand Down
61 changes: 61 additions & 0 deletions src/main/java/seedu/cc/model/util/NewSampleDataUtil.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package seedu.cc.model.util;

import java.util.Arrays;
import java.util.Set;
import java.util.stream.Collectors;

import seedu.cc.model.ClinicBook;
import seedu.cc.model.ReadOnlyClinicBook;
import seedu.cc.model.patient.Nric;
import seedu.cc.model.patient.Patient;
import seedu.cc.model.person.Address;
import seedu.cc.model.person.Email;
import seedu.cc.model.person.Name;
import seedu.cc.model.person.Phone;
import seedu.cc.model.tag.Tag;

/**
* Contains utility methods for populating {@code AddressBook} with sample data.
*/
public class NewSampleDataUtil {
public static Patient[] getSamplePatients() {
return new Patient[]{
new Patient(new Name("Alex Yeoh"), new Nric("S5323891B"), new Phone("87438807"),
new Email("[email protected]"), new Address("Blk 30 Geylang Street 29, #06-40"),
getTagSet("friends")),
new Patient(new Name("Bernice Yu"), new Nric("S5323891B"), new Phone("99272758"),
new Email("[email protected]"), new Address("Blk 30 Lorong 3 Serangoon Gardens, #07-18"),
getTagSet("colleagues", "friends")),
new Patient(new Name("Charlotte Oliveiro"), new Nric("S5323891B"), new Phone("93210283"),
new Email("[email protected]"), new Address("Blk 11 Ang Mo Kio Street 74, #11-04"),
getTagSet("neighbours")),
new Patient(new Name("David Li"), new Nric("S5323891B"), new Phone("91031282"),
new Email("[email protected]"), new Address("Blk 436 Serangoon Gardens Street 26, #16-43"),
getTagSet("family")),
new Patient(new Name("Irfan Ibrahim"), new Nric("S5323891B"), new Phone("92492021"),
new Email("[email protected]"), new Address("Blk 47 Tampines Street 20, #17-35"),
getTagSet("classmates")),
new Patient(new Name("Roy Balakrishnan"), new Nric("S5323891B"), new Phone("92624417"),
new Email("[email protected]"), new Address("Blk 45 Aljunied Street 85, #11-31"),
getTagSet("colleagues"))
};
}

public static ReadOnlyClinicBook getSampleClinicBook() {
ClinicBook sampleAb = new ClinicBook();
for (Patient samplePatient : getSamplePatients()) {
sampleAb.addPatient(samplePatient);
}
return sampleAb;
}

/**
* Returns a tag set containing the list of strings given.
*/
public static Set<Tag> getTagSet(String... strings) {
return Arrays.stream(strings)
.map(Tag::new)
.collect(Collectors.toSet());
}

}
14 changes: 7 additions & 7 deletions src/main/java/seedu/cc/ui/MainWindow.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import javafx.stage.Stage;
import seedu.cc.commons.core.GuiSettings;
import seedu.cc.commons.core.LogsCenter;
import seedu.cc.logic.Logic;
import seedu.cc.logic.ClinicLogic;
import seedu.cc.logic.commands.CommandResult;
import seedu.cc.logic.commands.exceptions.CommandException;
import seedu.cc.logic.parser.exceptions.ParseException;
Expand All @@ -28,10 +28,10 @@ public class MainWindow extends UiPart<Stage> {
private final Logger logger = LogsCenter.getLogger(getClass());

private Stage primaryStage;
private Logic logic;
private ClinicLogic logic;

// Independent Ui parts residing in this Ui container
private PersonListPanel personListPanel;
private PatientListPanel personListPanel;
private ResultDisplay resultDisplay;
private HelpWindow helpWindow;

Expand All @@ -53,7 +53,7 @@ public class MainWindow extends UiPart<Stage> {
/**
* Creates a {@code MainWindow} with the given {@code Stage} and {@code Logic}.
*/
public MainWindow(Stage primaryStage, Logic logic) {
public MainWindow(Stage primaryStage, ClinicLogic logic) {
super(FXML, primaryStage);

// Set dependencies
Expand Down Expand Up @@ -110,13 +110,13 @@ private void setAccelerator(MenuItem menuItem, KeyCombination keyCombination) {
* Fills up all the placeholders of this window.
*/
void fillInnerParts() {
personListPanel = new PersonListPanel(logic.getFilteredPersonList());
personListPanel = new PatientListPanel(logic.getFilteredPatientList());
personListPanelPlaceholder.getChildren().add(personListPanel.getRoot());

resultDisplay = new ResultDisplay();
resultDisplayPlaceholder.getChildren().add(resultDisplay.getRoot());

StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getAddressBookFilePath());
StatusBarFooter statusBarFooter = new StatusBarFooter(logic.getClinicBookFilePath());
statusbarPlaceholder.getChildren().add(statusBarFooter.getRoot());

CommandBox commandBox = new CommandBox(this::executeCommand);
Expand Down Expand Up @@ -163,7 +163,7 @@ private void handleExit() {
primaryStage.hide();
}

public PersonListPanel getPersonListPanel() {
public PatientListPanel getPatientListPanel() {
return personListPanel;
}

Expand Down
Loading

0 comments on commit 40ebf14

Please sign in to comment.