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

[T6A2][F11-B3]LEUNG Cheuk Ting #508

Open
wants to merge 2 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
12 changes: 6 additions & 6 deletions src/seedu/addressbook/logic/Logic.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,18 @@
import seedu.addressbook.data.person.ReadOnlyPerson;
import seedu.addressbook.parser.Parser;
import seedu.addressbook.storage.StorageFile;
import seedu.addressbook.storage.Storage;

import java.util.Collections;
import java.util.List;
import java.util.Optional;

/**
* Represents the main Logic of the AddressBook.
*/
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){
Logic(Storage storageFile, AddressBook addressBook){
setStorage(storageFile);
setAddressBook(addressBook);
}

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

Expand All @@ -43,9 +43,9 @@ void setAddressBook(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.
* @throws Storage.InvalidStorageFilePathException if the target file path is incorrect.
*/
private StorageFile initializeStorage() throws StorageFile.InvalidStorageFilePathException {
private Storage initializeStorage() throws Storage.InvalidStorageFilePathException {
return new StorageFile();
}

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

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

public abstract class Storage {

// add all the abstract function

public abstract void save(AddressBook addressBook) throws StorageOperationException;

public abstract AddressBook load() throws StorageOperationException;

public abstract String getPath();

}
17 changes: 14 additions & 3 deletions src/seedu/addressbook/storage/StorageFile.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,9 @@
/**
* 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 All @@ -27,12 +29,15 @@ public class StorageFile {
/**
* Signals that the given file path does not fulfill the storage filepath constraints.
*/
//used in logic class, need an abstruct func


public static class InvalidStorageFilePathException extends IllegalValueException {
public InvalidStorageFilePathException(String message) {
super(message);
}
}

/**
* Signals that some error has occured while trying to convert and read/write data between the application
* and the storage file.
Expand Down Expand Up @@ -83,6 +88,7 @@ private static boolean isValidPath(Path filePath) {
*
* @throws StorageOperationException if there were errors converting and/or storing data to file.
*/
//used in logic class, need abs function
public void save(AddressBook addressBook) throws StorageOperationException {

/* Note: Note the 'try with resource' statement below.
Expand All @@ -108,6 +114,9 @@ public void save(AddressBook addressBook) throws StorageOperationException {
*
* @throws StorageOperationException if there were errors reading and/or converting data from file.
*/

//used in logic class, need an abstruct func

public AddressBook load() throws StorageOperationException {
try (final Reader fileReader =
new BufferedReader(new FileReader(path.toFile()))) {
Expand Down Expand Up @@ -140,7 +149,9 @@ public AddressBook load() throws StorageOperationException {
throw new StorageOperationException("File contains illegal data values; data type constraints not met");
}
}


//used in logic class, need an abstruct func

public String getPath() {
return path.toString();
}
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.

You also need to update the test case.
We are using stub here, which means in LogicTest we only care whether the logic works or not. We don't need to test the storage part anymore. (Yeah, there is somewhere in LogicTest that verifies whether the storage works or not. Find by yourself. :) )


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

public class StorageStub extends Storage {

private String path;
private AddressBook addressBook;

public StorageStub(String p) {
// TODO Auto-generated constructor stub
this.path = p;
}

public void save(AddressBook addressBook) throws StorageOperationException{

}



public AddressBook load() throws StorageOperationException{
return this.addressBook;
Copy link

Choose a reason for hiding this comment

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

Why do you need this code here? A return null is enough? Stubs don't usually do any work.

}

public String getPath(){
return this.path;
}
}
7 changes: 4 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,8 @@
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 +29,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