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.

138 lines
5.4 KiB

package cmpt213.assignment2.packagedeliveriestracker.textui;
import cmpt213.assignment2.packagedeliveriestracker.gson.extras.RuntimeTypeAdapterFactory;
import cmpt213.assignment2.packagedeliveriestracker.model.BookPackage;
import cmpt213.assignment2.packagedeliveriestracker.model.ElectronicPackage;
import cmpt213.assignment2.packagedeliveriestracker.model.PackageInfo;
import cmpt213.assignment2.packagedeliveriestracker.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.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<>();
/**
* It saves the packageList to a file.
*/
private static 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
*/
private static 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);
}
}
/**
* This function is the main function of the program. It creates a TextMenu object, loads the
* packages from the file, displays the menu, and then waits for the user to input a number between
* 1 and 7. If the user inputs a number outside of that range, the program will display an error
* message. If the user inputs 7, the program will save the packages to the file and exit. If the
* user inputs a number between 1 and 6, the program will call the corresponding function in the
* TextMenu class
*/
public static void main(String[] args) {
TextMenu menu = new TextMenu();
load();
do {
menu.display();
System.out.println("choose an option between 1 and 7:");
Scanner scan = new Scanner(System.in);
int option = Integer.parseInt(scan.nextLine());
if (option > 7 || option < 1) {
System.out.println("invalid input");
} else if (option == 7) {
save();
System.out.print("packages saved");
break;
} else {
switch (option) {
case 1:
menu.list(packageList);
break;
case 2:
menu.add(packageList);
break;
case 3:
menu.remove(packageList);
break;
case 4:
menu.overDueList(packageList);
break;
case 5:
menu.upcomingList(packageList);
break;
case 6:
menu.markDelivered(packageList);
}
}
}
while (true);
}
}