|
|
|
@ -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<PackageInfo> packageList=new ArrayList<>();
|
|
|
|
|
private static void save(){
|
|
|
|
|
Gson gson = new GsonBuilder().registerTypeAdapter(LocalDateTime.class,
|
|
|
|
|
new TypeAdapter<LocalDateTime>() {
|
|
|
|
|
@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);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|