diff --git a/.idea/libraries/google_code_gson.xml b/.idea/libraries/google_code_gson.xml new file mode 100644 index 0000000..b8c8e54 --- /dev/null +++ b/.idea/libraries/google_code_gson.xml @@ -0,0 +1,10 @@ + + + + + + + + + + \ No newline at end of file diff --git a/cmpt213.assignment1.packagedeliveriestracker.iml b/cmpt213.assignment1.packagedeliveriestracker.iml index c90834f..b5d0958 100644 --- a/cmpt213.assignment1.packagedeliveriestracker.iml +++ b/cmpt213.assignment1.packagedeliveriestracker.iml @@ -7,5 +7,6 @@ + \ No newline at end of file diff --git a/list.json b/list.json new file mode 100644 index 0000000..759f080 --- /dev/null +++ b/list.json @@ -0,0 +1 @@ +[{"name":"name","note":"notes","price":15.0,"weight":5.0,"delivered":false,"expectedDate":"2022-07-20T12:12"}] \ No newline at end of file diff --git a/out/production/cmpt213.assignment1.packagedeliveriestracker/Main$1.class b/out/production/cmpt213.assignment1.packagedeliveriestracker/Main$1.class new file mode 100644 index 0000000..b7ad654 Binary files /dev/null and b/out/production/cmpt213.assignment1.packagedeliveriestracker/Main$1.class differ diff --git a/out/production/cmpt213.assignment1.packagedeliveriestracker/Main.class b/out/production/cmpt213.assignment1.packagedeliveriestracker/Main.class index fcc1047..5a2f52e 100644 Binary files a/out/production/cmpt213.assignment1.packagedeliveriestracker/Main.class and b/out/production/cmpt213.assignment1.packagedeliveriestracker/Main.class differ diff --git a/src/Main.java b/src/Main.java index 6a5eb99..2225a17 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,13 +1,70 @@ +import com.google.gson.*; +import com.google.gson.stream.JsonReader; +import com.google.gson.stream.JsonWriter; + +import java.io.*; +import java.time.LocalDateTime; +import java.time.chrono.ChronoLocalDate; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.List; import java.util.Scanner; public class Main { - +private static final String fileName = "list.json"; private static ArrayList packageList=new ArrayList<>(); +private static void save(){ + Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class, + new TypeAdapter() { + @Override + public void write(JsonWriter jsonWriter, + LocalDateTime localDateTime) throws IOException { + jsonWriter.value(localDateTime.toString()); + } + @Override + public LocalDateTime read(JsonReader jsonReader) throws IOException { + return LocalDateTime.parse(jsonReader.nextString()); + } + }).create(); + try{ + Writer w = new FileWriter(fileName); + gson.toJson(packageList, w); + w.flush(); + w.close(); + } + catch (IOException e){ + e.printStackTrace(); + } +} + +private static void load(){ + File file = new File(fileName); +try{ + JsonElement element = JsonParser.parseReader(new FileReader(fileName)); + JsonArray array = element.getAsJsonArray(); + for (int i = 0; i < array.size(); i++){ + JsonObject pObj = array.get(i).getAsJsonObject(); + System.out.println(pObj.get("expectedDate").getAsString()); + DateTimeFormatter format = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm"); + packageList.add(new PackageInfo(pObj.get("name").getAsString(), + pObj.get("note").getAsString(), + pObj.get("price").getAsDouble(), + pObj.get("weight").getAsDouble(), + pObj.get("delivered").getAsBoolean(), + //fix date + LocalDateTime.parse(pObj.get("expectedDate").getAsString(), format)) + ); + } + System.out.println("packages loaded"); +} +catch (FileNotFoundException e){ +System.out.println("no packages to load"); + } +} public static void main(String[] args) { TextMenu menu=new TextMenu(); +load(); do{ menu.display(); System.out.println("choose an option between 1 and 7:"); @@ -17,6 +74,8 @@ do{ System.out.println("invalid input"); } else if(option == 7){ + save(); + System.out.print("packages saved"); break; } else{ @@ -37,6 +96,5 @@ do{ } while (true); } - }