Advertisement
Guest User

medialistfrombill

a guest
Dec 5th, 2019
99
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.82 KB | None | 0 0
  1. import java.util.ArrayList;
  2. import java.util.Collections;
  3.  
  4. /**This class encapsulates a list of media items in a user's collection.
  5. * The list is implemented as either an ArrayList or array of type MediaItem.
  6. * Media items are either a book (electronic format), movie, podcast, or song.
  7. * Each type of media item is represented by an instance of the Book, Movie, Podcast, or Song class.
  8. * These classes are subclasses of MediaItem. The list stores media items as
  9. * references of type MediaItem.
  10. **/
  11. public class MediaList {
  12.  
  13. private ArrayList<MediaItem> itemList;
  14.  
  15. public MediaList(){
  16. itemList = new ArrayList<MediaItem>();
  17. }
  18.  
  19. public void addItem(MediaItem newItem){
  20. itemList.add(newItem);
  21. }
  22.  
  23. public boolean containsItem(String targetTitle, String targetAuthor){
  24. for (MediaItem target: itemList){
  25. if (target.getTitle().equalsIgnoreCase(targetTitle) && target.getAuthor().equalsIgnoreCase(targetAuthor))
  26. return true;
  27. }
  28. return false;
  29. }
  30.  
  31. public boolean removeItem(String targetTitle, String targetAuthor){
  32. for (MediaItem target: itemList){
  33. if (target.getTitle().equalsIgnoreCase(targetTitle) && target.getAuthor().equalsIgnoreCase(targetAuthor)){
  34. itemList.remove(target);
  35. return true;
  36. }
  37. }
  38. return false;
  39. }
  40.  
  41. public String[] getItemListAsStringArray(){
  42. if (isEmpty() == true){
  43. String[] mediaEmptyTest = new String[0];
  44. return mediaEmptyTest;
  45. }
  46. String[] mediaInfo = new String[itemList.size()];
  47. int count = 0;
  48. for (MediaItem targets: itemList){
  49. mediaInfo[count] = targets.toString();
  50. count++;
  51. }
  52. return mediaInfo;
  53. }
  54.  
  55. public String[] getAllItemsByTitle(String targetTitle){
  56. int titleCount = 0;
  57. ArrayList<MediaItem> titleList = new ArrayList<MediaItem>();
  58. for (MediaItem target: itemList){
  59. if(target.getTitle().equalsIgnoreCase(targetTitle)){
  60. titleList.add(target);
  61. }
  62. }
  63. String[] targetTitles = new String[titleList.size()];
  64. for (MediaItem targets : titleList){
  65. targetTitles[titleCount] = targets.toString();
  66. titleCount++;
  67. }
  68. return targetTitles;
  69. }
  70.  
  71. public String[] getAllItemsByAuthor(String targetAuthor){
  72. int authorCount = 0;
  73. ArrayList<MediaItem> authorList = new ArrayList<MediaItem>();
  74. for (MediaItem target: itemList){
  75. if(target.getAuthor().equalsIgnoreCase(targetAuthor)){
  76. authorList.add(target);
  77. }
  78. }
  79. String[] targetAuthors = new String[authorList.size()];
  80. for (MediaItem targets : authorList){
  81. targetAuthors[authorCount] = targets.toString();
  82. authorCount++;
  83. }
  84. return targetAuthors;
  85. }
  86.  
  87. public String[] getSortedListOfAuthors(){
  88. ArrayList<String> sortedList = new ArrayList<String>();
  89. for (MediaItem targets : itemList){
  90. sortedList.add(targets.getAuthor());
  91. }
  92. Collections.sort(sortedList);
  93. String[] sortedArray = new String[sortedList.size()];
  94. int counter = 0;
  95. for (String author: sortedList){
  96. sortedArray[counter] = author;
  97. counter++;
  98. }
  99. return sortedArray;
  100.  
  101. }
  102.  
  103. public int getNumItems(){
  104. return itemList.size();
  105. }
  106.  
  107. public boolean isEmpty(){
  108. return itemList.isEmpty();
  109. }
  110.  
  111. /****** Private, "helper" method section. You may define any private
  112. methods you want below. This is not a requirement of this project.
  113. These methods perform tasks that make the code above simler and easier to
  114. develop, troubleshoot, and understand.
  115. ******/
  116. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement