You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

83 lines
3.4 KiB

package cmpt213.assignment4.packagedeliveries.client.control;
import cmpt213.assignment4.packagedeliveries.client.gson.extras.RuntimeTypeAdapterFactory;
import cmpt213.assignment4.packagedeliveries.client.model.BookPackage;
import cmpt213.assignment4.packagedeliveries.client.model.ElectronicPackage;
import cmpt213.assignment4.packagedeliveries.client.model.PackageInfo;
import cmpt213.assignment4.packagedeliveries.client.model.PerishablePackage;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonSyntaxException;
import com.google.gson.TypeAdapter;
import com.google.gson.reflect.TypeToken;
import com.google.gson.stream.JsonReader;
import com.google.gson.stream.JsonWriter;
import java.io.IOException;
import java.lang.reflect.Type;
import java.time.LocalDateTime;
import java.util.ArrayList;
/**
* It's a class that contains a main method that creates a TextMenu object, loads a list of packages
* from a file, and then loops through a menu of options until the user chooses to quit
*/
public class PackageDeliveriesTracker {
private static final String fileName = "list.json";
private static final RuntimeTypeAdapterFactory<PackageInfo> r = RuntimeTypeAdapterFactory.of(PackageInfo.class, "type")
.registerSubtype(BookPackage.class, "book")
.registerSubtype(PerishablePackage.class, "perishable")
.registerSubtype(ElectronicPackage.class, "electronic");
private static final 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());
}
}).registerTypeAdapterFactory(r).create();
private static ArrayList<PackageInfo> packageList = new ArrayList<>();
private static PackageDeliveriesTracker instance;
public static PackageDeliveriesTracker getInstance() {
if (instance == null) {
instance = new PackageDeliveriesTracker();
}
return instance;
}
public String serializePackage(PackageInfo p) {
Type pType = new TypeToken<PackageInfo>() {
}.getType();
return gson.toJson(p);
}
public ArrayList<PackageInfo> deserializePackageString(String gsonString) {
try {
System.out.println(gsonString + "deserializePackageString gsonString");
Type pType = new TypeToken<ArrayList<PackageInfo>>() {
}.getType();
if (!(packageList == null)) packageList.clear();
packageList = gson.fromJson(gsonString, pType);
System.out.println(packageList + " --> packageList");
if (packageList != null) {
for (PackageInfo p : packageList) {
if (p instanceof BookPackage) p.setType("book");
else if (p instanceof PerishablePackage) p.setType("perishable");
else if (p instanceof ElectronicPackage) p.setType("electronic");
}
}
} catch (JsonSyntaxException e) {
e.printStackTrace();
}
return packageList;
}
}