Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.ArrayList;
- import java.util.Collections;
- /**This class encapsulates a list of media items in a user's collection.
- * The list is implemented as either an ArrayList or array of type MediaItem.
- * Media items are either a book (electronic format), movie, podcast, or song.
- * Each type of media item is represented by an instance of the Book, Movie, Podcast, or Song class.
- * These classes are subclasses of MediaItem. The list stores media items as
- * references of type MediaItem.
- **/
- public class MediaList {
- private ArrayList<MediaItem> itemList;
- public MediaList(){
- itemList = new ArrayList<MediaItem>();
- }
- public void addItem(MediaItem newItem){
- itemList.add(newItem);
- }
- public boolean containsItem(String targetTitle, String targetAuthor){
- for (MediaItem target: itemList){
- if (target.getTitle().equalsIgnoreCase(targetTitle) && target.getAuthor().equalsIgnoreCase(targetAuthor))
- return true;
- }
- return false;
- }
- public boolean removeItem(String targetTitle, String targetAuthor){
- for (MediaItem target: itemList){
- if (target.getTitle().equalsIgnoreCase(targetTitle) && target.getAuthor().equalsIgnoreCase(targetAuthor)){
- itemList.remove(target);
- return true;
- }
- }
- return false;
- }
- public String[] getItemListAsStringArray(){
- if (isEmpty() == true){
- String[] mediaEmptyTest = new String[0];
- return mediaEmptyTest;
- }
- String[] mediaInfo = new String[itemList.size()];
- int count = 0;
- for (MediaItem targets: itemList){
- mediaInfo[count] = targets.toString();
- count++;
- }
- return mediaInfo;
- }
- public String[] getAllItemsByTitle(String targetTitle){
- int titleCount = 0;
- ArrayList<MediaItem> titleList = new ArrayList<MediaItem>();
- for (MediaItem target: itemList){
- if(target.getTitle().equalsIgnoreCase(targetTitle)){
- titleList.add(target);
- }
- }
- String[] targetTitles = new String[titleList.size()];
- for (MediaItem targets : titleList){
- targetTitles[titleCount] = targets.toString();
- titleCount++;
- }
- return targetTitles;
- }
- public String[] getAllItemsByAuthor(String targetAuthor){
- int authorCount = 0;
- ArrayList<MediaItem> authorList = new ArrayList<MediaItem>();
- for (MediaItem target: itemList){
- if(target.getAuthor().equalsIgnoreCase(targetAuthor)){
- authorList.add(target);
- }
- }
- String[] targetAuthors = new String[authorList.size()];
- for (MediaItem targets : authorList){
- targetAuthors[authorCount] = targets.toString();
- authorCount++;
- }
- return targetAuthors;
- }
- public String[] getSortedListOfAuthors(){
- ArrayList<String> sortedList = new ArrayList<String>();
- for (MediaItem targets : itemList){
- sortedList.add(targets.getAuthor());
- }
- Collections.sort(sortedList);
- String[] sortedArray = new String[sortedList.size()];
- int counter = 0;
- for (String author: sortedList){
- sortedArray[counter] = author;
- counter++;
- }
- return sortedArray;
- }
- public int getNumItems(){
- return itemList.size();
- }
- public boolean isEmpty(){
- return itemList.isEmpty();
- }
- /****** Private, "helper" method section. You may define any private
- methods you want below. This is not a requirement of this project.
- These methods perform tasks that make the code above simler and easier to
- develop, troubleshoot, and understand.
- ******/
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement