diff --git a/src/main/java/seedu/address/MainApp.java b/src/main/java/seedu/address/MainApp.java index fb4fbfc2e66..a31f65ec155 100644 --- a/src/main/java/seedu/address/MainApp.java +++ b/src/main/java/seedu/address/MainApp.java @@ -15,8 +15,11 @@ import seedu.address.commons.util.StringUtil; import seedu.address.logic.Logic; import seedu.address.logic.LogicManager; +import seedu.address.model.CustomerBook; import seedu.address.model.Model; import seedu.address.model.ModelManager; +import seedu.address.model.OrderBook; +import seedu.address.model.PhoneBook; import seedu.address.model.ReadOnlyDataBook; import seedu.address.model.ReadOnlyUserPrefs; import seedu.address.model.ScheduleBook; @@ -92,26 +95,73 @@ public void init() throws Exception { * 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) { - /*Optional addressBookOptional; - ReadOnlyAddressBook initialData; + Optional> customerBookOptional; + Optional> phoneBookOptional; + Optional> orderBookOptional; + + ReadOnlyDataBook initialCustomerData; + ReadOnlyDataBook initialPhoneData; + ReadOnlyDataBook initialOrderData; + try { - addressBookOptional = storage.readAddressBook(); - if (!addressBookOptional.isPresent()) { - logger.info("Data file not found. Will be starting with a sample AddressBook"); + customerBookOptional = storage.readCustomerBook(); + + if (!customerBookOptional.isPresent()) { + logger.info("Data file not found. Will be starting with a sample CustomerBook"); } - initialData = addressBookOptional.orElseGet(SampleDataUtil::getSampleAddressBook); + initialCustomerData = customerBookOptional.orElseGet(SampleDataUtil::getSampleCustomerBook); + } catch (DataConversionException e) { - logger.warning("Data file not in the correct format. Will be starting with an empty AddressBook"); - initialData = new AddressBook(); + logger.warning("Data file not in the correct format. Will be starting with an empty CustomerBook"); + initialCustomerData = new CustomerBook(); + } catch (IOException e) { - logger.warning("Problem while reading from the file. Will be starting with an empty AddressBook"); - initialData = new AddressBook(); - }*/ + logger.warning("Problem while reading from the file. Will be starting with an empty CustomerBook"); + initialCustomerData = new CustomerBook(); + } + + try { + phoneBookOptional = storage.readPhoneBook(); + + if (!phoneBookOptional.isPresent()) { + logger.info("Data file not found. Will be starting with a sample PhoneBook"); + } + + initialPhoneData = phoneBookOptional.orElseGet(SampleDataUtil::getSamplePhoneBook); + + } catch (DataConversionException e) { + logger.warning("Data file not in the correct format. Will be starting with an empty PhoneBook"); + initialPhoneData = new PhoneBook(); + + } catch (IOException e) { + logger.warning("Problem while reading from the file. Will be starting with an empty PhoneBook"); + initialPhoneData = new PhoneBook(); + } + + try { + orderBookOptional = storage.readOrderBook(); + + if (!orderBookOptional.isPresent()) { + logger.info("Data file not found. Will be starting with a sample OrderBook"); + } + + initialOrderData = orderBookOptional.orElseGet(SampleDataUtil::getSampleOrderBook); + + } catch (DataConversionException e) { + logger.warning("Data file not in the correct format. Will be starting with an empty OrderBook"); + + initialOrderData = new OrderBook(); + + } catch (IOException e) { + logger.warning("Problem while reading from the file. Will be starting with an empty OrderBook"); + initialOrderData = new OrderBook(); + } + + //ReadOnlyDataBook customerBook = SampleDataUtil.getSampleCustomerBook(); + //ReadOnlyDataBook phoneBook = SampleDataUtil.getSamplePhoneBook(); + //ReadOnlyDataBook orderBook = SampleDataUtil.getSampleOrderBook(); - ReadOnlyDataBook customerBook = SampleDataUtil.getSampleCustomerBook(); - ReadOnlyDataBook phoneBook = SampleDataUtil.getSamplePhoneBook(); - ReadOnlyDataBook orderBook = SampleDataUtil.getSampleOrderBook(); - return new ModelManager(customerBook, phoneBook, orderBook, new ScheduleBook(), userPrefs); + return new ModelManager(initialCustomerData, initialPhoneData, initialOrderData, new ScheduleBook(), userPrefs); } diff --git a/src/main/java/seedu/address/logic/LogicManager.java b/src/main/java/seedu/address/logic/LogicManager.java index de02a0b2bb1..771917defbe 100644 --- a/src/main/java/seedu/address/logic/LogicManager.java +++ b/src/main/java/seedu/address/logic/LogicManager.java @@ -51,27 +51,9 @@ public CommandResult execute(String commandText) throws CommandException, ParseE Command command = addressBookParser.parseCommand(commandText); commandResult = command.execute(model); - try { - storage.saveAddressBook(model.getAddressBook()); - } catch (IOException ioe) { - throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe); - } try { storage.saveCustomerBook(model.getCustomerBook()); - } catch (IOException ioe) { - throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe); - } - try { storage.savePhoneBook(model.getPhoneBook()); - } catch (IOException ioe) { - throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe); - } - try { - storage.saveScheduleBook(model.getScheduleBook()); - } catch (IOException ioe) { - throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe); - } - try { storage.saveOrderBook(model.getOrderBook()); } catch (IOException ioe) { throw new CommandException(FILE_OPS_ERROR_MESSAGE + ioe, ioe);