import java.util.Arrays;

// Class that hold a collection of CD objects
class Inventory
{
	// maximum number of CDs that will be held
	static public final int MAX_NUM_OF_PRODUCTS = 10;

	private CD products[] = new CD[MAX_NUM_OF_PRODUCTS];

	// number of CDs currently in the inventory
	private int numOfProducts;
	
	// default constructor
	public Inventory()
	{
		numOfProducts = 0;
	}

	// returns the value of all the CDs in the inventory
	// adds up the value of each CD, returns sum
	public double getTotalValue()
	{
		double inventoryValue = 0;
		for (int i = 0; i < products.length; i++)
		{
			CD theCD = products[i];
			if (theCD != null)
			{
				inventoryValue += theCD.getInventoryValue();
			}
		}
		return inventoryValue;
	}
	
	// sorts the CDs by title
	public void sortInventory()
	{
		Arrays.sort(products, 0, numOfProducts) ;
	}


	// Return the CD at the index given
	public CD getCD(int index)
	{
		return products[index];
	}

	// Add a new CD to the end of the inventory
	public void addCD(CD p)
	{
		products[numOfProducts] = p;
		numOfProducts++;
	}

	// returns the number of CDs currently in the inventory
	public int getNumberOfCDs()
	{
		return numOfProducts;
	}
}
import java.io.*;
import java.util.*; 
import java.text.*;

import javax.swing.*;
import java.awt.*;
import java.awt.event.*;

import java.net.URL;

/*
 * Driver program to demonstrate the use of the CD class to store and display inventory information
 */
public class Inventory_Part5 {

	// Create an Inventory object to hold up to 10 CDs from the input that the user enters
	Inventory theInventory = new Inventory();
	
	int index = 0;

	// create a number formatter that turns doubles into strings that look like prices
	NumberFormat moneyFormat = NumberFormat.getCurrencyInstance();

	// GUI elements to display currently selected DVD's information
	private JLabel itemNumberLabel;
	private JTextField itemNumberText;
	
	private JLabel produdctNameLabel;
	private JTextField prodnameText;
	
	private JLabel genreLabel;
	private JTextField genreText;
	
	private JLabel numinstockLabel;
	private JTextField numinstockText;
	
	private JLabel productPriceLabel;
	private JTextField productPriceText;
	
	private JLabel restockingFeeLabel;
	private JTextField restockingFeeText;
	
	private JLabel valueLabel;
	private JTextField valueText;
	
	private JLabel entireInventoryValueLabel;
	private JLabel entireInventoryValueText;

	// load up the logo into a component for the GUI
	private JLabel logoLabel;
	
	// panel to hold buttons
	private JPanel buttonPanel; 

	private JFrame dvdFrame;

	// setup buttons
	// button to display next inventory item
	JButton nextButton;
	JButton previousButton;
	JButton firstButton;
	JButton lastButton;

	// setup the GUI
	public Inventory_Part5()
	{
		// keep tracks of how many CDs the user has entered so far
		int counter = 0;
		
		// create Scanner to obtain input from command window
		BufferedReader input = new BufferedReader(new InputStreamReader(System.in) );
		
		//Print out a screen title
		System.out.println("Inventory Program\n\n");
		
		// we're going to keep asking for user input until the user enters stop for an item name
		while( counter < 10 ) {
			
			String itemTitle = null;
			do {
				try {
					// prompt for input
					System.out.print( "Enter the title of the CD or the word stop: " );
					itemTitle = input.readLine();
				} catch (IOException e) {
					System.out.println("Problem with that input. Please try again.");
				}
			} while (itemTitle == null);
			
			// if the user entered stop for the item name, then quit the loop
			if ( itemTitle.equalsIgnoreCase("stop") ) {
				break;
			}
			
			//Enter Item number
			String itemNumber = null;
			do {
				try {
					System.out.print( "Enter the Item number: " ); // prompt for input
					itemNumber = input.readLine();
				} catch (IOException e) {
					System.out.println("Problem with that input. Please try again.");
				}
			} while (itemNumber == null);
					
			//Enter CD genre
			String itemGenre = null;
			do {
				try {
					System.out.print( "Enter the genre of the CD: " );
					itemGenre = input.readLine();
				} catch (IOException e) {
					System.out.println("Problem with that input. Please try again.");
				}
			} while (itemGenre == null);					

			// Enter Number of Items in Stock
			// read Number of Items in Stock
 			int itemQuantity = -1;
			do {
				try {
					System.out.print("Enter number of Items in Stock: ");
					itemQuantity = Integer.valueOf(input.readLine()).intValue();
					// while loop to repeat the entry of Items in stock until a positive value is entered
					while ( itemQuantity < 0)
					{
						System.out.print("Invalid Entry: Please enter a non-negative for Number in Stock: ");
						// read Item number
						itemQuantity = Integer.valueOf(input.readLine()).intValue(); 
					}
				} catch (IOException e) {
					System.out.println("Problem with that input. Please try again.");
				}
			} while (itemQuantity < 0);
			
			
			// Enter price of the Item
			// prompt for input
			double itemPrice = -1.0; 
			do {
				try {
				System.out.print( "Enter the price of the item: " ); 
				// Read price of the OfficeSupply
					itemPrice = Double.valueOf(input.readLine()).doubleValue();
					// while loop to repeat the entry of price a positive value is entered
					while ( itemPrice <=0.0 )
					{
						System.out.print("Invalid Entry: Please enter a positive Item Price: ");
						// read Item Price
						itemPrice = Double.valueOf(input.readLine()).doubleValue(); 
					}
				} catch (IOException e) {
					System.out.println("Problem with that input. Please try again.");
				}
			} while (itemPrice < 0);
			
			

			// create a new CD object
			CD theCD = new CD(itemTitle,  itemNumber, itemGenre, itemQuantity,  itemPrice);
			// stick the new CD into our array at the end
			theInventory.addCD(theCD);
			
			// increment the counter
			counter++;

			// Print out an extra new line
			System.out.println();

		} // end while


		// now that the user is done entering CDs,
		
		// setup the element of the GUI
		itemNumberLabel = new JLabel("Product Number: ");
		produdctNameLabel = new JLabel("Title: ");
		genreLabel = new JLabel("Genre: ");
		numinstockLabel = new JLabel("Number in Stock: ");
		productPriceLabel = new JLabel("Product Price: ");
		restockingFeeLabel = new JLabel("Restocking Fee: ");
		valueLabel = new JLabel("Inventory Value: ");
		entireInventoryValueLabel  = new JLabel("Value of Entire Inventory:");
		logoLabel = new JLabel();

		itemNumberText = new JTextField("");
		prodnameText = new JTextField("");
		genreText = new JTextField("");
		numinstockText = new JTextField("");
		productPriceText = new JTextField("");
		restockingFeeText = new JTextField("");
		valueText = new JTextField("");
		entireInventoryValueText = new JLabel("");

		itemNumberText.setEditable(false);
		prodnameText.setEditable(false);
		genreText.setEditable(false);
		numinstockText.setEditable(false);
		productPriceText.setEditable(false);
		restockingFeeText.setEditable(false);
		valueText.setEditable(false);
		

		// setup the code for the buttons
		nextButton = new JButton("Next"); 
		nextButton.addActionListener(new ActionListener() // register event handler 
		{ 
			public void actionPerformed(ActionEvent event) // process button event 
			{  	
				// when going to the previous item, step forward one
				index++;
				// if we were at the end (ie. index = theInventory.getNumberOfCDs() - 1), then index will be theInventory.getNumberOfCDs() now
				if ( index == theInventory.getNumberOfCDs() ) {
					// we can't have the index exceed the size of our inventory array, so we jump to the front of the inventory
					// whose index is
					index = 0;
				}
				repaint();				
			}
		});
		
		previousButton = new JButton("Previous"); 
		previousButton.addActionListener(new ActionListener() // register event handler 
		{ 
			public void actionPerformed(ActionEvent event) // process button event 
			{  	
				// when going to the previous item, step back one
				index--;
				// if we were at the beginning (ie. index = 0), then index will be -1 now
				if ( index == -1 ) {
					// we can't have index == -1, so we jump to the end of the inventory
					// whose index is
					index = theInventory.getNumberOfCDs() - 1;
				}
				repaint();
			}
		});
		
		firstButton = new JButton("First"); 
		firstButton.addActionListener(new ActionListener() // register event handler 
		{ 
			public void actionPerformed(ActionEvent event) // process button event 
			{  	
				// the first element of the inventory is at
				index = 0;
				
				repaint();				
			}
		});
		
		lastButton = new JButton("Last"); 
		lastButton.addActionListener(new ActionListener() // register event handler 
		{ 
			public void actionPerformed(ActionEvent event) // process button event 
			{  	
				// the last element of the inventory is at
				index = theInventory.getNumberOfCDs() - 1;

				repaint();				
			}
		});

		dvdFrame = new JFrame(); // JFrame container

		// title of window
		dvdFrame.setTitle("CD Inventory - 5");

		// quit the program when the window is closed
 		dvdFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		// create and setup panel to hold buttons
		buttonPanel = new JPanel();
		buttonPanel.add(nextButton);
		buttonPanel.add(previousButton);
		buttonPanel.add(firstButton);
		buttonPanel.add(lastButton);
		// add the panel to the top of the GUI's window
		dvdFrame.getContentPane().add(buttonPanel, BorderLayout.NORTH);

		// setup a panel to collect all the components.
		// this will have 8 rows and 2 columns
		JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4));
		// row 1
		centerPanel.add(itemNumberLabel);
		centerPanel.add(itemNumberText);

		// row 2
		centerPanel.add(produdctNameLabel);
		centerPanel.add(prodnameText);

		// row 3
		centerPanel.add(genreLabel);
		centerPanel.add(genreText);

		// row 4
		centerPanel.add(numinstockLabel);
		centerPanel.add(numinstockText);

		// row 5
		centerPanel.add(productPriceLabel);
		centerPanel.add(productPriceText);

		// row 6
		centerPanel.add(restockingFeeLabel);
		centerPanel.add(restockingFeeText);

		// row 7
		centerPanel.add(valueLabel);
		centerPanel.add(valueText);
		
		// add the panel to the center of the GUI's window
		dvdFrame.getContentPane().add(centerPanel, BorderLayout.CENTER);

		// Setup a panel to hold the information about the entire inventory's value
		JPanel entireInventoryValuePanel = new JPanel();
		entireInventoryValuePanel.add(entireInventoryValueLabel);
		entireInventoryValuePanel.add(entireInventoryValueText);

		// add this panel to the bottom of the GUI's window
		dvdFrame.getContentPane().add(entireInventoryValuePanel, BorderLayout.SOUTH);
		
		// Load the logo.jpg image from the disk
		try {
			logoLabel = new JLabel();
			logoLabel.setIcon(new javax.swing.ImageIcon(getClass().getResource("logo.jpg")));
			dvdFrame.getContentPane().add(logoLabel, BorderLayout.EAST);
		} catch (Exception e) {
			// display an error message
			JOptionPane.showMessageDialog (null,
				"Problem loading image logo.jpg",
				"Inventory GUI",
				JOptionPane.ERROR_MESSAGE);
		}
		
		// now that the user is done entering CDs, and teh GUI is setup
		// sort the CDs by name
		theInventory.sortInventory();
		// display the GUI on screen
		setVisible(true);
	}

	public void setVisible(boolean status) {
		// display the window
		dvdFrame.pack();
		dvdFrame.setVisible(status);	
		repaint();
	}

	// each time the program data is modified, called repaint() to get the most recent information on the screen
	public  void repaint() {

		// display the current item on the screen's text area
		// get the CD at the current index
		CD theCD = theInventory.getCD(index);
	
		// print out its information to the screen
		if (theCD != null) {

			itemNumberText.setText( ""+theCD.getItemNumber() );
			
			prodnameText.setText( theCD.getTitle() );

			genreText.setText( theCD.getGenre() );

			productPriceText.setText( moneyFormat.format(theCD.getPrice()) );

			restockingFeeText.setText( moneyFormat.format(theCD.restockingFee()) );

			numinstockText.setText( ""+theCD.getItemQuantity() );
			
			valueText.setText( moneyFormat.format(theCD.getInventoryValue()) );
			
		}
		
		entireInventoryValueText.setText( moneyFormat.format(theInventory.getTotalValue()) );

	}

	public static void main(String args[]) throws IOException
	{	

		new Inventory_Part5();

	}
}

The above is a Inventory program which after being modified adds a button to the GUI that will allow the user to move the the first item, the previous item, next item, last item in that inventory. If the first item is displayed and the user clicks on the previous button the last item should display and so on.
Is this code adequate and I need help adding a company logo to the GUI using Java graphics.

Did you post the CD class?

no what is the cd class?

no what is the cd class?

I don't know what the CD class is, and if you don't either, then lines like these make no sense:

CD theCD = theInventory.getCD(index);

Should I incorporate the CD clas in with the rest of the program?

Should I incorporate the CD clas in with the rest of the program?

I would imagine so, given that the whole project seems to revolve around the CD class:

// Class that hold a collection of CD objects
class Inventory

And you are referring to the CD class all over the place, so presumably you have a CD class with this constructor:

CD theCD = new CD(itemTitle, itemNumber, itemGenre, itemQuantity, itemPrice);

Thank you

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.