here is the full code...there are 2 class files that i am using....
public class Assn1{
private CD[] myCDs;
private Date release;
private static int counter = 0;
private static boolean go = true;
public static void main(String[] args)
{
while(go == true)
{
new Assn1();
}
}
//-------------------------------------------------------------------------------
// Procedure: Assn1
//
// Summary: main driver method for program
//-------------------------------------------------------------------------------
public Assn1()
{
//initialize data members
myCDs = new CD[5]; //provide a dimension for the array
for(int i = 0; i < myCDs.length; i++)
{
//main menu for navigating program
Object[] possibleValues = { "Make New Entry",
"View Entries",
"Clear Entires",
"Oldest Entries First",
"Newest Entries First",
"Exit"};
Object selectedValue = JOptionPane.showInputDialog(null,
"Please select one of the following:",
"Input",
JOptionPane.INFORMATION_MESSAGE,
null,
possibleValues,
possibleValues[0]);
//user selected make a new entry
if(selectedValue.equals(possibleValues[0]))
{
makeNew();
}
//view all entries in the CD array
if(selectedValue.equals(possibleValues[1]))
{
viewAll();
}
//clear all entries in the CD array
if(selectedValue.equals(possibleValues[2]))
{
clearAll();
}
//sort by oldest first and
//show the entries in that order
if(selectedValue.equals(possibleValues[3]))
{
JOptionPane.showMessageDialog(null,"Entries have been sorted by Oldest to Newest");
sortByOldest();
}
//sort by newest first and
//show entriest in that order
if(selectedValue.equals(possibleValues[4]))
{
JOptionPane.showMessageDialog(null,"Entries have been sorted by Newest to Oldest");
sortByNewest();
}
//user selected exit the program....
//make sure they really want to exit
if(selectedValue.equals(possibleValues[5]))
{
exitProgram();
}
}
}
//-------------------------------------------------------------------------------
// Procedure: sortByNewest
//
// Summary: sorts array values newest to oldest
//-------------------------------------------------------------------------------
public void sortByNewest()
{
if(counter > 1)
{
CD extra = new CD();
for(int outer = 0; outer < counter - 1; outer++)
{
for(int inner = 0; inner < counter - 1; inner++)
{
if( myCDs[inner].getreleaseDate().before(myCDs[inner + 1].getreleaseDate()))
{
//this part i got help from robert
extra = myCDs[inner];
myCDs[inner] = myCDs[inner + 1];
myCDs[inner + 1] = extra;
}
}
}
}
}
//-------------------------------------------------------------------------------
// Procedure: sortByOldest
//
// Summary: sorts array values oldest to newest
//-------------------------------------------------------------------------------
public void sortByOldest()
{
if(counter > 1)
{
CD extra = new CD();
for(int outer = 0; outer < counter - 1; outer++)
{
for(int inner = 0; inner < counter - 1; inner++)
{
if( myCDs[inner].getreleaseDate().after(myCDs[inner + 1].getreleaseDate()))
{
//this part i got help from robert
extra = myCDs[inner] ;
myCDs[inner] = myCDs[inner + 1];
myCDs[inner + 1] = extra;
}
}
}
}
}
//-------------------------------------------------------------------------------
// Procedure: makeNew
//
// Summary: creates a new entry in CD array
//-------------------------------------------------------------------------------
public void makeNew()
{
if(counter <=4)
{
try
{
//prompt user for entries
String title = JOptionPane.showInputDialog("Enter name of cd");
String genre = JOptionPane.showInputDialog("Enter type of music");
String label = JOptionPane.showInputDialog("Enter label the artist is on");
String artist = JOptionPane.showInputDialog("Enter artist name");
String day = JOptionPane.showInputDialog("Enter day of release");
int d = Integer.parseInt(day);
String month = JOptionPane.showInputDialog("Enter month of release");
int m = Integer.parseInt(month);
String year = JOptionPane.showInputDialog("Enter year of release");
int y = Integer.parseInt(year);
myCDs[counter] = new CD(artist, title, d, m, y, genre, label );
if(myCDs[counter] != null){counter++;}
}
catch(Exception Ex)
{
JOptionPane.showMessageDialog(null, "Invalid input entered");
}
}
else JOptionPane.showMessageDialog(null, "The CD array is full, you cannot add more CD's");
}
//-------------------------------------------------------------------------------
// Procedure: viewAll
//
// Summary: displays all current entries in array
//-------------------------------------------------------------------------------
public void viewAll()
{
String output = "";
for(int count = 0; count < myCDs.length; count++)
{
if(myCDs[count] != null)
output += myCDs[count].Display() + "\n";
}
JOptionPane.showMessageDialog(null, output, "All cds in list", JOptionPane.INFORMATION_MESSAGE);
}
//-------------------------------------------------------------------------------
// Procedure: clearAll
//
// Summary: clears all entries in the array
//-------------------------------------------------------------------------------
public void clearAll()
{
Object[] option = { "Yes", "No" };
Object decision = JOptionPane.showInputDialog(null,
"Clear All Entries in CD Collection",
"Input",
JOptionPane.INFORMATION_MESSAGE,
null,
option,
option[0]);
if(decision.equals(option[0]))
{
for(int j = 0; j < myCDs.length; j++)
{
if(myCDs[j] != null)
{
myCDs[j] = null;
}
}
}
}
//-------------------------------------------------------------------------------
// Procedure: exitProgram
//
// Summary: provides means for user to exit program
//-------------------------------------------------------------------------------
public void exitProgram()
{
Object[] option = { "Yes",
"No"
};
Object decision = JOptionPane.showInputDialog(null,
"Do you really want to exit?",
"Input",
JOptionPane.INFORMATION_MESSAGE,
null,
option,
option[0]);
if(decision.equals(option[0]))
{
go = false;
System.exit( 0 );
}
}
}//end class Assn1