Can anyone assist me with the GUI for this program. I have tried several times will little luck. Here is the source code. Any help would be greatly appreciated.

import java.text.DecimalFormat;
import javax.swing.JFrame;

public class Inventory1  
{

   
    public static void main(String[] args) 
    {
        
        // Information on the Cds used
        Cd a = new Cd( 1, "10 Years", 6, 9.49, "10 Years");
        Cd b =  new Cd( 2, "System of a Down", 12, 9.99, "Hypnotize");
        Cd c = new Cd( 3, "Trapt", 45, 11.47, "Trapt");
        Cd d = new Cd( 4, "Social Distortion", 8, 7.99, "S L and R&R");
        Cd e = new Cd( 5, "Seether", 15, 11.99, "Seether" );
        
        // Adds the Cds to the inventory class
        Inventory inventory = new Inventory();
        inventory.add(a);
        inventory.add(b);
        inventory.add(c);
        inventory.add(d);
        inventory.add(e);
        
        // Sorts the Cds
        inventory.bubbleSort();
        
        // Formats the Cd table
       // System.out.println("---+----------------------+------"
	//			+ "+----------+------------+------------+----------------+");

		

		
      //  System.out.println(String.format( "%-2s | %-15s | %2s | %7s |%7s | %10s | %10s", "#", "Name", "Qty", "Price", "Total", "Cd Title", "Restocking Fee"));
        
      //  System.out.println("---+----------------------+------"
	//			+ "+----------+------------+------------+----------------+");
       
            
        String label1;
        // Creates a for loop to print out the Cds
        for( int i = 0; i < inventory.getCount(); i++ ) {
            
            GUI gui = new GUI();
            gui.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            gui.setSize(275, 180);
            gui.setVisible( true );
            
          
            Product product = inventory.get(i);
            System.out.println( product );
            add(label1);
            
        } // End For loop
        
        DecimalFormat format = new DecimalFormat("$#,##0.00");
        String value = format.format(inventory.value() );
        System.out.println( "\nThe entire inventory is worth: " + value); 
    } // End main
} // End Class Inventory 1
class Product {
    
    private String CD1;
    private int stock1;
    private double cost1;
    private int invent1;
    
    
    
    // Four-argument constructor
    public Product ( int invent, String CD, int stock, double cost )
    {
        invent1 = invent;
        CD1 = CD;
        stock1 = stock;
        cost1 = cost;
        
        
    } // End four-argument Product constructor
   
    // Set Inventory Number
    public void setInvent1( int invent ) 
    { 
        invent1 = invent; // Should validate
    } // End method SetInvent
    
    //Return Inventory Number
    public int getInvent1() 
    {
        return invent1;
    } // End method getInvent1
    
    // Set CD name
    public void setCD1( String CD )
    {
        CD1 = CD; // Should validate
        
        
    } // End method setCD
    
    // Return CD name
    public String getCD1()
    {
        return CD1;
    } // End method getCD
    
    // Set the number of CDs in stock
    public void setStock1( int stock )
    {
        stock1 = stock; // Should validate
    } // End method setStock
    
    // Return number of CDs in stock
    public int getStock1()
    {
        return stock1;
    } // End method getStock
    
    // Sets the cost for the CD
    public void setCost1( double cost )
    {
        cost1 = cost; // Should validate
    } // End method setCost
    
    // Return cost for CD
    public double getCost1()
    {
        return cost1;
    } // End method getCost1
    
    // Determines what the total value of the CDs in the inventory
    public double total()
    {
        return cost1 * stock1;
    } // End method total
public String toString() 
    {
 
        return String.format("%-2s %-20s %4d %6.2f %8.2f ", invent1, CD1, stock1, cost1, total());
    }
} // End Class Product
public class Inventory {
    private Product cds[];
    private int count;
    
    
    public Inventory() {
        cds = new Product[150]; // creates an array for cds
        count = 0;  // creates counter      
    } // End Inventory constructor
    
    public void add(Product a) {
        cds[count++] = a;
    } 
    
    public Product get(int index) {
        return cds[index];
    }
    
    public void bubbleSort() {
        int b = getCount();
        for( int sort = 1; sort < b; sort++ ) {
            for( int i = 0; i < b - sort; i++ ) {
                String name1 = cds[i].getCD1();
                String name2 = cds[i + 1].getCD1();
                if (name1.compareToIgnoreCase(name2) > 0) {
                    Product temp = cds[i];
                    cds[i] = cds[i + 1];
                    cds[i + 1] = temp;
                } // End if 
            } // end if 
        } // end for loop
    } // end sort
    
    public double value() {
        double value = 0.00;
        for( int i = 0; i < count; i++ ) {
            value += cds[i].total();
        }
        return value;       
    }
    
    public int getCount() {
        return count;
    } 
} //end class inventory
public GUI() 
    {
      
        
     super ("CDs Inventory");
     setLayout( new FlowLayout() ); 
     
     label = new JLabel ( "This shows the inventory of CDs");
     add(label );
     
     Inventory1 cdgui = new Inventory1();
     label = new JLabel ( cdgui.toString());
     add(label);
     
        
    // button = new JButton("Previous");
   //  add(button);
    }
}

Recommended Answers

All 3 Replies

don't know what you've tried, but looks to me like your conversion would be a lot easier if you splitted all logic that's into your main method into methods.

the only thing that has to be in your main method, is a call to the gui (even if that is just a cmd prompt gui with a few lines saying what options you have).

start of with that, I might have some time later today to help you any further.

A main difference between using console I/O with a user and a GUI is that the program doesn't ask a question and wait for the response in the same way.
With GUI the program shows a screen with the question and some input areas and buttons that the user can fill in and press a button. The program shows this screen and then exits to the JVM to wait on the user's input. When the user enters data and presses a button, the JVM calls a listener method in the program where the code can gather the data that was entered, do some processing, build and show a new GUI and then return to the JVM to wait for the user to respond.

Thanks for the advice I will work off create methods and try this again. I will repost if I have any other questions.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.