forked from nus-cs2113-AY2223S2/tp
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request nus-cs2113-AY2223S2#23 from AY2223S2-CS2113-F13-3/…
…Storage Merge Storage to master
- Loading branch information
Showing
4 changed files
with
174 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package seedu.duke.storage; | ||
|
||
import com.google.gson.GsonBuilder; | ||
import com.google.gson.Gson; | ||
import com.google.gson.reflect.TypeToken; | ||
import com.google.gson.stream.JsonReader; | ||
import seedu.duke.Event; | ||
import seedu.duke.EventList; | ||
import java.io.File; | ||
import java.io.IOException; | ||
import java.io.FileWriter; | ||
import java.io.InputStreamReader; | ||
import java.io.FileInputStream; | ||
import java.nio.charset.StandardCharsets; | ||
import java.util.ArrayList; | ||
|
||
/* | ||
//Importing classes from ui, parser and Duke. | ||
import seedu.duke.Parser | ||
import seedu.duke.ui.Ui | ||
import seedu.duke.Duke | ||
import seedu.duke.Event | ||
import seedu.duke.EventList | ||
*/ | ||
|
||
|
||
public class Storage { | ||
//private final Ui ui = new Ui(); | ||
private static final String fileLocation = System.getProperty("user.dir") + "/save.json"; | ||
final TypeToken<ArrayList<Event>> eventToken = new TypeToken<ArrayList<Event>>(){}; | ||
GsonBuilder builder = new GsonBuilder().setDateFormat("YYYY/MM/DD HH:mm:ss").setPrettyPrinting(); | ||
Gson gson = builder.create(); | ||
|
||
public void saveToFile(EventList eventList) { | ||
File saveFile = new File(fileLocation); | ||
String gsonData = gson.toJson(eventList.getFullList()); | ||
if (!saveFile.exists()) { | ||
try { | ||
saveFile.createNewFile(); | ||
} catch (IOException e) { | ||
//ui.showException("IOException"); | ||
} | ||
|
||
} | ||
try { | ||
FileWriter taskWriter; | ||
taskWriter = new FileWriter(saveFile.getAbsoluteFile(), false); | ||
taskWriter.write(gsonData); | ||
taskWriter.close(); | ||
} catch (IOException e) { | ||
System.out.println("IOException"); | ||
} | ||
} | ||
|
||
|
||
public ArrayList<Event> loadEvents() { | ||
File saveFile = new File(fileLocation); | ||
ArrayList<Event> savedList = new ArrayList<>(); | ||
if (!saveFile.exists()) { | ||
return savedList; | ||
} | ||
InputStreamReader fileReader; | ||
try { | ||
fileReader = new InputStreamReader(new FileInputStream(saveFile), StandardCharsets.UTF_8); | ||
JsonReader gsonInterpreter = new JsonReader(fileReader); | ||
savedList = gson.fromJson(fileReader, eventToken); //Placeholder. | ||
} catch (Exception e) { | ||
System.out.println(e); | ||
} | ||
return savedList; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
package seedu.duke.storage; | ||
|
||
import org.junit.jupiter.api.Order; | ||
import org.junit.jupiter.api.Test; | ||
|
||
import seedu.duke.EventList; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
|
||
class StorageTest { | ||
|
||
private static final String fileLocation = System.getProperty("user.dir") + "/save.json"; | ||
File saveFile = new File(fileLocation); | ||
private final Storage storage = new Storage(); | ||
@Test | ||
@Order(1) | ||
public void emptyLoad(){ //check that it can load when there isn't an instance of save.json in the path. | ||
saveFile.delete(); | ||
EventList testList = new EventList(storage.loadEvents()); | ||
storage.loadEvents(); | ||
assert((testList.getSize()==0) && (!saveFile.exists())); | ||
} | ||
@Test | ||
@Order(2) | ||
public void saveEvents(){ //check that storage class saves a file | ||
EventList testList = new EventList(); | ||
testList.addEvent("testing", "10:00", "2023/03/20", "10:00", "2023/03/21"); | ||
testList.addEvent("testing2","03:24", "2023/04/01", "08:50", "2023/03/23"); | ||
storage.saveToFile(testList); | ||
assert(saveFile.exists()); | ||
saveFile.delete(); | ||
} | ||
|
||
|
||
@Test | ||
@Order(3) | ||
public void loadEvents() throws FileNotFoundException { | ||
//checks that storage EventList from deserialized save.json matches original EventList data | ||
EventList original = new EventList(); | ||
original.addEvent("testing", "10:00", "2023/03/20", | ||
"10:00", "2023/03/21"); | ||
original.addEvent("testing2","03:24", "2023/04/01", | ||
"08:50", "2023/03/23"); | ||
storage.saveToFile(original); | ||
EventList testListCheck = new EventList(); | ||
testListCheck.addEvent("testing", "10:00", "2023/03/20", | ||
"10:00", "2023/03/21"); | ||
testListCheck.addEvent("testing2","03:24", "2023/04/01", | ||
"08:50", "2023/03/23"); | ||
EventList testList = new EventList(storage.loadEvents()); | ||
String a = testListCheck.getFullList().toString(); | ||
String b = testList.getFullList().toString(); | ||
assert(a.equals(b)); | ||
saveFile.delete(); | ||
} | ||
@Test | ||
@Order(4) | ||
public void updateEvents(){ //Check that data can be updated and matches what was updated. | ||
EventList original = new EventList(); | ||
original.addEvent("testing", "10:00", "2023/03/20", | ||
"10:00", "2023/03/21"); | ||
original.addEvent("testing2","03:24", "2023/04/01", | ||
"08:50", "2023/03/23"); | ||
storage.saveToFile(original); | ||
EventList edited = new EventList(storage.loadEvents()); | ||
edited.addEvent("edit new event", "19:00", "2023/03/20", "13:00", "2023/03/21"); | ||
storage.saveToFile(edited); | ||
EventList savedEvent = new EventList(storage.loadEvents()); | ||
String a = edited.getFullList().toString(); | ||
String b = savedEvent.getFullList().toString(); | ||
assert(a.equals(b)); | ||
saveFile.delete(); | ||
} | ||
} |