GUI program

Inventory Program part 4

Page 1 of 1

1 Replies - 2759 Views - Last Post: 08 May 2010 - 03:41 PM Rate Topic: -----

#1 Guest_Kieth*


Reputation:

GUI program

Posted 08 May 2010 - 03:37 PM

I am working on my inventory program part 4 and I am having some difficulty with two things. I need my window to display the total value of the entire inventory and I can not figure out how to code a method to sum the total of the value of inventory in stock for all five objects in my array.

Second, when I run clean and build in my IDE it gives the following message "Note: C:\Users\Kieth\Documents\NetBeansProjects\Inventory Program Part 4\src\src\inventoryprogrampart4\FrameHelper.java uses unchecked or unsafe operations."

Any advise would be appreciated. The code that I have so far is as follows

package src.inventoryprogrampart4;


import javax.swing.JFrame;


public class InventoryViewer
{
    public static void main(String[] args)
    {
        JFrame frame = new FrameHelper();
        frame.setSize(300 , 350);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setTitle("DVD Inventory Program");
        frame.setLocationRelativeTo(null);
        frame.setVisible(true);
        
    }
}

package src.inventoryprogrampart4;

/**
 *
 * @author Kieth
 */

import java.awt.GridLayout;
import java.awt.BorderLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JPanel;
import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JTextField;
import java.util.Arrays;
import java.text.NumberFormat;

public class FrameHelper  extends JFrame
{
      NumberFormat nf = NumberFormat.getCurrencyInstance();
      private JPanel gridPanel = new JPanel(); 

      private JPanel panel = new JPanel(); 

      
      JButton nextButton; 

      
      JTextField itemNumberField; 
      JTextField movieTitleField; 
      JTextField unitsInStockField; 
      JTextField unitPriceField; 
      JTextField valueOfUnitsInStockField;
      JTextField movieGenreField;
      JTextField restockFeeField;
      JTextField totalInventoryValueField;
      
      JLabel lblItemNumber; 
      JLabel lblMovieTitle; 
      JLabel lblUnitsInStock; 
      JLabel lblUnitPrice; 
      JLabel lblValueOfUnitsInStock;
      JLabel lblMovieGenre;
      JLabel lblRestockFee;
      JLabel lblTotalInventoryValue;

      
      Genre genre1;
      Genre genre2;
      Genre genre3;
      Genre genre4;
      Genre genre5;


      public static final int MAX_GENRE = 5; // set maximum size for Genre Array
      Genre[] genre = new Genre[MAX_GENRE]; // create Genre Array object
      static int ArrayIndex = 0; // array index

      public FrameHelper() // constructor
      {
           
            genre1 = new Genre(); 
            genre1.setItemNumber("085391895923");
            genre1.setMovieTitle("Blazing Saddles");
            genre1.setUnitPrice(12.98);
            genre1.setUnitsInStock(250);
            genre1.setMovieGenre("Comedy");
            
            
            genre2 = new Genre(); 
            genre2.setItemNumber("024543656098");
            genre2.setMovieTitle("Avatar");
            genre2.setUnitPrice(19.99);
            genre2.setUnitsInStock(125);
            genre2.setMovieGenre("Sci-Fi");

            genre3 = new Genre(); 
            genre3.setItemNumber("024543665922");
            genre3.setMovieTitle("Crazy Heart");
            genre3.setUnitPrice(16.99);
            genre3.setUnitsInStock(98);
            genre3.setMovieGenre("Drama-Music");

            genre4 = new Genre(); 
            genre4.setItemNumber("085393142025");
            genre4.setMovieTitle("Full Metal Jacket");
            genre4.setUnitPrice(12.98);
            genre4.setUnitsInStock(143);
            genre4.setMovieGenre("Drama-War");

            genre5 = new Genre(); 
            genre5.setItemNumber("883929086696");
            genre5.setMovieTitle("The Blind Side");
            genre5.setUnitPrice(21.69);
            genre5.setUnitsInStock(105);
            genre5.setMovieGenre("Drama-Sports");

            
            genre[0] = genre1;
            genre[1] = genre2;
            genre[2] = genre3;
            genre[3] = genre4;
            genre[4] = genre5;


            gridPanel.setLayout(new BorderLayout()); // create a border layout
            gridPanel.add(this.createLabelPanel(), BorderLayout.WEST); // add label panel
            gridPanel.add(this.createTextPanel(), BorderLayout.CENTER); // add field panel
            gridPanel.add(this.createButtonPanel(), BorderLayout.SOUTH); // add button panel
            add(gridPanel);
      }
      
      private JPanel createButtonPanel()
      {
            ActionListener btnListen = new ButtonListener(); 
            
            nextButton = new JButton("Next Movie");  
            nextButton.setActionCommand("Next");  
            nextButton.addActionListener(btnListen); 
            
            JPanel panel = new JPanel();
            panel.add(nextButton);
            return panel; 
      } 
      private JPanel createLabelPanel()
      {
           
            lblItemNumber = new JLabel("ItemNumber: ");
            lblMovieTitle = new JLabel("Movie Title: ");
            lblUnitPrice = new JLabel("Unit Price: ");
            lblUnitsInStock = new JLabel("Units In Stock: ");
            lblValueOfUnitsInStock = new JLabel("Value Of Units In Stock: ");
            
            lblMovieGenre = new JLabel("Genre: ");
            lblRestockFee = new JLabel("Restock Fee: ");
            lblTotalInventoryValue = new JLabel("Total Inventory Value: ");

            panel = new JPanel();
            panel.setLayout(new GridLayout(8, 1, 5 , 5));
            
            panel.add(lblItemNumber);
            panel.add(lblMovieTitle);
            panel.add(lblUnitPrice);
            panel.add(lblUnitsInStock);
            panel.add(lblValueOfUnitsInStock);
            panel.add(lblMovieGenre);
            panel.add(lblRestockFee);
            panel.add(lblTotalInventoryValue);
            return panel;// return the panel
      } 
      private JPanel createTextPanel()
      {
            
            itemNumberField = new JTextField(); 
            itemNumberField.setEditable(false);
            

            movieTitleField = new JTextField(); 
            movieTitleField.setEditable(false);  


            unitPriceField = new JTextField(); 
            unitPriceField.setEditable(false);  


            unitsInStockField = new JTextField();   
            unitsInStockField.setEditable(false);

            valueOfUnitsInStockField = new JTextField();
            valueOfUnitsInStockField.setEditable(false);

            movieGenreField = new JTextField();   
            movieGenreField.setEditable(false);   

            restockFeeField = new JTextField();
            restockFeeField.setEditable(false);

            totalInventoryValueField = new JTextField();
            totalInventoryValueField.setEditable(false);


            panel = new JPanel(); 
            panel.setLayout(new GridLayout(8, 1 , 5 , 5));
            panel.add(itemNumberField); 
            panel.add(movieTitleField); 
            panel.add(unitPriceField); 
            panel.add(unitsInStockField);
            panel.add(valueOfUnitsInStockField);
            panel.add(movieGenreField);
            panel.add(restockFeeField);
            panel.add(totalInventoryValueField);





            return panel; // return the panel
      } // end createTextPanel method
      // nested ButtonListener class
      class ButtonListener implements ActionListener
      {
            public void actionPerformed(ActionEvent e)
            {
                  
                  Arrays.sort(genre, new MovieTitleComparator()); 
                  if (e.getActionCommand().equals("Next"))
                        if (ArrayIndex < genre.length) 
                        {
                              itemNumberField.setText(genre[ArrayIndex].getItemNumber());
                              
                              movieTitleField.setText(genre[ArrayIndex].getMovieTitle());
                              
                              unitPriceField.setText(nf.format(genre[ArrayIndex].getUnitPrice()));
                              
                              unitsInStockField.setText(String.valueOf(genre[ArrayIndex].getUnitsInStock()));
                              
                              valueOfUnitsInStockField.setText(nf.format(genre[ArrayIndex].getInventoryValue()));
                              
                              movieGenreField.setText(String.valueOf(genre[ArrayIndex].getMovieGenre()));
                              
                              restockFeeField.setText(nf.format(genre[ArrayIndex].getRestockFee()));
                              
                              totalInventoryValueField.setText(nf.format(genre[ArrayIndex].getTotalInventoryValue()));

                              ArrayIndex = ArrayIndex + 1;
                        } // end if
            } // end actionPerformed
      } // end class ButtonListener
} // end FrameHelperClass

package src.inventoryprogrampart4;



import java.text.NumberFormat;

public class ProductInfo
{
    NumberFormat nf = NumberFormat.getCurrencyInstance();


    private String itemNumber;
    private String movieTitle;
    private double unitsInStock;
    private double unitPrice;
    private double inventoryValue;
    private double totalInventoryValue;

    public ProductInfo(){}


    public ProductInfo(String itemNumberIn , String movieTitleIn , int unitsInStockIn , double unitPriceIn)
    {
        this.setItemNumber(itemNumberIn);
        this.setMovieTitle(movieTitleIn);
        this.setUnitsInStock(unitsInStockIn);
        this.setUnitPrice(unitPriceIn);
    }


    public void setItemNumber(String itemNumberIn)
    {
        this.itemNumber = itemNumberIn;
    }

    public String getItemNumber()
    {
        return itemNumber;
    }

    public void setMovieTitle(String movieTitleIn)
    {
        this.movieTitle = movieTitleIn;
    }

    public String getMovieTitle()
    {
        return movieTitle;
    }

    public void setUnitsInStock(double unitsInStockIn)
    {
        this.unitsInStock = unitsInStockIn;
    }

    public double getUnitsInStock()
    {
        return unitsInStock;
    }

    public void setUnitPrice(double unitPriceIn)
    {
        this.unitPrice = unitPriceIn;
    }

    public double getUnitPrice()
    {
        return unitPrice;
    }

    

    public double getInventoryValue()
    {
        return inventoryValue = getUnitPrice()*getUnitsInStock();
    }

    

    public void setTotalInventoryValue(double totalInventoryValue)
    {
        this.totalInventoryValue = totalInventoryValue + getTotalInventoryValue();
    }

    public double getTotalInventoryValue()
    {
        return totalInventoryValue;
    }
}//End ProductInfo class


package src.inventoryprogrampart4;



public class Genre extends ProductInfo
{
    
    private String movieGenre;
    private double restockFee;

    

    Genre(){}

    public void setMovieGenre(String movieGenreIn)
    {
        this.movieGenre = movieGenreIn;
    }   

    public String getMovieGenre()
    {
        return movieGenre;
    }


    public double getRestockFee()
    {
        return restockFee = (getUnitPrice() * getUnitsInStock() * .05);
    }
}//end Genre class

package src.inventoryprogrampart4;


import java.util.Comparator;

public class MovieTitleComparator implements Comparator
{

    public int compare (Object productInfoOne , Object productInfoTwo)
            

    {

        String movieTitle1 = ((ProductInfo)productInfoOne).getMovieTitle().toUpperCase();
        String movieTitle2 = ((ProductInfo)productInfoTwo).getMovieTitle().toUpperCase();


        
        if (!(movieTitle1.equals(movieTitle2)))
                  return movieTitle1.compareTo(movieTitle2);
            else
                  return movieTitle2.compareTo(movieTitle1);

    }
}


Is This A Good Question/Topic? 0

Replies To: GUI program

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10364
  • View blog
  • Posts: 38,380
  • Joined: 27-December 08

Re: GUI program

Posted 08 May 2010 - 03:41 PM

The root of your problem lies within your MovieTitleComparator class. Since Comparator is Generic, MovieTitleComparator should be Generic as well. So your header: public class MovieTitleComparator implements Comparator should read: public class MovieTitleComparator<T> implements Comparator<T>, replacing the Object types with generic T types in your Comparator methods as specified by the API.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1