package photomanager; /** * The Photo class represents a photo. * * A photo has a source (URL or path to the file), * * the width and height in pixels, * * the date the photo was taken, * * * comments the user has added (if any). * * If you look at the class definition, * * * after "public class Photo" you will see "implements Comparable<Photo>". * * You can ignore it; it means that the Photo class must implement a method called * compareTo that takes a Photo as a parameter and returns an integer. * * Why do we need it? * * Because it will allow us to sort ArrayList of Photos using the compareTo method that you will define. * */ public class Photo implements Comparable { private String photoSource; private int width, height; private String date; private StringBuffer comments; //++++++++++++++++++++++++++++ CONSTRUCTORS /** * DONE Initializes the instance variables with the provided parameter values and * DONE Initializes comments with a StringBuffer object. * * DONE It will throw an IllegalArgumentException with the message * "Constructor: Invalid arguments" * if any string parameter is null, * or blank, * or if the width or height are negative or zero. * * DONE Use the String class isBlank() method to verify whether a string is blank. * * DONE Assume that if a date is provided, it represents a valid date. * * DONE Don't verify whether photoSource represents a photo that exists. * * @param photoSource URL or file location. * @param width Photo's width in pixels. * @param height Photo's height in pixels. * @param date Date the photo was taken. * @throws IllegalArgumentException For invalid parameter. */ public Photo(String photoSource, int width, int height, String date) { if (photoSource == null || photoSource.isBlank() || date == null || date.isBlank() || width <= 0 || height <= 0) { throw new IllegalArgumentException("Constructor: Invalid arguments"); } this.comments = new StringBuffer(); this.photoSource = photoSource; this.width = width; this.height = height; this.date = date; } /** * Copy constructor. * * Modifications to the new object should not affect the original object. * * @param photo To copy. */ public Photo(Photo photo) { this(photo.photoSource, photo.width, photo.height, photo.date); this.comments = new StringBuffer(photo.comments); } // ++++++++++++++++++++++++++++ METHODS /** * Returns a string with photoSource, width, height and date values separated * by commas. * * Comments are not part of the string. * * @return String with values separated by commas. */ public String toString() { char separatorComma = ','; String stringCreated = photoSource + separatorComma + width + separatorComma + height + separatorComma + date; return stringCreated; } /** * Get method for photoSource. * @return photoSource. */ public String getPhotoSource() { return photoSource; } /** * Get method for width. * @return width. */ public int getWidth() { return width; } /** * Get method for height. * @return height. */ public int getHeight() { return height; } /** * Get method for date. * @return date. */ public String getDate() { return date; } /** * DONE Appends the newComment parameter to the comments StringBuffer. * * DONE Comments must be separated by commas. * * DONE The last comment should not be followed by a comma. * * DONE If the parameter is null or blank according to isBlank() * * DONE no comment will be appended, and IllegalArgumentException * * * DONE with the message "Invalid comment" will be thrown. * * DONE The method returns a reference to the current object. * * * @param newComment Comment to add. * @return Reference to the current object. */ public Photo addComments(String newComment) { if (newComment.isBlank() || newComment == null) { throw new IllegalArgumentException("Invalid comment"); } else { if (comments.length() != 0) { comments.append(','); } comments.append(newComment); } return this; } /** * Returns a string that corresponds to the comments. * * @return String corresponding to comments. */ public String getComments() { return comments.toString(); } /** * This method will compare the date of the current object * against the parameter's object. * * * Use the Utilities.getDate() method that takes a string as a parameter * and returns an integer (of type long) representing the date. * * We can represent dates including time using integers. * * The number returned by Utilities.getDate() * represents seconds since the Unix epoch (00:00:00 UTC on 1 January 1970). * * The compareTo method will return * * ||a negative value if the date of the current object * ||precedes the date of the parameter, * * zero if the dates are the same, * * a positive value otherwise. * * Don't be surprise if the amount of code required for compareTo method * is minimal. * * Do not using casting to convert a long to int. * * I know that date is a string. * */ public int compareTo(Photo photo) { if ( Utilities.getDate(this.date) < Utilities.getDate(photo.date) ) { return -1; } else if ( Utilities.getDate(this.date) == Utilities.getDate(photo.date) ) { return 0; } else { return 1; } } }