package cs112java;

// this is the first version that uses static methods and static variables
// to manage the CD data
// the shortcoming is it can only handle one collection at a time, you can not have
// several different collections at the same time
public class CDCollection {
    
    static String[] artists;  
    static String[] titles;
    static int[] prices;
    static int count;
    

    public CDCollection() {
        artists = new String[5];
        titles  = new String[5];
        prices = new int[5];
        count = 0;
    }
    
    //for testing purposes
    public static void main(String[] args) {
        
        CDCollection.printCDs();

        for (int i = 0; i < 6; i++) {
            CDCollection.addCD("title" + i, "artist" + i, i);
        }
        CDCollection.printCDs();
        for (int i = 6; i < 12; i++) {
            CDCollection.addCD("title" + i, "artist" + i, i);
        }
        CDCollection.printCDs();
    }

    private static void addCD(String title, String artist, int price) {
        if (count == artists.length) {
            artists = biggerArray(artists);
            titles = biggerArray(titles);
            prices = biggerArray(prices);
        }
        artists[count] = artist;
        titles[count] = title;
        prices[count] = price;
        count++;
    }

    private static void printCDs() {
        System.out.println("_________________________________");
        System.out.println("This collection has the follwing CDs \n");
        for (int i = 0; i < count; i++) {
            System.out.println("CD " + (i+1) + " " + artists[i]
                    + titles[i] + " " + prices[i]);
        }
        System.out.println("_________________________________");
    }

    private static String[] biggerArray(String[] original) {
        String[] newArray = new String[original.length * 2];
        for (int i = 0; i < original.length; i++) {
            newArray[i] = original[i];
        }
        return newArray;
    }
  
    private static int[] biggerArray(int[] original) {
        int[] newArray = new int[original.length * 2];
        for (int i = 0; i < original.length; i++) {
            newArray[i] = original[i];
        }
        return newArray;
    }

}

