//******************************************************************** // CDCollection.java Author: Lewis and Loftus // // Rappresenta una collezione di CD. //******************************************************************** import CD; import java.text.NumberFormat; public class CDCollection { private CD[] collection; private int count; private double totalValue; private int currentSize; //----------------------------------------------------------------- // Crea una collezione di 100 elementi inizialmente vuota // mediante un array di oggetti CD. //----------------------------------------------------------------- public CDCollection () { currentSize = 100; collection = new CD[currentSize]; count = 0; totalValue = 0.0; } //----------------------------------------------------------------- // Aggiunge un CD alla collezione, increamenta la dimensione della // collezione se necessario. //----------------------------------------------------------------- public void addCD (String title, String artist, double value, int tracks) { if (count == currentSize) increaseSize(); collection[count] = new CD (title, artist, value, tracks); totalValue += value; count++; } //----------------------------------------------------------------- // Effettua il rapporto sulla intera collezione, // e richiama il metodo toString sul singolo CD. //----------------------------------------------------------------- public String toString() { NumberFormat fmt = NumberFormat.getCurrencyInstance(); String report = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n"; report += "La mia collezione di CD\n\n"; report += "Numero di CD: " + count + "\n"; report += "Valore totale: " + fmt.format(totalValue) + "\n"; report += "Costo medio: " + fmt.format(totalValue/count); report += "\n\nCD Lista:\n\n"; for (int cd = 0; cd < count; cd++) report += collection[cd].toString() + "\n"; return report; } //----------------------------------------------------------------- // Raddoppia la dimensione della collezione creando un array pił // grande e copiando i dati correnti nel nuovo array. //----------------------------------------------------------------- private void increaseSize () { currentSize *= 2; CD[] temp = new CD[currentSize]; for (int cd = 0; cd < collection.length; cd++) temp[cd] = collection[cd]; collection = temp; } }