I need to create a method for sorting my CD objects by the CD Title. I have no idea where to begin. I haven't yet covered any sorting algorithms so I need to know how I can sort this thing. I'm not asking for someone to write the code for me I just need some direction. Here's the code:
Code:
// CD.java
// Represents a compact disc object
class CD
{
private String title; // CD title (name of product)
private String number; // CD product number
private double numStock; // CD stock number
private double price; // price of CD
private double inventoryValue; //number of units in stock times price of each unit
// constructor initializes CD information
public CD( String cdTitle, String productNumber, double
numberInStock, double cdPrice )
{
title = cdTitle;
number = productNumber;
numStock = numberInStock;
price = cdPrice;
} // end constructor
public double getInventoryValue()
{
return numStock * price;
} //end getInventoryValue
public void displayInventory()
{
System.out.printf( "\n%s%35s\n%s%12s\n%s%9.2f\n%s%12s%.2f\n%s%5s%.2f\n \n" , "CD Title:", title, "Product Number:", number , "Number in Stock:", numStock , "CD Price:" , "$" , price , "Inventory Value:" , "$" , getInventoryValue() );
} // end method
public static double calculateTotalInventory( CD completeCDInventory[] )
{
double totalInventoryValue = 0;
for ( int count = 0; count < completeCDInventory.length; count++ )
{
totalInventoryValue += completeCDInventory[count].getInventoryValue();
} // end for
return totalInventoryValue;
} // end calculateTotalInventory
public static void displayTotalInventory( CD completeCDInventory[] )
{
System.out.printf( "\n%s\n" ,"Inventory of CDs :" );
for ( int count = 0; count < completeCDInventory.length; count++ )
{
System.out.printf( "%s%d", "Item# ", count + 1 );
completeCDInventory[count].displayInventory();
}// end for
}// end displayTotalInventory
} // end class CD
Code:
public class CDInventory
{
// executes application
public static void main( String args[] )
{
CD completeCDInventory[] = new CD[ 5 ]; // creates a new 5 element array
// populates array with objects that implement Inventory2
completeCDInventory[ 0 ] = new CD( "Sixpence None the Richer" , "D121401" , 12 , 11.99 );
completeCDInventory[ 1 ] = new CD( "Clear" , "D126413" , 10 , 10.99 );
completeCDInventory[ 2 ] = new CD( "NewsBoys: Love Liberty Disco" , "2438-51720-2" , 10 , 12.99 );
completeCDInventory[ 3 ] = new CD( "Skillet: Hey You, I Love Your Soul" , "D122966" , 9 , 9.99 );
completeCDInventory[ 4 ] = new CD( "Michael Sweet: Real" , "020831-1376-204" , 15 , 12.99 );
double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable
CD.displayTotalInventory( completeCDInventory ); //calls Inventory's display method
System.out.println("Total Inventory Value is: " + totalInventoryValue);
} // end main
} // end class
Comment