import java.text.DateFormat; import java.text.SimpleDateFormat; import java.time.Duration; import java.time.LocalDate; import java.time.LocalDateTime; import java.time.format.DateTimeFormatter; import java.time.temporal.ChronoUnit; import static java.lang.Math.abs; public class PackageInfo implements Comparable{ private String name; private String note; private double price; private double weight; private boolean delivered; private LocalDateTime expectedDate; public PackageInfo(String name, String note, double price, double weight, boolean delivered, LocalDateTime expectedDate) { this.name =name; this.note = note; this.price = price; this.weight =weight; this.delivered =delivered; this.expectedDate = expectedDate; } public String getName() { return name; } public boolean getDelivered() { return delivered; } public LocalDateTime getExpectedDate() { return expectedDate; } public void setDelivered(boolean delivered) { this.delivered = delivered; } @Override public int compareTo(PackageInfo p){ return this.expectedDate.compareTo(p.getExpectedDate()); } @Override public String toString() { DateTimeFormatter format = DateTimeFormatter.ofPattern("yyy-MM-dd HH:mm"); LocalDateTime today = LocalDateTime.now(); today.format(format); long diff = ChronoUnit.DAYS.between(today, expectedDate); String isDelivered = delivered ? "yes" : "no"; return "Name: " + name + "\n" + "Notes: " + note + "\n" + "Price: " + price + "\n" + "Weight: " + weight + "\n" + "Expected Delivery Date: " + expectedDate.toString() + "\n" + "Delivered? " + isDelivered + "\n" + ((diff > 0 && !delivered) ? diff + " days remaining" : abs(diff) + " days overdue"); } }