import cs112java.CD;

// this is the true object oriented CDCollection version
// you can have several different collections at the same time since
// they will have their seperate instance variables (count and cds array)

public class CDCollection2 {
    
    CD[] cds;
    int count;
    
    /** Creates a new instance of CDCollection */
    public CDCollection2() {
        cds  = new CD[5];
        count = 0;
    }
    
    //for testing
    public static void main(String[] args) {
        CDCollection2 cc = new CDCollection2();
        CDCollection2 cc1 = new CDCollection2();
        
        cc.printCDs();

        for (int i = 0; i < 10; i++) {
            cc.addCD(new CD("title" + i, "artist" + i, i));
        }
        cc.printCDs();
        cc.deleteCDInOrder("title5");
        cc.printCDs();
        
        for (int i = 6; i < 12; i++) {
            cc1.addCD(new CD("title" + i, "artist" + i, i));
        }
        //cc1.printCDs();
    }

    public void addCD(CD cd) {
        if (count == cds.length) {
            cds = biggerArray(cds);
        }
        cds[count] = cd;
        count++;
    }
    
    public boolean deleteCD(String title) {
        int loc = findCD(title);
        if (loc == -1)
            return false;
        cds[loc] = cds[count-1];
        count--;
        return true;
    }

    public boolean deleteCDInOrder(String title) {
        int loc = findCD(title);
        if (loc == -1)
            return false;
        for (int i = loc+1; i < count; i++) {
            cds[i-1] = cds[i];
        }
        count--;
        return true;
    }

    
    private  void printCDs() {
        System.out.println("_________________________________");
        for (int i = 0; i < count; i++) {
            System.out.println("CD " + (i+1) + " " + cds[i]);
        }
        System.out.println("_________________________________");
    }

    private static CD[] biggerArray(CD[] original) {
        CD[] newArray = new CD[original.length * 2];
        for (int i = 0; i < original.length; i++) {
            newArray[i] = original[i];
        }
        return newArray;
    }

    private int findCD(String title) {
        int i = count - 1;
        while (i >= 0 && !cds[i].getTitle().equals(title))
            i--;
        return i;
    }
}

