Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[T6A1][F14-B3] Leslie #507

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 12 additions & 13 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.parser.Parser;
import seedu.addressbook.storage.Storage;
import seedu.addressbook.storage.StorageFile;

import java.util.Collections;
Expand All @@ -17,41 +18,37 @@
public class Logic {


private StorageFile storage;
private Storage storage;
private AddressBook addressBook;

/** The list of person shown to the user most recently. */
private List<? extends ReadOnlyPerson> lastShownList = Collections.emptyList();

public Logic() throws Exception{
setStorage(initializeStorage());
setStorage(initializeStorage());
setAddressBook(storage.load());
}

Logic(StorageFile storageFile, AddressBook addressBook){
setStorage(storageFile);
Logic(Storage storage, AddressBook addressBook){
setStorage(storage);
setAddressBook(addressBook);
}

void setStorage(StorageFile storage){
void setStorage(Storage storage){
this.storage = storage;
}

void setAddressBook(AddressBook addressBook){
this.addressBook = addressBook;
}

/**
* Creates the StorageFile object based on the user specified path (if any) or the default storage path.
* @throws StorageFile.InvalidStorageFilePathException if the target file path is incorrect.
*/
private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePathException {
return new StorageFile();
}

public String getStorageFilePath() {
return storage.getPath();
}

private Storage initializeStorage() throws StorageFile.InvalidStorageFilePathException {
return new StorageFile();
}

/**
* Unmodifiable view of the current last shown list.
Expand Down Expand Up @@ -88,6 +85,8 @@ private CommandResult execute(Command command) throws Exception {
storage.save(addressBook);
return result;
}



/** Updates the {@link #lastShownList} if the result contains a list of Persons. */
private void recordResult(CommandResult result) {
Expand Down
14 changes: 14 additions & 0 deletions src/seedu/addressbook/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package seedu.addressbook.storage;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.storage.StorageFile.StorageOperationException;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This class and its methods should have a header comments. If not, developers who needs to inherit from this class will not know how exactly they should override the methods and other developers who write client code for this class will not know how to use it either.

public abstract class Storage {

public abstract void save(AddressBook addressBook) throws StorageOperationException;

public abstract AddressBook load() throws StorageOperationException;

public abstract String getPath();

}
2 changes: 1 addition & 1 deletion src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
/**
* Represents the file used to store address book data.
*/
public class StorageFile {
public class StorageFile extends Storage {

/** Default file path used if the user doesn't provide the file name. */
public static final String DEFAULT_STORAGE_FILEPATH = "addressbook.txt";
Expand Down
29 changes: 29 additions & 0 deletions src/seedu/addressbook/storage/StorageStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package seedu.addressbook.storage;
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It’s better to add the StorageStub class in the test\java\seedu\addressbook\storage folder because it is used for testing only.


import seedu.addressbook.data.AddressBook;
import seedu.addressbook.storage.StorageFile.StorageOperationException;

import java.nio.file.Path;
import java.nio.file.Paths;

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Header comments?

public class StorageStub extends Storage {

public StorageStub(String path) {
}

@Override
public void save(AddressBook addressBook) throws StorageOperationException {

}

@Override
public AddressBook load() throws StorageOperationException {
return null;
}

@Override
public String getPath() {
return null;
}

}
12 changes: 6 additions & 6 deletions test/java/seedu/addressbook/logic/LogicTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
import seedu.addressbook.data.person.*;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.storage.StorageFile;
import seedu.addressbook.storage.StorageStub;

import java.util.*;

Expand All @@ -28,16 +28,16 @@ public class LogicTest {
@Rule
public TemporaryFolder saveFolder = new TemporaryFolder();

private StorageFile saveFile;
private StorageStub saveStub;
private AddressBook addressBook;
private Logic logic;

@Before
public void setup() throws Exception {
saveFile = new StorageFile(saveFolder.newFile("testSaveFile.txt").getPath());
saveStub = new StorageStub(saveFolder.newFile("testsaveStub.txt").getPath());
addressBook = new AddressBook();
saveFile.save(addressBook);
logic = new Logic(saveFile, addressBook);
saveStub.save(addressBook);
logic = new Logic(saveStub, addressBook);
}

@Test
Expand Down Expand Up @@ -90,7 +90,7 @@ private void assertCommandBehavior(String inputCommand,
//Confirm the state of data is as expected
assertEquals(expectedAddressBook, addressBook);
assertEquals(lastShownList, logic.getLastShownList());
assertEquals(addressBook, saveFile.load());
assertEquals(addressBook, saveStub.load());
}


Expand Down