Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.text.NumberFormat;
- import java.io.*;
- import java.util.*;
- //Modified by Brian Carey
- public class DVDCollection
- {
- private DVD[] collection;
- private int count;
- private double totalCost;
- public DVDCollection()
- {
- collection = new DVD[100];
- count = 0;
- totalCost = 0.0;
- }
- public void addDVD(String title, String director, int year, double cost, boolean bluray)
- {
- int i=0;
- if (count == collection.length)
- increaseSize();
- DVD d=new DVD(title, director, year, cost, bluray);
- while(i<count && director.compareToIgnoreCase(collection[i].director) > 0)
- {
- i++;
- }
- for(int j=count-1; j>=i;j--)
- {
- collection[j+1]=collection[j];
- }
- collection[i] = d;
- totalCost += cost;
- count++;
- }
- public String toString()
- {
- NumberFormat fmt = NumberFormat.getCurrencyInstance();
- String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
- report += "My DVD Collection\n\n";
- report += "Number of DVDs: " + count + "\n";
- report += "Total cost: " + fmt.format(totalCost) + "\n";
- report += "Average cost: " + fmt.format(totalCost/count);
- report += "\n\nDVD List:\n\n";
- for (int dvd = 0; dvd < count; dvd++)
- report += collection[dvd].toString() + "\n";
- return report;
- }
- private void increaseSize()
- {
- DVD[] temp = new DVD[collection.length * 2];
- for (int dvd = 0; dvd < collection.length; dvd++)
- temp[dvd] = collection[dvd];
- collection = temp;
- }
- public int searchForDVD(String director)
- {
- int min = 0, max = count - 1, mid = 0;
- while(min <= max)
- {
- mid = (min + max) / 2;
- if (collection[mid].getDirector().equals(director))
- return mid;
- else
- if (director.compareTo(collection[mid].getDirector()) < 0)
- max = mid - 1;
- else
- min = mid + 1;
- }
- return -1;
- }
- public String getDVD(int pos)
- {
- return collection[pos].toString();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement