Welcome to Dream.In.Code
Become a Java Expert!

Join 149,516 Java Programmers for FREE! Get instant access to thousands of Java experts, tutorials, code snippets, and more! There are 1,368 people online right now. Registration is fast and FREE... Join Now!




creating a GUI to display an array of objects

 
Reply to this topicStart new topic

creating a GUI to display an array of objects

sammyboy78
28 Jun, 2007 - 06:41 PM
Post #1

New D.I.C Head
*

Joined: 28 Jun, 2007
Posts: 1


My Contributions
I need to create a simple GUI window that displays my inventory of compact disc (CD) objects
Here's an example using DVDs( I only need one of the two types of display, the easiest one!):
IPB Image
For my current code I have a CD class that represents a CD object, a CD2 subclass of CD I also have a CDInventory class that creates the CD objects and executes the methods in the CD class (driver class?). I have a hint about how to proceed from my instructor: "The CD and CD2 classes don't need to be modified, just the CDInventory class to include the GUI". In addition to the above mentioned classes I've created an additional class, GUICDInventory, to try and get the GUI to work but I keep getting this error:
C:\Documents and Settings\Sam\GUICDInventory.java:22: '.class' expected
cDJList = new JList( Object[] gUICDInventory );
^
C:\Documents and Settings\Sam\GUICDInventory.java:22: ';' expected
cDJList = new JList( Object[] gUICDInventory );
^
2 errors

I'm not sure why I'm getting the error or even if I'm headed in the right direction here...I'll work through it with anyone that wants to help me.

Here's my code:

CODE
// GUICDInventory.java
// uses CD class
import java.awt.FlowLayout;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.SwingConstants;
import javax.swing.JList;
import javax.swing.JScrollPane;

public class GUICDInventory extends JFrame
{

    CDInventory gUICDInventory = new CDInventory();

    private JList cDJList;

    public GUICDInventory()
    {
        super( "Compact Disc Inventory" );
        setLayout( new FlowLayout() );

        cDJList = new JList( Object[]  gUICDInventory );
        cDJList.setVisibleRowCount( 35 );

        add( new JScrollPane( cDJList ) );

    }

    // executes application
    public static void main( String args[] )
    {
        GUICDInventory guICDInventory = new GUICDInventory();
        guICDInventory.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        guICDInventory.setSize( 500, 800 );
        guICDInventory.setVisible( true );


    } // end main

} // end class GUICDInventory


CODE
// CDInventory.java
// uses CD class

public class CDInventory
{

    // executes application
    public static void main( String args[])
    {

        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 );

        double totalInventoryValue = CD.calculateTotalInventory( completeCDInventory ); //declares totalInventoryValue variable

        CD.displayTotalInventory( completeCDInventory ); //calls CD's display method

        System.out.println("Total Inventory Value is: " + totalInventoryValue);

        CD.sortedCDInventory( completeCDInventory ); // calls CD's sortedCDInventory method

        System.out.println("Total Inventory Value is: " + totalInventoryValue);

    } // end main

} // end class CDInventory


CODE
// CD2.java
// subclass of CD

public class CD2 extends CD
{
    protected int copyrightDate; // CDs copyright date variable declaration
    private double 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

    public double getInventoryValue() // modified subclass method to add restocking fee
    {
        price2 = price + price * 0.05;
        return numStock * price2;

    } //end getInventoryValue

    public void displayInventory() // modified subclass display method
        {

            System.out.printf( "\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



CODE
// CD.java
// Represents a compact disc object
import java.util.Arrays;

class CD implements Comparable
{
    protected String title; // CD title (name of product)
    protected String prodNumber; // CD product number
    protected double numStock; // CD stock number
    protected double price; // price of CD
    protected double 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

    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%.2f\n%s%5s%.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

    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 (unsorted):" );

        for ( int count = 0; count < completeCDInventory.length; count++ )
        {
            System.out.printf( "%s%d", "Item# ", count + 1 );

            completeCDInventory[count].displayInventory();

        }// end for

    }// end displayTotalInventory

    public int compareTo( Object obj ) //overlaod compareTo method
    {
        CD tmp = ( CD )obj;

        if( this.title.compareTo( tmp.title ) < 0 )
        {
            return -1; //instance lt received
        }
        else if( this.title.compareTo( tmp.title ) > 0 )
        {
            return 1; //instance gt received
        }

        return 0; //instance == received

        }// end compareTo method

    public static void sortedCDInventory( CD completeCDInventory[] )
    {
           System.out.printf( "\n%s\n" ,"Inventory of CDs (sorted by title):" );

           Arrays.sort( completeCDInventory ); // sort array

           for( int count = 0; count < completeCDInventory.length; count++ )
           {
                System.out.printf( "%s%d", "Item# ", count + 1 );

                   completeCDInventory[count].displayInventory();}
           }


} // end class CD


This post has been edited by sammyboy78: 28 Jun, 2007 - 06:45 PM
User is offlineProfile CardPM
+Quote Post

Fast ReplyReply to this topicStart new topic
Time is now: 1/7/09 08:09PM

Be Social

Dream.In.Code RSS Feed Dream.In.Code LinkedIn Group Follow Us On Twitter

Live Java Help!

Java Tutorials

Reference Sheets

Java Snippets

DIC Chatroom

Bye Bye Ads

Monthly Drawing

Thumb Drive

Top Contributors

Top 10 Kudos This Month