public class MediaLibrary { // TODO: change /** * The current items in the media library. */ private MediaItem[] items = new MediaItem[0]; /** * The maximum number of items enforced by the media library. */ private int maxItems; /** * The maximum duration enforced by the media library. */ private int maxDuration; /** * Creates a media library that can support the specified * number of items with the specified total * maximum duration. * * @param maxItems The maximum number of items that * the media library can handle. * @param maxDuration The total maximum duration that * the current items may have. */ public MediaLibrary(int maxItems, int maxDuration) { this.maxItems = maxItems; this.maxDuration = maxDuration; } /** * Determines whether an item is in the media library. * * @param item The item to check. * @return True if it is in the media library, false otherwise. */ public boolean containsItem(MediaItem item) { for (int i = 0; i < items.length; i++) { if (items[i] == item) { return true; } } return false; } /** * Adds a non-null item that is not in the media library to * the media library unless there is no more space. * * @param item The item to add. * @return True if the item has been added, false if * the item is null or if there is no more space or * if the item is already in the media library. */ public boolean addItem(MediaItem item) { // TODO: add more checks // check invalid states if (item == null) { // Cannot add null item, sorry return false; } if (items.length == maxItems) { // Cannot add more items to the media library return false; } // all checks ok, add the item MediaItem[] newItems = new MediaItem[items.length + 1]; for (int j = 0; j < items.length; j++) { newItems[j] = items[j]; } newItems[items.length] = item; items = newItems; return true; } /** * Recursively searches an item with a specified name in * the item array (from startIndex to 0) * and if present, returns the highest index of such an item. * If no such item exists, the method returns -1. * * @param name The name of the item to search. * @param startIndex The index to start from (0 <= startIndex < items.length). * @return If found, the index of the last item in the array when * starting to search from startIndex to 0. * If no such item exists, the method returns -1. */ public int lastIndexOf(String title, int startIndex) { // TODO: implement return -42; } // Other helper methods, not needed for this task. /** * Removes an item from the media libraty and returns if something * has actually been removed. * * @param item The item to remove. * @return True if the item was removed, false if the item was * not in the media library. */ public boolean removeItem(MediaItem item) { // search for the item in the media library for (int i = 0; i < items.length; i++) { if (items[i] == item) { // if found, create a copy of the items without it MediaItem[] newItems = new MediaItem[items.length - 1]; for (int j = 0; j < newItems.length; j++) { if (j < i) { newItems[j] = items[i]; } else { newItems[j] = items[i + 1]; } } // and replace the old items with the new ones items = newItems; return true; } } return false; } @Override public String toString() { String result = ""; for (int i = 0; i < items.length; i++) { result += items[i].toString() + "\n"; } if (this instanceof Lasting) { result += "Duration: " + ((Lasting) this).getDuration() + " s"; } return result; } }