Skip to content

Commit

Permalink
Merge pull request nus-cs2113-AY2223S2#23 from AY2223S2-CS2113-F13-3/…
Browse files Browse the repository at this point in the history
…Storage

Merge Storage to master
  • Loading branch information
kyrixn authored Mar 11, 2023
2 parents 224d5fd + 460df9c commit 3993b81
Show file tree
Hide file tree
Showing 4 changed files with 174 additions and 0 deletions.
2 changes: 2 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ repositories {
}

dependencies {

implementation 'com.google.code.gson:gson:2.10.1'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0'
testRuntimeOnly group: 'org.junit.jupiter', name: 'junit-jupiter-engine', version: '5.5.0'
}
Expand Down
25 changes: 25 additions & 0 deletions src/main/java/seedu/duke/Duke.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,35 @@
package seedu.duke;

import java.util.Scanner;
//import seedu.duke.ui.Ui;
//import seedu.duke.EventList;
//import seedu.duke.parser.Parser;


public class Duke {
/**
* Main entry-point for the java.duke.Duke application.
*/
//private final TaskList tasks;
//private final Ui ui;
//private final Parser parser;
public Duke (){
//ui = new Ui();
//tasks = new TaskList();
//parser = new Parser();
}
public void run(){
//ui.showWelcome();
//boolean isExit = false;
//while(!isExit){
//String fullCommand = ui.getUserCommand();
//parser.parseInput(fullCommand, tasks);
//if (fullCommand.equalsIgnoreCase("bye")){
//isExit = true;
//}
//}
}

public static void main(String[] args) {
String logo = " ____ _ \n"
+ "| _ \\ _ _| | _____ \n"
Expand All @@ -19,3 +43,4 @@ public static void main(String[] args) {
System.out.println("Hello " + in.nextLine());
}
}

72 changes: 72 additions & 0 deletions src/main/java/seedu/duke/storage/Storage.java
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;
}
}
75 changes: 75 additions & 0 deletions src/test/java/seedu/duke/storage/StorageTest.java
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();
}
}

0 comments on commit 3993b81

Please sign in to comment.