2009-12-09 18:55:18 UTC
public class Statistics
{
private Statistic[] data;
private int count;
private double totalValue;
private int currentSize;
//-----------------------------------------------------------------
// Creates an initially empty data.
//-----------------------------------------------------------------
public Statistics()
{
currentSize = 50;
data = new Statistics[currentSize];
count = 0;
totalValue = 0.0;
}
//-----------------------------------------------------------------
// Adds a Statistics to the data, increasing the size of the
// data if necessary.
//-----------------------------------------------------------------
public void addStatistic (String title, String artist, double value,
int tracks)
{
if (count == currentSize)
increaseSize();
data[count] = new Statistic (title, artist, value, tracks);
totalValue += value;
count++;
}
//-----------------------------------------------------------------
// Adds a Statistics to the collection, increasing the size of the
// collection if necessary.
//-----------------------------------------------------------------
public void addStatistic (Statistic acd)
{
if (count == currentSize)
increaseSize();
data[count] = acd;
totalValue +=acd.getValue();
count++;
}
//-----------------------------------------------------------------
// find a Statistic in the data, if found, returns an index to the Statistic,
// if not found in the data return -1
//-----------------------------------------------------------------
public int find (Statistic acd)
{
int index;
boolean found = false;
for (index = 0; index
if( data[index].equals(acd) )
found = true;
}
if(found)
return (index-1);
else
return -1;
}
//_________________________________
// retrieve a Statistic from the data
// return a Statistic
//____________________________________
public Statistic retrieve(int index)
{
return data[index];
}
public Statistic retrieve(Statistic acd)
{
Statistic someStatistic = null;
int index = find(acd);
if (index >-1)
someStatistic = data[index];
return someStatistic;
}
// delete
//precondition: index has to be grater then -1
public String delete ( int index)
{
if ( index >-1)
{
for ( int i = index; i
count --;
return "removed";
}
else
return "index is negative - did not remove";
}// end delete
public String delete (Statistic acd)
{
int index = find(acd);
if ( index >-1)
{
for ( int i = index; i
count --;
return "removed";
}
else
return "index is negative - did not remove";
}// end delete
// insert a Statistic after index
// post condition index has to be greater then -1, and acd is not null
public String insert (Statistic acd, int index)
{
String isdone;
if( index >-1)
{
// shift the data
for ( int i = count - 1 ; i> index ; i--)
data[i+1] = data[i];
data[index+1] =acd;
isdone = " a Statistic is inserted";
}
else
isdone = " a Statistic is not inserted";
return isdone;
}//end insert
//-----------------------------------------------------------------
// Returns a report describing the Statistics data.
//-----------------------------------------------------------------
public String toString()
{
NumberFormat fmt = NumberFormat.getCurrencyInstance();
String report = "&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&&\n";
report += "My Statistics Collection\n\n";
report += "Number of Statistic's: " + count + "\n";
report += "Total value: " + fmt.format(totalValue) + "\n";
report += "Average cost: " + fmt.format(totalValue/count);
report += "\n\nCD List:\n\n";
for (int Statistic = 0; statistic < count; statistic++)
report += data[statistic].toString() + "\n";
return report;
}
//-----------------------------------------------------------------
// Doubles the size of the data by creating a larger array
// and copying into it the existing collection.
//-----------------------------------------------------------------
private void increaseSize ()
{
currentSize *= 2;
Statistic[] temp = new Statistic[currentSize];
for (int statistic = 0; statistic < data.length; statistic++)
temp[statistic] = data[statistic];
data = temp;
}
}
errors look like this(only a few examples).......................................
Statistics.java.java:3: class Statistics is public, should be declared in