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.
228 lines
8.2 KiB
228 lines
8.2 KiB
package cmpt213.assignment4.packagedeliveries.webappserver.control;
|
|
|
|
import cmpt213.assignment4.packagedeliveries.webappserver.gson.extras.RuntimeTypeAdapterFactory;
|
|
import cmpt213.assignment4.packagedeliveries.webappserver.model.BookPackage;
|
|
import cmpt213.assignment4.packagedeliveries.webappserver.model.ElectronicPackage;
|
|
import cmpt213.assignment4.packagedeliveries.webappserver.model.PackageInfo;
|
|
import cmpt213.assignment4.packagedeliveries.webappserver.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.*;
|
|
import java.lang.reflect.Type;
|
|
import java.nio.file.Files;
|
|
import java.nio.file.NoSuchFileException;
|
|
import java.nio.file.Paths;
|
|
import java.time.LocalDateTime;
|
|
import java.time.format.DateTimeFormatter;
|
|
import java.util.ArrayList;
|
|
import java.util.Collections;
|
|
import java.util.Iterator;
|
|
|
|
/**
|
|
* 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 PackageManager {
|
|
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 PackageManager instance;
|
|
|
|
public static PackageManager getInstance() {
|
|
if (instance == null) {
|
|
instance = new PackageManager();
|
|
instance.load();
|
|
}
|
|
return instance;
|
|
}
|
|
|
|
public void addPackage(PackageInfo p) {
|
|
packageList.add(p);
|
|
Collections.sort(packageList);
|
|
}
|
|
|
|
/**
|
|
* 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 (NoSuchFileException e) {
|
|
createFile();
|
|
//throw new RuntimeException(e);
|
|
} catch (IOException e) {
|
|
throw new RuntimeException(e);
|
|
}
|
|
}
|
|
|
|
private void createFile() {
|
|
System.out.println("CreateFile");
|
|
try {
|
|
File packages = new File(fileName);
|
|
if(packages.createNewFile()) {
|
|
System.out.println("New file list.json created");
|
|
}
|
|
}
|
|
catch (IOException e) {
|
|
System.out.println("No file exists, creating a new file");
|
|
e.printStackTrace();
|
|
}
|
|
}
|
|
|
|
public String getAllPackages() {
|
|
Collections.sort(packageList);
|
|
return gson.toJson(packageList);
|
|
}
|
|
|
|
/**
|
|
* This function takes in a list of packages, a boolean for whether or not the user wants to see
|
|
* packages that are due, and a boolean for whether or not the user wants to see all packages. It
|
|
* then returns a list of packages that are sorted by their expected delivery date
|
|
*
|
|
* @param pList the list of packages to be sorted
|
|
* @param due true if you want to sort the list by due date, false if you want to sort the list by
|
|
* expected date
|
|
* @return An ArrayList of PackageInfo objects.
|
|
*/
|
|
//due=true returns overdue packages, else upcoming packages
|
|
public ArrayList<PackageInfo> sortList(ArrayList<PackageInfo> pList, boolean due) {
|
|
ArrayList<PackageInfo> sortedList = new ArrayList<>();
|
|
LocalDateTime today = LocalDateTime.now();
|
|
DateTimeFormatter format = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm");
|
|
today.format(format);
|
|
for (int i = 0; i < pList.size(); i++) {
|
|
PackageInfo p = pList.get(i);
|
|
if (!p.getDelivered()) {
|
|
if (due && today.isAfter(p.getExpectedDate())) {
|
|
sortedList.add(p);
|
|
} else if (!due && today.isBefore(p.getExpectedDate())) {
|
|
sortedList.add(p);
|
|
}
|
|
}
|
|
}
|
|
Collections.sort(sortedList);
|
|
return sortedList;
|
|
}
|
|
|
|
public String overDuePackages() {
|
|
ArrayList<PackageInfo> overdue = sortList(packageList, true);
|
|
return gson.toJson(overdue);
|
|
}
|
|
|
|
public String upcomingPackages() {
|
|
ArrayList<PackageInfo> upcoming = sortList(packageList, false);
|
|
// System.out.println(upcoming + " --> upcoming packages");
|
|
return gson.toJson(upcoming);
|
|
}
|
|
|
|
public void removePackage(PackageInfo p) {
|
|
Iterator<PackageInfo> itr = packageList.iterator();
|
|
int index = 0;
|
|
while (itr.hasNext()) {
|
|
PackageInfo pkg = itr.next();
|
|
if (pkg.toString().hashCode() == p.toString().hashCode()) {
|
|
break;
|
|
}
|
|
index += 1;
|
|
}
|
|
packageList.remove(index);
|
|
}
|
|
|
|
public boolean isDelivered(int i) {
|
|
return packageList.get(i).getDelivered();
|
|
}
|
|
|
|
public void setDelivered(PackageInfo p) {
|
|
Iterator<PackageInfo> itr = packageList.iterator();
|
|
int index = 0;
|
|
while (itr.hasNext()) {
|
|
PackageInfo pkg = itr.next();
|
|
if (pkg.toString().hashCode() == p.toString().hashCode()) {
|
|
break;
|
|
}
|
|
index += 1;
|
|
}
|
|
packageList.get(index).setDelivered(true);
|
|
}
|
|
|
|
public PackageInfo deserializePackage(String packageGson){
|
|
PackageInfo newPackage = null;
|
|
try {
|
|
System.out.println(packageGson + "-------> packageGson");
|
|
Type lType = new TypeToken<PackageInfo>() {
|
|
}.getType();
|
|
newPackage = gson.fromJson(packageGson, lType);
|
|
System.out.println(newPackage + " newPackage");
|
|
|
|
//for (PackageInfo p : packageList) {
|
|
if (newPackage instanceof BookPackage) {
|
|
newPackage.setType("book");
|
|
} else if (newPackage instanceof PerishablePackage) {
|
|
newPackage.setType("perishable");
|
|
} else if (newPackage instanceof ElectronicPackage) {
|
|
newPackage.setType("electronic");
|
|
}
|
|
//}
|
|
|
|
} catch (JsonSyntaxException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return newPackage;
|
|
}
|
|
}
|
|
|