package cmpt213.assignment2.packagedeliveriestracker.textui; import cmpt213.assignment2.packagedeliveriestracker.model.PackageFactory; import cmpt213.assignment2.packagedeliveriestracker.model.PackageInfo; import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.format.DateTimeParseException; import java.util.*; /** * It's a class that displays a menu and allows the user to interact with the program */ public class TextMenu { private final String title = "Package Tracker"; private final String[] options = new String[]{ "List of all packages", "Add a package", "remove a package", "list overdue packages", "List upcoming packages", "mark package as delivered", "exit"}; /** * This function displays the title of the menu, the current date, and the options available to the * user */ public void display() { int tag = title.length(); for (int i = 0; i <= tag + 3; i++) { System.out.print("#"); } System.out.println("\n# " + title + " #"); for (int i = 0; i <= tag + 3; i++) { System.out.print("#"); } DateFormat today = new SimpleDateFormat("yyy-MM-dd"); Calendar cal = Calendar.getInstance(); System.out.println("\nToday is: " + today.format(cal.getTime())); for (int i = 0; i < options.length; i++) { System.out.println((i + 1) + ": " + options[i]); } } /** * This function takes an ArrayList of PackageInfo objects and prints them out in reverse order * * @param packageList The list of packages to be displayed */ public void list(ArrayList packageList) { if (packageList.size() == 0) { System.out.println("No packages to show"); } else { Collections.sort(packageList); for (int i = 0; i < packageList.size(); i++) { System.out.println("Package #" + (i + 1)); System.out.println(packageList.get(i) + "\n"); } } } /** * This function allows the user to add a package to the list of packages * * @param packageList the list of packages */ public void add(ArrayList packageList) { Scanner scan = new Scanner(System.in); int itemType; do { System.out.println("please select (1) book, (2) perishable, or (3) electronic package"); itemType = Integer.parseInt(scan.nextLine()); } while (itemType < 1 || itemType > 3); PackageFactory.PackageType pType = PackageFactory.PackageType.Book; String author = ""; LocalDateTime expiryDate = LocalDateTime.now(); double handlingFee = 0.0; String pName; do { System.out.println("enter a valid package name:"); pName = scan.nextLine(); } while (pName.length() == 0); System.out.println("notes:"); String pNotes = scan.nextLine(); boolean checkDate = false; DateTimeFormatter format = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm"); LocalDateTime pDate = LocalDateTime.now(); while (checkDate == false) { try { System.out.println("enter date as yyyy-mm-dd hh:mm:"); pDate = LocalDateTime.parse(scan.nextLine(), format); checkDate = true; } catch (DateTimeParseException e) { System.out.println("invalid date format"); } } double pPrice; do { System.out.println("enter price:"); pPrice = Double.parseDouble(scan.nextLine()); } while (pPrice < 0); double pWeight; do { System.out.println("enter weight:"); pWeight = Double.parseDouble(scan.nextLine()); } while (pWeight <= 0); switch (itemType) { case 1: do { System.out.println("enter author"); author = scan.nextLine(); } while (author.length() == 0); break; case 2: boolean checkExpDate = false; while (checkExpDate == false) { try { System.out.println("enter expiry date as yyyy-mm-dd hh:mm"); expiryDate = LocalDateTime.parse(scan.nextLine(), format); checkExpDate = true; } catch (DateTimeParseException e) { System.out.println("invalid date format"); } } pType = PackageFactory.PackageType.Perishable; break; case 3: do { System.out.println("enter handling fee"); handlingFee = Double.parseDouble(scan.nextLine()); } while (handlingFee <= 0); pType = PackageFactory.PackageType.Electronic; break; } PackageInfo p = PackageFactory.create(pType, pName, pNotes, pPrice, pWeight, false, pDate, author, expiryDate, handlingFee); packageList.add(p); System.out.println(pName + " has been added to the list!"); } /** * This function takes in an ArrayList of PackageInfo objects and prints out the list of packages * in the ArrayList. The user is then prompted to enter the number of the package they want to * remove. If the user enters a number that is not in the list, the user is prompted to enter a * number again. If the user enters 0, the function returns without removing anything. If the user * enters a number that is in the list, the package is removed from the list and the user is * notified * * @param packageList the list of packages to be displayed */ public void remove(ArrayList packageList) { this.list(packageList); if (packageList.size() == 0) { return; } Scanner scan = new Scanner(System.in); int n; do { System.out.println("enter item number you want to remove (0 to cancel):"); n = scan.nextInt(); } while (n < 0 || n > packageList.size()); if (n > 0) { System.out.println(packageList.get(n - 1).getName() + " has been removed from the list."); packageList.remove(n - 1); } } /** * 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 sortList(ArrayList pList, boolean due) { ArrayList 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; } /** * This function takes in a list of packages and returns a list of packages that are overdue * * @param packageList the list of packages to be sorted */ public void overDueList(ArrayList packageList) { ArrayList overdue = sortList(packageList, true); if (overdue.size() == 0) { System.out.println("no overdue packages to show"); return; } this.list(overdue); } /** * This function takes in a list of packages and prints out the packages that are upcoming * * @param packageList the list of packages to be sorted */ public void upcomingList(ArrayList packageList) { ArrayList upcoming = sortList(packageList, false); if (upcoming.size() == 0) { System.out.println("no upcoming packages to show"); return; } this.list(upcoming); } /** * This function takes a list of packages and returns a list of packages that have not been * delivered * * @param packageList The list of packages to be filtered * @return An ArrayList of PackageInfo objects that have not been delivered. */ public ArrayList getUndelivered(List packageList) { ArrayList undelivered = new ArrayList<>(); for (int i = 0; i < packageList.size(); i++) { if (packageList.get(i).getDelivered() == false) { undelivered.add(packageList.get(i)); } } return undelivered; } /** * This function takes a list of packages and prints out the undelivered packages, then asks the * user to select a package to mark as delivered * * @param packageList the list of packages to be marked as delivered */ public void markDelivered(ArrayList packageList) { ArrayList undelivered = getUndelivered(packageList); if (undelivered.size() == 0) { System.out.println("No undelivered packages to show"); return; } this.list(undelivered); Scanner scan = new Scanner(System.in); int n; do { System.out.println("enter item number you want to mark delivered (0 to cancel):"); n = scan.nextInt(); } while (n < 0 || n > undelivered.size()); if (n > 0) { PackageInfo p = undelivered.get(n - 1); packageList.get(packageList.indexOf(p)).setDelivered(true); System.out.println(p.getName() + " has been delivered."); } } }