The program has no errors and when ran will show the logo, buttons but not the inventory data. I have been working on figuring out why it will not display the cd information with no luck. Could use some fresh eyes to look at this and help me figure out what I may have done wrong. Any help would be greatly appreciated. The program is below:

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import javax.swing.*;


public final class Inventory1 extends JFrame  
{
    private JButton previousJButton;
    private JButton nextJButton;
    private JButton firstJButton;
    private JButton lastJButton;
    private JButton saveJButton;
    private JButton addJButton;
    private JButton deleteJButton;
    private JButton modifyJButton;
    private JTextArea display;
    protected int currentProduct;
    
    private static ArrayList<Cd> inventory = new ArrayList<>();
    
    public Inventory1() 
    {
        super("Cd's Inventory" );
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        
        Inventory2();
        
        // Set up main panel for cd information
        JPanel mainpanel = new JPanel();
        setContentPane(mainpanel);
        mainpanel.setLayout(new BoxLayout(mainpanel, BoxLayout.Y_AXIS));
        
        
        display = new JTextArea(10,60);
        display.setEditable(false);
        mainpanel.add(display);
        
        // Set up buttons
        JPanel buttons = new JPanel();
        buttons.setLayout(new BoxLayout( buttons, BoxLayout.X_AXIS));
        
        firstJButton = new JButton( "First" ); // Creates button "first"
        currentProduct = 0;
        firstJButton.addActionListener( new ActionListener() 
                {
            @Override
                    public void actionPerformed( ActionEvent event ) 
                    {
                       Inventory1.this.currentProduct = 0;
                       Inventory1.this.cdinvent();
                    }
                }
                );
        add( firstJButton ); // adds first to JFrame
        
        previousJButton = new JButton ( "Previous" ); // Creates button "previous"
        currentProduct = 0;
        firstJButton.addActionListener ( new ActionListener() 
        {
            @Override
            public void actionPerformed ( ActionEvent event ) 
            {
                Inventory1.this.currentProduct--;
                
                if ( Inventory1.this.currentProduct < 0 ) Inventory1.this.currentProduct = Inventory1.inventory.size()-1;
                Inventory1.this.cdinvent();
            }
        }
                );
        add( previousJButton ); //adds previous to JFrame
        
        nextJButton = new JButton( "Next" ); // Creates button "next"
        currentProduct = 0;
        nextJButton.addActionListener( new ActionListener() 
        {
            @Override
            public void actionPerformed( ActionEvent event ) 
            {
                Inventory1.this.currentProduct++;
                if ( Inventory1.this.currentProduct >= Inventory1.inventory.size()) Inventory1.this.currentProduct = 0;
                Inventory1.this.cdinvent();
            }
        }
                );
        add( nextJButton ); // adds next to JFrame
        
        lastJButton = new JButton( "Last" ); // Creates button "last"
        currentProduct = 0;
        lastJButton.addActionListener( new ActionListener() 
        {
            @Override
            public void actionPerformed( ActionEvent event ) 
            {
                Inventory1.this.currentProduct = Inventory1.inventory.size()-1;
                Inventory1.this.cdinvent();
            }
        }
                );
       add( lastJButton ); // adds last to JFrame
       
       saveJButton = new JButton ( "Save" ); // Creates button "save"
       saveJButton.addActionListener( new ActionListener() 
       {
           @Override
           public void actionPerformed( ActionEvent event ) 
           {
               save();
           }
       }
               );
       add( saveJButton );
       
       addJButton = new JButton ( "Add Cd" ); // Creates button "Add Cd"
       addJButton.addActionListener( new ActionListener()  
       {
           @Override
           public void actionPerformed ( ActionEvent event ) 
           {
               String Artist = JOptionPane.showInputDialog(Inventory1.this, "What is the Artist for the CD?");
               int Stock = Integer.parseInt(JOptionPane.showInputDialog( Inventory1.this, "Number of CD's?"));
               double Cost = Double.parseDouble(JOptionPane.showInputDialog(Inventory1.this, "What is the cost of the CD?"));
               String Title = JOptionPane.showInputDialog(Inventory1.this, "What is the title of the CD?");
               
               while (Artist.length() < 100)
               {
                   Artist += " ";
               } 
               int num = 0;
               
               for ( int a = 0; a < inventory.size(); a++ ) 
               {
                   if ( inventory.get(a).getInvent1() > num)
                        num = inventory.get(a).getInvent1();
               }
               
               Cd cd = new Cd ( num + 1, Artist, Stock, Cost, Title);
               inventory.add(cd);
               currentProduct = inventory.size()-1;
               
               cdinvent();
           }
       }
               );
       add( addJButton );
       
       modifyJButton = new JButton ( "Modify Cd" );
       modifyJButton.addActionListener( new ActionListener() 
       {
           @Override
           public void actionPerformed ( ActionEvent event ) 
           {
               Product p = inventory.get(currentProduct);
               p.setInvent1(Integer.parseInt(JOptionPane.showInputDialog(Inventory1.this, "What is the product number?", p.getInvent1())));
               p.setCD1(JOptionPane.showInputDialog(Inventory1.this, "What is the artist for the CD?", p.getCD1()));
               p.setStock1(Integer.parseInt(JOptionPane.showInputDialog(Inventory1.this, "How many CDs are in stock?", p.getStock1())));
               p.setCost1(Double.parseDouble(JOptionPane.showInputDialog(Inventory1.this, "What is the cost of the CD?", p.getCost1())));
               
               cdinvent();
           }
       }
               );
       add( modifyJButton );
       
       deleteJButton = new JButton ( "Delete Cd");
       deleteJButton.addActionListener( new ActionListener() 
       {
           @Override
           public void actionPerformed (ActionEvent event ) 
           {
              try 
              {
                 inventory.remove(currentProduct);
              }
              catch ( Exception except) 
              {
                 currentProduct--;
                 if ( currentProduct < 0 ) currentProduct = 0;
                 
                 cdinvent();
              }
           }
       }
               );
       add( deleteJButton );
       
       mainpanel.add(buttons);
       GUI logo = new GUI();
       mainpanel.add(logo);
       cdinvent();
    }
    
    private void Inventory2()
    {
         // 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 array class
        inventory.add(a);
        inventory.add(b);
        inventory.add(c);
        inventory.add(d);
        inventory.add(e);
        
        // Sorts the Cds 
       inventory = bubbleSort( inventory );
       
    }
     public void cdinvent()
    {
        try 
        {
            cdinvent(inventory.get( currentProduct ), display);
        }
        catch (Exception except) 
        {
            cdinvent( null, display);
        }
    }
     private void cdinvent(Product p, JTextArea j) 
     {
        j.setText("");
        if (p == null) return;
        
        j.setText("Product Number\tArtist\tUnits in Stock\tPrice\tRestock Fee\tValue\tAlbum\n");
        j.setText(String.format("%03d\t%s\t%03d\t$%.2f\t$%.2f\t$%.2f\t%s\n", 
                p.getInvent1(),p.getCD1(),p.getCost1(),p.getStock1(), p.total()));
   
        j.append(String.format("Total value of inventory: $%.2f\n", value()));
        j.setVisible(true);
        display = j;
     }
      public static double value() {
        double value = 0.00;
        for( int i = 0; i < inventory.size(); i++ ) {
            value += inventory.get(i).total();
        }
        return value;       
    }
    private ArrayList<Cd> bubbleSort(ArrayList<Cd> inventory) {
         int b = inventory.size();
        for( int sort = 1; sort < b; sort++ ) {
            for( int i = 0; i < b - sort; i++ ) {
                if (inventory.get(i).getCD1().compareToIgnoreCase( inventory.get(i + 1).getCD1()) > 0) {
                    Cd temp = inventory.get(i);
                    inventory.set(i, inventory.get( i + 1 ));
                    inventory.set( i + 1, temp );
                } // End if 
            } // end if 
        } // end for loop
        return inventory;
    } // end sort   
    public void save() 
    {
       save(true);
    }
    public void save( boolean saved ) // Saves the file to the harddrive so it can be accessed later.
    {
       try 
       {
          BufferedWriter write = new BufferedWriter( new FileWriter("C:\\data\\inventory.dat"));
          for ( int b = 0; b < inventory.size(); b++ )
          {
             Cd gui = inventory.get(b);
             write.write( "Product number: " + gui.getInvent1() + "\n");
             write.write( "Artist name: " + gui.getCD1() + "\n");
             write.write( "CDs in stock: " + gui.getStock1() + "\n");
             write.write( "Title of CD: " +gui.getName() + "\n");
             write.write( "Cost of CD: " +gui.getCost1() + "\n");
             write.write ( "Value of Inventory: " + gui.total() + "\n");
             write.newLine();
             
             write.write( "Restocking fee: " + gui.getRestockFee() + "\n");
             write.write( "Value of Inventory: " + gui.total() + "\n");
             
          }
       }   
          catch ( Exception except ) 
          {
             if ( saved ) 
             {
                new File( "C:\\data\\") .mkdir();
                save( false );
             }
          }
    }
     public static void main(String[] args) 
    {
       Inventory1 gui = new Inventory1(); 
       gui.pack();
       gui.setVisible(true);
       System.out.println( inventory );
       System.out.println(value());
     }

    
} // End Class Inventory 1
class Product {
    
    private String CD;
    private int stock;
    private double cost;
    private int invent;
    
    // Four-argument constructor
    public Product ( int invent, String CD, int stock, double cost )
    {
        this.invent = invent;
        this.CD = CD;
        this.stock = stock;
        this.cost = cost;
        
    } // End four-argument Product constructor
   
    // Set Inventory Number
    public void setInvent1( int invent ) 
    { 
        this.invent = invent; // Should validate
    } // End method SetInvent
    
    //Return Inventory Number
    public int getInvent1() 
    {
        return invent;
    } // End method getInvent1
    
    // Set CD name
    public void setCD1( String CD )
    {
        this.CD = CD; // Should validate
        
        
    } // End method setCD
    
    // Return CD name
    public String getCD1()
    {
        return CD;
    } // End method getCD
    
    // Set the number of CDs in stock
    public void setStock1( int stock )
    {
        this.stock = stock; // Should validate
    } // End method setStock
    
    // Return number of CDs in stock
    public int getStock1()
    {
        return stock;
    } // End method getStock
    
    // Sets the cost for the CD
    public void setCost1( double cost )
    {
        this.cost = cost; // Should validate
    } // End method setCost
    
    // Return cost for CD
    public double getCost1()
    {
        return cost;
    } // End method getCost1
    
    // Determines what the total value of the CDs in the inventory
    public double total()
    {
        return cost * stock;
    } // End method total
    
 
    
    public String toString() 
    {
 
        return String.format("%-2s %-20s %4d %6.2f %8.2f ", invent, CD, stock, cost, total());
    }
} // End Class Product
class Cd extends Product {
   
    private String name;
    
    public Cd(int invent, String CD, int stock, double cost, String name) {
        super( invent, CD, stock, cost);
        setName(name);
    }
    
    public double getRestockFee() {
        return getStock1() * getCost1() * .05;
    } // End method getRestockFee
    
    @Override
    public double total() {
        return getStock1() * getCost1() + getRestockFee();
    }

    public void setName( String name1 ) {
        name = name1;
    }

    public String getName() {
        return name;
    }

    public String toString() {
        String fee = String.format("$ %12.2f | ", getRestockFee() );
        String Name = String.format( "%-10s", name );
        return super.toString() + " | " + Name + " | " + fee;
    }
}
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import javax.swing.JPanel;


public class GUI extends JPanel 
{
    // Creates Dimentions for Logo
    public static final int x_dia = 100; 
    public static final int y_dia = 100;
    
    public static final int cdx_dia = 20;
    public static final int cdy_dia = 20;
    
    public static final int littlehole = x_dia + 40;
    public static final int smallhole = y_dia + 40;
    public GUI() 
    {
    super();
    setPreferredSize(new Dimension( 200, 200 ));
    }
    @Override
    public void paint(Graphics g) 
    {
        super.paint(g); // Draw CD Logo
        g.setColor( Color.black ); //draw cd
        g.drawOval(x_dia, y_dia, 100, 100);
        g.fillOval(x_dia, y_dia, 100, 100);
        g.drawOval(littlehole, smallhole, 20, 20);
        g.setColor(Color.white);
        g.fillOval(littlehole, smallhole, 20, 20);
        g.setColor(Color.red);
        g.drawString("CD Inventory", 117, 125);
    }
}

Recommended Answers

All 10 Replies

will not display the cd information

Can you give us a clue on how/where the data is to be displayed?
Where does the info come from? Where is it to be displayed?

I see some catch blocks that do NOT call the printStackTrace() method. You need to add calls to that method to be sure you don't miss any exceptions.

Can you give us a clue on how/where the data is to be displayed?
Where does the info come from? Where is it to be displayed?

I see some catch blocks that do NOT call the printStackTrace() method. You need to add calls to that method to be sure you don't miss any exceptions.

The data is supposed to be displayed in the GUI however I can not seem to get it to do so. I will work on adding the printStackTrace() methods also and will respond if I may need more help. Thanks for the reply.

What data is supposed to display where in the GUI?

What data is supposed to display where in the GUI?

Sorry the ArrayList of the CD information. The method Inventory2() has all the information that is supposed to end up in the GUI. It should show up one item at a time and the buttons first, previous, next, and last to move to different items in the inventory. I am confused as to why it is not showing up.

Where is the info supposed to show up? A JLabel or a text field or ??? And what is the name of the variable the is supposed to show the info?
What is the name of the variable that has the data that is to be shown?

I have the info to show up in a text field. At least I had thought so. In the method cdinvent() I created the variable j for the TextArea and called up the data with j.append from the Product class to display the data. I had formatted the array list here to. I had thought this was the correct way of doing this. I have only been doing Java for a few months now and am struggling with the GUI. Every thing in my program shows up except for the array of the cds which is in Inventory2() method.

Your GUI layout looks unusual. You add components this way and that way.
I think it should be reworked.

To see if the JTextArea can display anything, put a String in it immediately after it is created and see if it is displayed. For example:

display.setText("THIS IS A  TEST");  // Show in the text area on startup

I tried that and it does not work at least in the cdinvent() method. However if i place it after I create the buttons for the GUI it does work. Any suggestions to point me in the right direction on how I can improve my GUI. I have been trying everything lately and feel more confused now than ever.

If I place it in line 39 the GUI displays "this is a test" in the TextArea of the GUI. I will look into the tutorials on layouts. Thanks for everything so far.

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.