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.
114 lines
4.2 KiB
114 lines
4.2 KiB
package cmpt213.assignment3.packagedeliveries.control;
|
|
|
|
import cmpt213.assignment3.packagedeliveries.gson.extras.RuntimeTypeAdapterFactory;
|
|
import cmpt213.assignment3.packagedeliveries.model.BookPackage;
|
|
import cmpt213.assignment3.packagedeliveries.model.ElectronicPackage;
|
|
import cmpt213.assignment3.packagedeliveries.model.PackageInfo;
|
|
import cmpt213.assignment3.packagedeliveries.model.PerishablePackage;
|
|
import com.google.gson.Gson;
|
|
import com.google.gson.GsonBuilder;
|
|
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.*;
|
|
import java.lang.reflect.Type;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.Paths;
|
|
import java.time.LocalDateTime;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Scanner;
|
|
|
|
/**
|
|
* 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;
|
|
}
|
|
|
|
/**
|
|
* It saves the packageList to a file.
|
|
*/
|
|
public void save() {
|
|
try {
|
|
Writer w = new FileWriter(fileName);
|
|
gson.toJson(packageList, w);
|
|
w.flush();
|
|
w.close();
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* It reads the json file, converts it to a list of PackageInfo objects, and then sets the type of
|
|
* each object to the appropriate type
|
|
*/
|
|
public void load() {
|
|
File file = new File(fileName);
|
|
try {
|
|
String json = Files.readString(Paths.get(fileName));
|
|
Type lType = new TypeToken<ArrayList<PackageInfo>>() {
|
|
}.getType();
|
|
packageList = gson.fromJson(json, lType);
|
|
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");
|
|
}
|
|
}
|
|
System.out.println("packages loaded");
|
|
} catch (FileNotFoundException e) {
|
|
System.out.println("no packages to load");
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
public String getAllPackages(){
|
|
StringBuilder b= new StringBuilder();
|
|
if (packageList.size() == 0) {
|
|
return("No packages to show");
|
|
} else {
|
|
Collections.sort(packageList);
|
|
for (int i = 0; i < packageList.size(); i++) {
|
|
String bString=("Package #" + (i + 1)) + "\n" + packageList.get(i) + "\n";
|
|
b.append(bString);
|
|
}
|
|
}
|
|
return b.toString();
|
|
}
|
|
}
|
|
|