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-B1]Leon Mak #498

Open
wants to merge 4 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
10 changes: 5 additions & 5 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 @@ -16,8 +17,7 @@
*/
public class Logic {


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

/** The list of person shown to the user most recently. */
Expand All @@ -28,12 +28,12 @@ public Logic() throws Exception{
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;
}

Expand Down
42 changes: 42 additions & 0 deletions src/seedu/addressbook/storage/Storage.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
package seedu.addressbook.storage;

import java.nio.file.Path;

import seedu.addressbook.data.AddressBook;
import seedu.addressbook.data.exception.IllegalValueException;
import seedu.addressbook.storage.Storage.StorageOperationException;

public abstract class Storage {

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.


protected Path path;

public Storage() {
}

/* Note: Note the use of nested classes below.
* More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
*/

/**
* Signals that some error has occured while trying to convert and read/write data between the application
* and the storage file.
*/
public static class StorageOperationException extends Exception {
public StorageOperationException(String message) {
super(message);
}
}

public abstract void save(AddressBook addressBook) throws StorageOperationException;

Choose a reason for hiding this comment

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

It is better to give the method header comments in the parent class rather than the child class. The child class can inherit the comment from the parent.


public abstract AddressBook load() throws StorageOperationException;

public String getPath() {
return path.toString();
}

public void setPath(Path path) {
this.path = path;
}

}
26 changes: 3 additions & 23 deletions src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,12 @@
/**
* 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";

/* Note: Note the use of nested classes below.
* More info https://docs.oracle.com/javase/tutorial/java/javaOO/nested.html
*/
private final JAXBContext jaxbContext;

/**
* Signals that the given file path does not fulfill the storage filepath constraints.
Expand All @@ -33,20 +31,6 @@ public InvalidStorageFilePathException(String message) {
}
}

/**
* Signals that some error has occured while trying to convert and read/write data between the application
* and the storage file.
*/
public static class StorageOperationException extends Exception {
public StorageOperationException(String message) {
super(message);
}
}

private final JAXBContext jaxbContext;

public final Path path;

/**
* @throws InvalidStorageFilePathException if the default path is invalid
*/
Expand All @@ -64,7 +48,7 @@ public StorageFile(String filePath) throws InvalidStorageFilePathException {
throw new RuntimeException("jaxb initialisation error");
}

path = Paths.get(filePath);
setPath(Paths.get(filePath));
if (!isValidPath(path)) {
throw new InvalidStorageFilePathException("Storage file should end with '.txt'");
}
Expand Down Expand Up @@ -141,8 +125,4 @@ public AddressBook load() throws StorageOperationException {
}
}

public String getPath() {
return path.toString();
}

}
30 changes: 30 additions & 0 deletions src/seedu/addressbook/storage/StorageStub.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package seedu.addressbook.storage;

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

import seedu.addressbook.data.AddressBook;

public class StorageStub extends Storage {

AddressBook addressBook;

public StorageStub(String pathString) {
setPath(Paths.get(pathString));
}

public StorageStub(Path path) {
setPath(path);
}

@Override
public void save(AddressBook addressBook) throws StorageOperationException {
this.addressBook = addressBook;
}

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

}
6 changes: 3 additions & 3 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,13 +28,13 @@ public class LogicTest {
@Rule
public TemporaryFolder saveFolder = new TemporaryFolder();

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

@Before
public void setup() throws Exception {
saveFile = new StorageFile(saveFolder.newFile("testSaveFile.txt").getPath());
saveFile = new StorageStub(saveFolder.newFile("testSaveFile.txt").getPath());
addressBook = new AddressBook();
saveFile.save(addressBook);
logic = new Logic(saveFile, addressBook);
Expand Down
2 changes: 1 addition & 1 deletion test/java/seedu/addressbook/storage/StorageFileTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
import seedu.addressbook.data.person.Phone;
import seedu.addressbook.data.tag.Tag;
import seedu.addressbook.data.tag.UniqueTagList;
import seedu.addressbook.storage.StorageFile.StorageOperationException;
import seedu.addressbook.storage.Storage.StorageOperationException;
import static seedu.addressbook.util.TestUtil.assertTextFilesEqual;

public class StorageFileTest {
Expand Down