Advertisement
Guest User

Untitled

a guest
Mar 21st, 2019
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.13 KB | None | 0 0
  1. import java.text.NumberFormat;
  2. import java.io.*;
  3. import java.util.*;
  4.  
  5. //Modified by Brian Carey
  6. public class DVDCollection
  7. {
  8. private DVD[] collection;
  9. private int count;
  10. private double totalCost;
  11.  
  12. public DVDCollection()
  13. {
  14. collection = new DVD[100];
  15. count = 0;
  16. totalCost = 0.0;
  17. }
  18. public void addDVD(String title, String director, int year, double cost, boolean bluray)
  19. {
  20. int i=0;
  21. if (count == collection.length)
  22. increaseSize();
  23.  
  24. DVD d=new DVD(title, director, year, cost, bluray);
  25.  
  26. while(i<count && director.compareToIgnoreCase(collection[i].director) > 0)
  27. {
  28. i++;
  29.  
  30. }
  31. for(int j=count-1; j>=i;j--)
  32. {
  33. collection[j+1]=collection[j];
  34. }
  35. collection[i] = d;
  36. totalCost += cost;
  37. count++;
  38. }
  39. public String toString()
  40. {
  41. NumberFormat fmt = NumberFormat.getCurrencyInstance();
  42.  
  43. String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
  44. report += "My DVD Collection\n\n";
  45.  
  46. report += "Number of DVDs: " + count + "\n";
  47. report += "Total cost: " + fmt.format(totalCost) + "\n";
  48. report += "Average cost: " + fmt.format(totalCost/count);
  49.  
  50. report += "\n\nDVD List:\n\n";
  51.  
  52. for (int dvd = 0; dvd < count; dvd++)
  53. report += collection[dvd].toString() + "\n";
  54.  
  55. return report;
  56. }
  57. private void increaseSize()
  58. {
  59. DVD[] temp = new DVD[collection.length * 2];
  60.  
  61. for (int dvd = 0; dvd < collection.length; dvd++)
  62. temp[dvd] = collection[dvd];
  63.  
  64. collection = temp;
  65. }
  66. public int searchForDVD(String director)
  67. {
  68. int min = 0, max = count - 1, mid = 0;
  69.  
  70. while(min <= max)
  71. {
  72. mid = (min + max) / 2;
  73. if (collection[mid].getDirector().equals(director))
  74. return mid;
  75. else
  76. if (director.compareTo(collection[mid].getDirector()) < 0)
  77. max = mid - 1;
  78. else
  79. min = mid + 1;
  80. }
  81.  
  82. return -1;
  83. }
  84. public String getDVD(int pos)
  85. {
  86. return collection[pos].toString();
  87. }
  88. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement