Songs Class:
public class Songs {
//-----------------------------------------------------------------
// Creates a SongCollection object and adds some songs to it. Prints
// reports on the status of the collection.
//-----------------------------------------------------------------
public static void main (String[] args)
{
SongCollection music = new SongCollection ();
music.addSong ("Storm Front", "Billy Joel", 14.95, 10);
music.addSong ("Come On Over", "Shania Twain", 14.95, 16);
music.addSong ("Soundtrack", "Les Miserables", 17.95, 33);
music.addSong ("Graceland", "Paul Simon", 13.90, 11);
System.out.println (music);
music.addSong ("Double Live", "Garth Brooks", 19.99, 26);
music.addSong ("Greatest Hits", "Jimmy Buffet", 15.95, 13);
System.out.println (music);
}
}
Song Collection Class:
import java.text.NumberFormat;
public class SongCollection {
private final int NUM_SONGS = 100;
private Song[] collection;
private int count;
private double totalCost;
//-----------------------------------------------------------------
// Constructor: Creates an initially empty collection.
//-----------------------------------------------------------------
public SongCollection ()
{
collection = new Song[NUM_SONGS];
count = 0;
totalCost = 0.0;
}
//-----------------------------------------------------------------
// Adds a song to the collection, increasing the size of the
// collection if necessary.
//-----------------------------------------------------------------
public void addSong (String title, String artist, double cost, int tracks)
{
if (count == collection.length)
increaseSize();
collection[count] = new Song (title, artist, cost, tracks);
totalCost += cost;
count++;
}
//-----------------------------------------------------------------
// Returns a report describing the CD collection.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n";
report += "My Song Collection\n\n";
report += "Number of songs: " + count + "\n";
report += "Total cost: " + fmt.format(totalCost) + "\n";
report += "Average cost: " + fmt.format(totalCost/count);
report += "\n\nSong List:\n\n";
for (int i = 0; i < count; i++) {
report += collection[i].toString() + "\n";
}
return report;
}
//-----------------------------------------------------------------
// Increases the capacity of the collection by creating a
// larger array and copying the existing collection into it.
//-----------------------------------------------------------------
private void increaseSize ()
{
Song[] temp = new Song[collection.length * 2];
for (int i = 0; i < collection.length; i++)
temp[i] = collection[i];
collection = temp;
}
}
Song Class:
import java.text.NumberFormat;
public class Song {
private String title, artist;
private double cost;
private int tracks;
//-----------------------------------------------------------------
// Creates a new Song with the specified information.
//-----------------------------------------------------------------
public Song (String name, String singer, double price, int numTracks)
{
title = name;
artist = singer;
cost = price;
tracks = numTracks;
}
//-----------------------------------------------------------------
// Returns a string description of this song.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String description;
description = fmt.format(cost) + "\t" + tracks + "\t";
description += title + "\t" + artist;
return description;
}
}
Output:
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
My Song Collection
Number of songs: 6
Total cost: $97.69
Average cost: $16.28
Song List:
$14.95 10 Storm Front Billy Joel
$14.95 16 Come On Over Shania Twain
$17.95 33 Soundtrack Les Miserables
$13.90 11 Graceland Paul Simon
$19.99 26 Double Live Garth Brooks
$15.95 13 Greatest Hits Jimmy Buffet
I think the formatting of the output could be better and maybe the code could be written in a more cohesive manner to improve readability.