Guest User

Untitled

a guest
Jan 22nd, 2018
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.68 KB | None | 0 0
  1. package edu.hawaii.ics613;
  2.  
  3. import java.util.ArrayList;
  4.  
  5. public class CompactDiscCollection {
  6. public ArrayList<CompactDisc> Collection;
  7. public ArrayList<Integer> HashValues;
  8.  
  9. public CompactDiscCollection() {
  10. ArrayList<CompactDisc> collection = new ArrayList<CompactDisc>();
  11. Collection = collection;
  12. ArrayList<Integer> hashvalues = new ArrayList<Integer>();
  13. HashValues = hashvalues;
  14. }
  15.  
  16. public void addCompactDisc(CompactDisc disc) {
  17. if (!HashValues.contains(disc.hashCode())){
  18. Collection.add(disc);
  19. HashValues.add(disc.hashCode());
  20. } else
  21. System.out.println("Error: Compact Disc already in collection.");
  22. }
  23.  
  24. public CompactDisc findCompactDisc(String title, String artist) {
  25. ArrayList<String> tracks = new ArrayList<String>();
  26. CompactDisc cd = new CompactDisc(title,artist,tracks);
  27. if(HashValues.contains(cd.hashCode())) {
  28. return Collection.get(Collection.indexOf(cd));
  29. }
  30. else
  31. return null;
  32. }
  33.  
  34. public boolean hasCompactDisc(String title, String artist) {
  35. ArrayList<String> tracks = new ArrayList<String>();
  36. CompactDisc cd = new CompactDisc(title,artist,tracks);
  37. if(HashValues.contains(cd.hashCode())) {
  38. return true;
  39. }
  40. else
  41. return false;
  42. }
  43.  
  44. public void removeCompactDisc(String title, String artist) {
  45. ArrayList<String> tracks = new ArrayList<String>();
  46. CompactDisc cd = new CompactDisc(title,artist,tracks);
  47. int index = Collection.indexOf(cd);
  48. if (HashValues.contains(cd.hashCode())) {
  49. Collection.remove(index);
  50. HashValues.remove(index);
  51. } else
  52. System.out.println("Error: Compact Disc not in collection.");
  53. }
  54.  
  55. public ArrayList<CompactDisc> getCollection() {
  56. return Collection;
  57. }
  58.  
  59. public int size() {
  60. return Collection.size();
  61. }
  62.  
  63. }
Add Comment
Please, Sign In to add comment