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.

86 lines
2.7 KiB

package cmpt213.assignment3.packagedeliveries.model;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.time.temporal.ChronoUnit;
import static java.lang.Math.abs;
/**
* PackageInfo is a class that contains information about a package
*/
public class PackageInfo implements Comparable<PackageInfo> {
private final String name;
private final String note;
private final double price;
private final double weight;
private final LocalDateTime expectedDate;
private boolean delivered;
private String type;
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;
}
/**
* This function returns the name of the person.
*
* @return The name of the person.
*/
public String getName() {
return name;
}
public boolean getDelivered() {
return delivered;
}
/**
* This function sets the value of the delivered variable to the value of the delivered parameter.
*
* @param delivered This is a boolean value that indicates whether the message has been delivered
* to the recipient.
*/
public void setDelivered(boolean delivered) {
this.delivered = delivered;
}
public LocalDateTime getExpectedDate() {
return expectedDate;
}
/**
* This function sets the type of the object to the type passed in as a parameter
*
* @param type The type of the event.
*/
public void setType(String type) {
this.type = type;
}
// Comparing the expected date of the package to the expected date of the package passed in as a
// parameter.
@Override
public int compareTo(PackageInfo p) {
return this.expectedDate.compareTo(p.getExpectedDate());
}
// Overriding the toString method.
@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: " + DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm").format(expectedDate) + "\n" + "Delivered? " + isDelivered + "\n" + ((diff > 0 && !delivered) ? diff + " days remaining" : abs(diff) + " days overdue");
}
}