Need more help with a GUI
It's the ever popular Inventory program again! I'm creating a GUI to display the information contained within an array of objects which, in this case, represent compact discs. I've received some good help from other's posts on this project since it seems there's a few of us working on the same one but now, since each person working on this project has programmed theirs differently, I'm stuck. I'm not sure how to proceed with my ActionListeners for the buttons I've created.
Here's my code:
// GUICDInventory.java
// uses CD class
import java.awt.*;
import javax.swing.*;
import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
publicclass GUICDInventoryextends JFrame
{
protected JPanel panel;//panel to hold buttons
protected JPanel cdImage;// panel to hold image
int displayElement = 0;
public String display(int element)
{
return CD2[element].toString();
}//end method
publicstaticvoid main( String args[] )
{
new GUICDInventory();
}// end main
public GUICDInventory()
{
CD completeCDInventory[] =new CD2[ 5 ];// creates a new 5 element array
// populates array with objects that implement CD
completeCDInventory[ 0 ] =new CD2("Sixpence None the Richer" ,"D121401" , 12 , 11.99, 1990 );
completeCDInventory[ 1 ] =new CD2("Clear" ,"D126413" , 10 , 10.99, 1998 );
completeCDInventory[ 2 ] =new CD2("NewsBoys: Love Liberty Disco" ,"2438-51720-2" , 10 , 12.99, 1999 );
completeCDInventory[ 3 ] =new CD2("Skillet: Hey You, I Love Your Soul" ,"D122966" , 9 , 9.99, 1998 );
completeCDInventory[ 4 ] =new CD2("Michael Sweet: Real" ,"020831-1376-204" , 15 , 12.99, 1995 );
//declares totalInventoryValue variable
double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory );
final JTextArea textArea =new JTextArea(display(displayElement));
final JButton prevBtn =new JButton("Previous");
final JButton nextBtn =new JButton("Next");
final JButton lastBtn =new JButton("Last");
final JButton firstBtn =new JButton("First");
prevBtn.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent ae)
{
displayElement = (//what goe here? ) % //what goes here?
textArea.setText(display(displayElement));// <--is this right?
}
});
nextBtn.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent ae)
{
displayElement = (//what goes here? ) % //what goes here?
textArea.setText(display(displayElement));// <--is this right?
}
});
firstBtn.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent ae)
{
displayElement = 0;
textArea.setText(display(displayElement));// <--is this right?
}
});
lastBtn.addActionListener(new ActionListener()
{
publicvoid actionPerformed(ActionEvent ae)
{
displayElement =//what goes here?;
textArea.setText(display(displayElement));// <--is this right?
}
});
JPanel panel =new JPanel(new GridLayout(1,2));
panel.add(firstBtn); panel.add(nextBtn); panel.add(prevBtn); panel.add(lastBtn);
JPanel cdImage =new JPanel(new BorderLayout());
cdImage.setPreferredSize(new Dimension(600,400));
JFrame cdFrame =new JFrame();
cdFrame.getContentPane().add(new JScrollPane(textArea),BorderLayout.CENTER);
cdFrame.getContentPane().add(panel,BorderLayout.SOUTH);
cdFrame.getContentPane().add(new JLabel("",new ImageIcon("cd.gif"),JLabel.CENTER),BorderLayout.NORTH);
cdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
cdFrame.pack();
cdFrame.setSize(500,200);
cdFrame.setTitle("Compact Disc Inventory");
cdFrame.setLocationRelativeTo(null);
cdFrame.setVisible(true);
}//end GUICDInventory constructor
}// end class GUICDInventory
// CD2.java
// subclass of CD
publicclass CD2extends CD
{
protectedint copyrightDate;// CDs copyright date variable declaration
privatedouble price2;
// constructor
public CD2( String title, String prodNumber,double numStock,double price,int copyrightDate )
{
// explicit call to superclass CD constructor
super( title, prodNumber, numStock, price );
this.copyrightDate = copyrightDate;
}// end constructor
publicdouble getInventoryValue()// modified subclass method to add restocking fee
{
price2 = price + price * 0.05;
return numStock * price2;
}//end getInventoryValue
// Returns a formated String contains the information about any particular item of inventory
public String displayInventory()// modified subclass display method
{
return String.format("\n%-22s%s\n%-22s%d\n%-22s%s\n%-22s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n \n" ,
"CD Title:", title,"Copyright Date:", copyrightDate,"Product Number:", prodNumber ,"Number in Stock:",
numStock ,"CD Price:" ,"$" , price ,"Restocking fee (5%):","$", price*0.05,"Inventory Value:" ,"$" ,
getInventoryValue() );
}// end method
}//end class CD2
// CD.java
// Represents a compact disc object
import java.util.Arrays;
class CDimplements Comparable
{
protected String title;// CD title (name of product)
protected String prodNumber;// CD product number
protecteddouble numStock;// CD stock number
protecteddouble price;// price of CD
protecteddouble inventoryValue;//number of units in stock times price of each unit
// constructor initializes CD information
public CD( String title, String prodNumber,double numStock,double price )
{
this.title = title;// Artist: album name
this.prodNumber = prodNumber;//product number
this.numStock = numStock;// number of CDs in stock
this.price = price;//price per CD
}// end constructor
publicdouble getInventoryValue()
{
return numStock * price;
}//end getInventoryValue
//Returns a formated String contains the information about any particular item of inventory
public String displayInventory()
{
//return the formated String containing the complete information about CD
return String.format("\n%-22s%s\n%-22s%s\n%-22s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n%-22s%s%.2f\n \n" ,
"CD Title:", title,"Product Number:", prodNumber ,"Number in Stock:",
numStock ,"CD Price:" ,"$" , price ,"Restocking fee (5%):","$", price*0.05,"Inventory Value:" ,"$" ,
getInventoryValue() );
}// end method
//method to calculate the total inventory of the array of objects
publicstaticdouble calculateTotalInventory( CD completeCDInventory[] )
{
double totalInventoryValue = 0;
for (int count = 0; count < completeCDInventory.length; count++ )
{
totalInventoryValue += completeCDInventory[count].getInventoryValue();
}// end for
return totalInventoryValue;
}// end calculateTotalInventory
// Method to return the String containing the Information about Inventory's Item
//as appear in array (non-sorted)
publicstatic String displayTotalInventory( CD completeCDInventory[] )
{
//string which is to be returned from the method containing the complete inventory's information
String retInfo ="\nInventory of CDs (unsorted):\n";
//loop to go through complete array
for (int count = 0; count < completeCDInventory.length; count++ )
{
retInfo = retInfo +"Item# " + (count + 1);//add the item number in String
retInfo = retInfo + completeCDInventory[count].displayInventory();//add the inventory detail in String
}// end for
return retInfo;//return the String containing complete detail of Inventory
}// end displayTotalInventory
publicint compareTo( Object obj )//overlaod compareTo method
{
CD tmp = ( CD )obj;
if( this.title.compareTo( tmp.title ) < 0 )
{
return -1;//instance lt received
}
elseif( this.title.compareTo( tmp.title ) > 0 )
{
return 1;//instance gt received
}
return 0;//instance == received
}// end compareTo method
//Method to return the String containing the Information about Inventory's Item
// in sorted order (sorted by title)
publicstatic String sortedCDInventory( CD completeCDInventory[] )
{
//string which is to be returned from the method containing the complete inventory's information
String retInfo ="\nInventory of CDs (sorted by title):\n";
Arrays.sort( completeCDInventory );// sort array
//loop to go through complete array
for(int count = 0; count < completeCDInventory.length; count++ )
{
retInfo = retInfo +"Item# " + (count + 1);//add the item number in String
retInfo = retInfo + completeCDInventory[count].displayInventory();//add the inventory detail in String
}
return retInfo;//return the String containing complete detail of Inventory
}// end method sortedCDInventory
}// end class CD

