Results 1 to 9 of 9
  1. #1
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Red face Looking for a little help

    Hi all,

    If someone can help with this Program?

    I need to modify the Inventory Program to include an Add button, a Delete button, and a Modify button on the GUI. These buttons should allow the user to perform the corresponding actions on the item name, the number of units in stock, and the price of each unit. An item added to the inventory should have an item number one more than the previous last item. add a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file. Add a search button to the GUI that allows the user to search for an item in the inventory by the product name. If the product is not found, the GUI should display an appropriate message. If the product is found, the GUI should display that productís information in the GUI.


    I did this but can't get it to work...Here is my Program.

    Java Code:
    import java.awt.*;              
    import java.awt.event.*;      
    import javax.swing.*;                 
    
    
    
    public class Week8 {
    
       
        public static void main(String[] args) {
    
    
            Movie dvd = null;
            Inventory inventory = new Inventory();        
    
            dvd = new Movie(15035, "The Davinci Code", 18, 19.99f, 2001);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(24478, "Fight Club", 14, 20.99f, 1998);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(32059, "Ladder 49", 1, 21.99f, 1995);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
    
            dvd = new Movie(35989, "National Treasure", 15, 18.99f, 2003);
            inventory.addMovie(dvd);                    
            System.out.println(dvd);
    
            dvd = new Movie(14418, "Facing the Giants", 10, 21.99f, 2006);
            System.out.println(dvd);
            inventory.addMovie(dvd);                    
            
            dvd = new Movie(13679, "Crimson Tide", 9, 7.99f, 2005);
            System.out.println(dvd);
            System.out.println("\nEnd of DVD collection!\n");
            inventory.addMovie(dvd);                   
    
            inventory.printInventory();
            new InventoryGUI(inventory);                          
        } 
    
    } 
    
    class DVD {
        private int itemNo;
        private String title;
        private int inStock;
        private float unitPrice;
    
        DVD() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        DVD(int itemNo, String title, int inStock, float unitPrice) {
            this.itemNo    = itemNo;
            this.title     = title;
            this.inStock   = inStock;
            this.unitPrice = unitPrice;
        }
    
        public int getItemNo()      { return itemNo; }
        public String getTitle()    { return title; }
        public int getInStock()     { return inStock; }
        public float getUnitPrice() { return unitPrice; }
    
        public float value() {
            return inStock * unitPrice;
        }
    
        public String toString() {
            return String.format("ItemNo=%2d   Title=%-22s   InStock=%3d   Price=$%7.2f   Value=$%8.2f",
                                  itemNo, title, inStock, unitPrice, value());
        }
    
        void addDVD() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        void closeFile() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        void openFile() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
    } 
    class Inventory {
    
        static int getitemNo() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
       
        private final int INVENTORY_SIZE = 30;
        private DVD[] items;
        private int numItems;
         
        Inventory() {
            items = new Movie[INVENTORY_SIZE];
            numItems = 0;
        }
    
        public int getNumItems() {
            return numItems;
        }
    
        public DVD getDVD(int n) {
            return items[n];
        }
    
        
        public void addMovie(DVD item) {
            items[numItems] = item;    
            ++numItems;
        }
    
    
        public double value() {
            double sumOfInventory = 0.0;
    
            for (int i = 0; i < numItems; i++)
                sumOfInventory += items[i].value();
                            
            return sumOfInventory;
        }
    
        
        public void printInventory() {
            System.out.println("\n DVD Inventory\n");
           
            
            if (numItems <= 0) {
                System.out.println("Inventory is empty at the moment.\n");
            } else {
                for (int i = 0; i < numItems; i++)
                    System.out.printf("%3d   %s\n", i, items[i]);
                System.out.printf("\nTotal value in inventory is $%,.2f\n\n", value());
            }
        }
        
    } 
    class Movie extends DVD {
        
        private int movieYear;
    
        
        public Movie(int MovieID, String itemName, int quantityOnHand, float itemPrice, int year) {
            super(MovieID, itemName, quantityOnHand, itemPrice);    
           
            this.movieYear = movieYear;        
        }
    
       
        public void setYear(int year) {
            movieYear = year;
        }
    
        
        public int getMovieYear() {
            return movieYear;
        }
    
        
         public float value() {
            return super.value() + restockingFee();
        } 
    
        
        public float restockingFee() {
            return super.value() * 0.05f;
        }
    
    } 
    
    
    
    
    class InventoryGUI extends JFrame 
    {
       
        
        private Inventory theInventory; 
        
         
        private int index = 0; 
        
         
        private final JLabel itemNumberLabel = new JLabel("  Item Number:"); 
        private JTextField itemNumberText; 
        
        private final JLabel prodnameLabel = new JLabel("  Product Name:"); 
        private JTextField prodnameText; 
        
        private final JLabel prodpriceLabel = new JLabel("  Price:"); 
        private JTextField prodpriceText; 
        
        private final JLabel numinstockLabel = new JLabel("  Number in Stock:"); 
        private JTextField numinstockText; 
        
        private final JLabel valueLabel = new JLabel("  Value:"); 
        private JTextField valueText; 
        
        private final JLabel restockingFeeLabel = new JLabel("  Restocking Fee:"); 
        private JTextField restockingFeeText; 
        
        private final JLabel totalValueLabel = new JLabel("  Inventory Total Value:"); 
        private JTextField totalValueText; 
    
        private JPanel centerPanel;
        private JPanel buttonPanel;
    
    
    
    
         
        InventoryGUI(Inventory inventory) {
            super(" Movie Inventory");
            final Dimension dim = new Dimension(140, 20);
            final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
            JPanel jp;
    
             
            theInventory = inventory;        
            
            
            
            centerPanel = new JPanel(); 
            centerPanel.setLayout(new BoxLayout(centerPanel, BoxLayout.Y_AXIS));
    
            buttonPanel = new JPanel();
            JButton nextButton = new JButton("Next");    
            nextButton.addActionListener(new NextButtonHandler());
            buttonPanel.add(nextButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton previousButton = new JButton("Previous");    
            previousButton.addActionListener(new PreviousHandler());
            buttonPanel.add(previousButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton firstButton = new JButton("First");    
            firstButton.addActionListener(new FirstHandler());
            buttonPanel.add(firstButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton lastButton = new JButton("Last");    
            lastButton.addActionListener(new LastHandler());
            buttonPanel.add(lastButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton addButton = new JButton("ADD");    
            addButton.addActionListener(new AddHandler());
            buttonPanel.add(addButton); 
            centerPanel.add(buttonPanel);
            
            buttonPanel = new JPanel();
            JButton deleteButton = new JButton("Delete");    
            deleteButton.addActionListener(new DeleteHandler());
            buttonPanel.add(deleteButton); 
            centerPanel.add(buttonPanel);
            
            JButton searchButton = new JButton("Search");
            searchButton.addActionListener(new SearchHandler());
            buttonPanel.add(searchButton);
            
            jp = new JPanel(flo);
            itemNumberLabel.setPreferredSize(dim);
            jp.add(itemNumberLabel); 
            itemNumberText = new JTextField(10); 
            itemNumberText.setEditable(false); 
            jp.add(itemNumberText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodnameLabel.setPreferredSize(dim);
            jp.add(prodnameLabel); 
            prodnameText = new JTextField(10); 
            prodnameText.setEditable(false); 
            jp.add(prodnameText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            prodpriceLabel.setPreferredSize(dim);
            jp.add(prodpriceLabel); 
            prodpriceText = new JTextField(10); 
            prodpriceText.setEditable(false); 
            jp.add(prodpriceText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            numinstockLabel.setPreferredSize(dim);
            jp.add(numinstockLabel); 
            numinstockText = new JTextField(10); 
            numinstockText.setEditable(false); 
            jp.add(numinstockText);  
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            restockingFeeLabel.setPreferredSize(dim);
            jp.add(restockingFeeLabel); 
            restockingFeeText = new JTextField(10); 
            restockingFeeText.setEditable(false); 
            jp.add(restockingFeeText); 
            centerPanel.add(jp);
            
            jp = new JPanel(flo);
            valueLabel.setPreferredSize(dim);
            jp.add(valueLabel); 
            valueText = new JTextField(10); 
            valueText.setEditable(false); 
            jp.add(valueText); 
            centerPanel.add(jp);
            
            
            jp = new JPanel(flo);
            totalValueLabel.setPreferredSize(dim);
            jp.add(totalValueLabel); 
            totalValueText = new JTextField(10);
            totalValueText.setEditable(false);
            jp.add(totalValueText); 
            centerPanel.add(jp);
    
            
            JLabel title = new JLabel("Best DVD's");
            add(title, "North");
            add(centerPanel); 
            Icon logo = new ImageIcon("C:/logo.gif"); 
            repaintGUI();
    
            setDefaultCloseOperation(EXIT_ON_CLOSE); 
            setSize(400, 550); 
            setResizable(false); 
            setLocationRelativeTo(null); 
            setVisible(true);
     
        } 
    
         
        public void repaintGUI() { 
            Movie temp = (Movie) theInventory.getDVD(index); 
            
            if (temp != null) { 
                itemNumberText.setText("" + temp.getItemNo());     
                prodnameText.setText(temp.getTitle()); 
                prodpriceText.setText(String.format("$%.2f", temp.getUnitPrice())); 
                restockingFeeText.setText(String.format("$%.2f", temp.restockingFee())); 
                numinstockText.setText("" + temp.getInStock()); 
                valueText.setText(String.format("$%.2f", temp.value())); 
            }
            totalValueText.setText(String.format("$%.2f", theInventory.value())); 
        }
    
        class NextButtonHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (++index) % numItems;
                repaintGUI();
            }
        }
       class PreviousHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                index--;
                if(index<0){
                    index = theInventory.getNumItems() -1;
                }
                repaintGUI();  
            }
         }
         class FirstHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index=0) % numItems;
                repaintGUI();
            }
         }   
       
         class LastHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index=5) % numItems;
                repaintGUI();
            }
         }
         
          class AddHandler implements ActionListener {
           public void actionPerformed(ActionEvent e){
             repaintGUI();
               }
             }
    
         
          class DeleteHandler implements ActionListener {
            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (index) % numItems;
                repaintGUI();
            }
         } 
          
    class ModifyHandler implements ActionListener {
        private Object prodName;
        private Object itemNo;
        private Object numinstockText;
    public void actionPerformed(ActionEvent e){
               if (prodName.getClass().equals(""))  
     {
      JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
            repaintGUI();
           }
                if (itemNo.getClass().equals(""))  
                {
     JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
             repaintGUI();
                }
    
                try 
                {  
                }
            catch (Exception d)
           {
         JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Negative GhostRider the pattern is full", JOptionPane.ERROR_MESSAGE);
                    repaintGUI();
                    }
    
    
                String name; int number; double amount, price; String title; 
                
                name = prodNameText();
                number = itemNumberText();
                amount = numinstockText();
                price = prodpriceText();
                repaintGUI();
            }
    
        private int itemNumberText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private double numinstockText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private String prodNameText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private double prodpriceText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private void repaintGUI() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        }  
           
         class SaveHandler implements ActionListener {
         public void actionPerformed(ActionEvent e){
              int itemNo = Inventory.getitemNo();
                DVD dvd = new DVD(); 
                dvd.openFile();
                int currentRecord = 0; 
    
                do 
                {
                    dvd.addDVD();
                    currentRecord = currentRecord + 1;
                    index = (++index) % itemNo;
    
                } while (currentRecord < itemNo); 
    
                    dvd.closeFile(); 
            } 
    
        } 
    
          class SearchHandler implements ActionListener {
              private Object searchText;
    
        
                public void actionPerformed(ActionEvent e,String prodNameText,String search){
        
    
                String name = prodNameText();
                int number = itemNumberText();
                int units = numinstockText();
                int currentDVD = 0;    
                do 
                {                    
                    repaintGUI();
                    name = prodNameText();
                    number = itemNumberText();
                    if(name.equalsIgnoreCase( search )) 
                        repaintGUI();
                }while(name.equalsIgnoreCase(search ) != true); 
            }
        private int itemNumberText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private int numinstockText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        private String prodNameText() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        private void repaintGUI() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
        public void actionPerformed(ActionEvent e) {
            throw new UnsupportedOperationException("Not supported yet.");
        }
    
          
        } 
    }
    Thanks for all the help.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    One thing I noticed is that your Inventory class stores DVDs in a DVD array. This is great if you know that the contents of this collection won't change, but now that you're adding "add" and "delete" to the mix, these assumptions won't hold and the model will break. Much better here is to use the more flexible ArrayList<DVD> class to hold your DVDs.

    Edit: I see that you've created a DVD array with a capacity to hold 30 DVDs. This could work as long as you don't go beyond this capacity. I still recommend using ArrayList, but see now that it is not 100% required.

    Now regarding this statement:
    I did this but can't get it to work...Here is my Program.
    It's your turn to be more specific. I see no attempt to add functionality to your add button here. So what exactly do you mean by "can't get it to work"?
    Last edited by Fubarable; 08-30-2008 at 04:16 AM.

  3. #3
    Norm's Avatar
    Norm is online now Moderator
    Join Date
    Jun 2008
    Location
    Eastern Florida
    Posts
    17,816
    Rep Power
    25

    Default

    can't get it to work
    Can you explain in a little more detail?

    What happened to the comments in your program?
    They are a useful way to explain to other programmers what it is the code is doing and to explain any tricky spots.

  4. #4
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Ok In the Modify class, I am getting some errors. Here is the code and the errors.

    Java Code:
    class ModifyHandler implements ActionListener {
             private Object title;
             private Object numItems;
             private Object inStock;
                public void actionPerformed(ActionEvent e){
                  if (title.getClass().equals(""))  
     {
      JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Not Going to Happen", JOptionPane.ERROR_MESSAGE);
            repaintGUI();
           }
                if (numItems.getClass().equals(""))  
                {
     JOptionPane.showMessageDialog(null, "Please Complete the Entry.","Not Going to Happen", JOptionPane.ERROR_MESSAGE);
             repaintGUI();
                }
                try 
                {  
            }
            catch (Exception d)
           {
     JOptionPane.showMessageDialog(null, "Recheck Entry use numbers for price and quantity.","Not Going to Happen", JOptionPane.ERROR_MESSAGE);
                    repaintGUI();
                    }
                String name; int number; double amount, price;  
                
                name = title();
                number = numItems();
                amount = inStock();
                price = unitPrice();
                repaintGUI();
            }
    
        private int numItems() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        private double inStock() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        private String title() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        private double unitPrice() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
        private void repaintGUI() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    
              }
    And here are the errors

    javaapplication52.InventoryGUI$ModifyHandler.actio nPerformed(Week9a.java:408)
    at javax.swing.AbstractButton.fireActionPerformed(Abs tractButton.java:1995)
    at javax.swing.AbstractButton$Handler.actionPerformed (AbstractButton.java:2318)
    at javax.swing.DefaultButtonModel.fireActionPerformed (DefaultButtonModel.java:387)
    at javax.swing.DefaultButtonModel.setPressed(DefaultB uttonModel.java:242)
    at javax.swing.plaf.basic.BasicButtonListener.mouseRe leased(BasicButtonListener.java:236)
    at java.awt.Component.processMouseEvent(Component.jav a:6041)
    at javax.swing.JComponent.processMouseEvent(JComponen t.java:3265)
    at java.awt.Component.processEvent(Component.java:580 6)
    at java.awt.Container.processEvent(Container.java:205 8)
    at java.awt.Component.dispatchEventImpl(Component.jav a:4413)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2116)
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.LightweightDispatcher.retargetMouseEvent( Container.java:4322)
    at java.awt.LightweightDispatcher.processMouseEvent(C ontainer.java:3986)
    at java.awt.LightweightDispatcher.dispatchEvent(Conta iner.java:3916)
    at java.awt.Container.dispatchEventImpl(Container.jav a:2102)
    at java.awt.Window.dispatchEventImpl(Window.java:2440 )
    at java.awt.Component.dispatchEvent(Component.java:42 43)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java: 599)
    at java.awt.EventDispatchThread.pumpOneEventForFilter s(EventDispatchThread.java:273)
    at java.awt.EventDispatchThread.pumpEventsForFilter(E ventDispatchThread.java:183)
    at java.awt.EventDispatchThread.pumpEventsForHierarch y(EventDispatchThread.java:173)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:168)
    at java.awt.EventDispatchThread.pumpEvents(EventDispa tchThread.java:160)
    at java.awt.EventDispatchThread.run(EventDispatchThre ad.java:121)


    Thanks for any help.

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Your error message is missing its head, the text on the top that states what kind of error message is being thrown. I'm going to guess that it's an UnsupportedOperationException and it's being thrown because your code is telling java to throw it. What would you expect a method such as this to do:
    Java Code:
        private double inStock() {
            throw new UnsupportedOperationException("Not yet implemented");
        }
    ?

  6. #6
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    Here is the error

    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  7. #7
    fishtoprecords's Avatar
    fishtoprecords is offline Senior Member
    Join Date
    Jun 2008
    Posts
    571
    Rep Power
    7

    Default

    Quote Originally Posted by booter4429 View Post
    Here is the error
    Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException
    Set a debugging breakpoing where you call the code, and see what is null.

    In all cases, a null pointer exception is a programmer error. You are the programmer, fix the bug. Don't pass a null.

    The event queue stuff can be a bit of a challenge, if you get really stuck, put a bunch of PMD in your code and see where it stops.

    PMD is Poor Man's Debugger, aka System.out.println(....

  8. #8
    booter4429 is offline Member
    Join Date
    Jul 2008
    Posts
    20
    Rep Power
    0

    Default

    OK I will debug it and see what it is going on.

    thanks for the help

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,371
    Blog Entries
    1
    Rep Power
    20

    Default

    First thing, please use an apropriate tittle when you asking a question. It's really valid to you get more answers, and at the same time it's really helpful to all other members who are looking at your thread.

    Next, as fishtoprecords says NullPointerExceptions are by programmers mistake. So you have to take care of it. Use break points and see where it takes a null value. So basically, try all possibilities first. Debugging is the major way.

    And also as much as possible be specific with the question. Reading a large code and answering can be really wired to others. :)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •