Results 1 to 3 of 3
  1. #1
    huggy11911 is offline Member
    Join Date
    Jun 2012
    Posts
    1
    Rep Power
    0

    Default Button Help in Inventory Program

    Hey everyone I am having some trouble with this program I am trying to make. It is just a simple inventory program and I want to make it so it has a add button, a delete button and a modify button that allows the user to perform the actions on the item name, the item number and the number of units that are in stock for each item.

    I also want to have a save button and I want it to save the inventory to a file: C:\data\inventory.dat. And the application must use exception handling to create a directory and file if necessary when the inventory is saved.

    I also want to include a search button that allows the user to search the inventory by the item name and once the search button is enabled, it either shows the product information or it tells the user that what was searched for wasn't found.

    I have searched all over the place to figure this out. I have a Java book and it really doesn't help at all. Everything I have found just gives me errors and its a total mess. I have no clue where to start or even how to do any of this. Can anyone please help me out or at least guide me in the right direction? Like I said I completely suck at Java and I don't like Java at all and I have no idea what to do. There is some code I added in but none of it seems to work and I don't know where everything is suppose to go, I am having bad difficulties I need a lot of help!! Please help!!

    Here is the code I have so far:

    ButtonHandler Class

    Java Code:
    
    package it215week7;
    
    import java.awt.FlowLayout;
    import java.awt.event.ActionListener;
    import java.awt.event.ActionEvent;
    import javax.swing.JFrame;
    import javax.swing.JButton;
    import javax.swing.Icon;
    import javax.swing.ImageIcon;
    import javax.swing.JOptionPane;
    
    public class ButtonHandler implements ActionListener {
    
        InventoryProgramPart4 inventory;
        //HM variable to track displayed item
        int itemDisplayed = 0;
    
        public ButtonHandler(InventoryProgramPart4 cdInventory) {
            this.inventory = cdInventory;
    
        }
    
        //HM get and set methods for the displayed item
        public int getItemDisplayed() {
            return itemDisplayed;
        }
    
        public void setItemDisplayed(int itemDisplayed) {
            this.itemDisplayed = itemDisplayed;
        }
    
        public void actionPerformed(ActionEvent event) {
    
            //HM comparison between objects is done by the equals method not ==
            if (event.getActionCommand().equals("Next")) {//creates event for previous item
                itemDisplayed++;//makes item display add one every time until it reaches 4
                if (itemDisplayed>=4) {
                    itemDisplayed=0;
                }
                inventory.paint(itemDisplayed);//displays the item
            }
            if (event.getActionCommand().equals("Previous")){//Creates event for previous button
                itemDisplayed--;
                if (itemDisplayed<0) {
                    itemDisplayed=3;
                }
                inventory.paint(itemDisplayed);//displays the item
            }
            if (event.getActionCommand().equals("First Product")){//creates the event for first product button
                itemDisplayed = 0;//displays the 0 item in Array
                inventory.paint(itemDisplayed);//displays the item
            }
            
            if (event.getActionCommand().equals("Last Product")){//creates the event for the last product button
               itemDisplayed=3;//displays the 4th item in Array
               inventory.paint(itemDisplayed);//displays the item
            }
        }
    }
    CDProduct

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    package it215week7;
    
    /**
     * A class that implements the product class for the CD product.
     *
     
     */
    public class CDProduct {
    
        private String name;
        private int itemNum;
        private int items;
        private double price;
    
        public int getItemNum() {
            return itemNum;
        }
    
        public void setItemNum(int itemNum) {
            this.itemNum = itemNum;
        }
    
        public int getItems() {
            return items;
        }
    
        public void setItems(int items) {
            this.items = items;
        }
    
        public String getName() {
            return name;
        }
    
        public void setName(String name) {
            this.name = name;
        }
    
        public double getPrice() {
            return price;
        }
    
        public void setPrice(double price) {
            this.price = price;
        }
    
        /**
         * Default constructor
         */
        public CDProduct() {
    
        }//end default constructor
    
        /**
         * Constructor with parameters
         *
         * @param name
         * @param itemNum
         * @param items
         * @param price
         */
        public CDProduct(String name, int itemNum, int items, double price) {
            this.name = name;
            this.itemNum = itemNum;
            this.items = items;
            this.price = price;
        }//end constructor
    
        public double getInventoryValue() {
            return (items * price);
        }
    
        public String toString() {
            return "Name:" + name + " Item:" + itemNum + " Items:" + items + " Price:" + price + " Value:" + getInventoryValue();
        }
    }
    ExtendedCDProduct

    Java Code:
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package it215week7;
    
    /**
     * Extends the CDProduct class and adds the genre attribute
     *
     */
    public class ExtendedCDProduct extends CDProduct {
    
        String genre;
        double restockingFee=0.05;
    
        public ExtendedCDProduct(String name, int itemNum, int items, double price, String genre) {
            super(name,itemNum,items,price);
            this.genre = genre;
        }//end constructor
    
        public String getGenre() {
            return genre;
        }
    
        public void setGenre(String genre) {
            this.genre = genre;
        }
    
        @Override
        public String toString() {
            return super.toString()+" Genre:"+getGenre();
        }
    
        @Override
        public double getInventoryValue() {
            double inventoryValue = super.getInventoryValue();
            //add the restocking fee
            inventoryValue = (1+restockingFee)*inventoryValue;
            return inventoryValue;
        }
    
    }
    Inventory Class

    Java Code:
    
    package it215week7;
    
    
    public class Inventory {
    
        CDProduct[] inventory = new CDProduct[10];
        int current = 0;
    
        public Inventory(int len) {
            inventory = new CDProduct[len];
        }
    
        /**
         * Add a CD to the inventory.
         * @param cd
         * @throws Exception
         */
        public void addCDProduct(CDProduct cd) throws Exception {
            if (current < 10) {
                inventory[current++]=cd;
            }else {
                throw new Exception("Inventory is full. No items can be added.");
            }
        }
        /**
         * sorts the array by cd name.
         */
        public void sort() {
            int len = current; //current length of the array
            for (int i=0; i<len;i++) {
                for(int j=i;j<len;j++) {
                    if (inventory[i].getName().compareToIgnoreCase(inventory[j].getName())>0) {
                        CDProduct temp = inventory[j];
                        inventory[j] = inventory[i];
                        inventory[i] = temp;
                    }
                }
            }
        }
        /**
         * Get the total inventory
         * @return
         */
        public double calculateTotalInventory() {
            double totalValue = 0;
            for(int i=0;i<current;i++) {
                CDProduct cd = inventory[i];
                totalValue += cd.getInventoryValue();
            }
            return totalValue;
        }
        /**
         * Gets the number of items in the inventory
         * @return
         */
        public int getNumberOfItems() {
            return current;
        }
    
        public CDProduct getItem(int index) {
            return inventory[index];
        }
    
        public void printInventory() {
            System.out.println("Current Inventory:");
            for(int i=0;i<current;i++) {
                System.out.println(inventory[i]);
            }
            System.out.println("The total value of the inventory is:"+calculateTotalInventory());
        }
    
    }
    InventoryProgramPart4 Class

    Java Code:
    package it215week7;
    
    import java.awt.*;
    import java.awt.GridLayout;
    import javax.swing.JFrame;
    import javax.swing.JLabel;
    import javax.swing.JPanel;
    import javax.swing.JTextField;
    import java.awt.event.ActionEvent;
    import java.awt.event.ActionListener;
    import java.net.URL;
    import javax.swing.JButton;
    import java.util.Arrays;
    import javax.swing.ImageIcon;
    import javax.swing.Icon;
    
    
    
    public class InventoryProgramPart4 extends JFrame {  
    
    
        Inventory cdInventory; //Creates the CD inventory
        // GUI elements for displaying information on the currently-selected items
        private final JLabel nameLabel = new JLabel(" CD Name: ");
        private JTextField nameText;
        private final JLabel numberLabel = new JLabel(" Item Number: ");
        private JTextField numberText;
        private final JLabel genreLabel = new JLabel(" Genre: ");
        private JTextField genreText;
        private final JLabel priceLabel = new JLabel(" Price: ");
        private JTextField priceText;
        private final JLabel quantityLabel = new JLabel(" Quantity: ");
        private JTextField quantityText;
        private final JLabel valueLabel = new JLabel(" Value: ");
        private JTextField valueText;
        private final JLabel totalValueLabel = new JLabel(" Inventory Total: ");
        private JLabel totalValueText;
        
       
    
         
         private JButton nextButton = new JButton();//creates next button
         private JButton prevButton = new JButton();//creates previous button
         private JButton firstButton = new JButton();//creates first item button
         private JButton lastButton = new JButton();//creates last item button
         
    
    
        public InventoryProgramPart4() {
    
    
        }
    
        public void initialize() {
    
            JPanel centerPanel = new JPanel(new GridLayout(8, 2, 0, 4)); //Panel for collecting all the components.
    
            centerPanel.add(nameLabel);
            nameText = new JTextField("");
            nameText.setEditable(false);
            centerPanel.add(nameText);
    
            centerPanel.add(numberLabel);
            numberText = new JTextField("");
            numberText.setEditable(false);
            centerPanel.add(numberText);
    
            centerPanel.add(genreLabel);
            genreText = new JTextField("");
            genreText.setEditable(false);
            centerPanel.add(genreText);
    
            centerPanel.add(priceLabel);
            priceText = new JTextField("");
            priceText.setEditable(false);
            centerPanel.add(priceText);
    
            centerPanel.add(quantityLabel);
            quantityText = new JTextField("");
            quantityText.setEditable(false);
            centerPanel.add(quantityText);
    
            centerPanel.add(valueLabel);
            valueText = new JTextField("");
            valueText.setEditable(false);
            centerPanel.add(valueText);
    
            //The following adds information on the whole inventory to the panel
            centerPanel.add(totalValueLabel);
            totalValueText = new JLabel("");
            centerPanel.add(totalValueText);
            
            ImageIcon image = new ImageIcon("dvd_cd_logos.jpeg");//declares the location for the icon
            JLabel picture = new JLabel(image);//creates new JLabel
            centerPanel.add(picture);//adds the picture to the centerPanel
                            
    
            
             JPanel buttonPanel = new JPanel();//Creates new JPanel
             nextButton.setText("Next");//sets text for next button
             nextButton.setActionCommand("Next");//sets the command for the next button
             
             nextButton.addActionListener(new ButtonHandler(this));//adds button handler to the button
             buttonPanel.add(nextButton);//adds next button to buttonPanel
    
            getContentPane().add(buttonPanel, BorderLayout.NORTH); 
            getContentPane().add(centerPanel, BorderLayout.CENTER); //Panel is added to the center of the GUI
    
            prevButton.setText("Previous Button");//Sets text for previous button
            prevButton.setActionCommand("Previous");//Sets the command for the Previous button
            prevButton.addActionListener(new ButtonHandler(this));//adds ButtonHanlder to the button
            buttonPanel.add(prevButton);//adds previous button to buttonPanel
            
            
            getContentPane().add(buttonPanel, BorderLayout.EAST);
            getContentPane().add(centerPanel, BorderLayout.CENTER);//Panel is added to the center of the GUI
            
            firstButton.setText("First Item");//Sets First Item Text
            firstButton.setActionCommand("First Product");//Sets command for the button
            firstButton.addActionListener(new ButtonHandler(this));//Adds buttonHandler to the button
            buttonPanel.add(firstButton);//Adds the firstButton to the buttonPanel
            
            getContentPane().add(buttonPanel, BorderLayout.SOUTH);
            getContentPane().add(centerPanel, BorderLayout.CENTER);//Panel is added to the center of the GUI
            
            lastButton.setText("Last Item");//Sets Last Item Text
            lastButton.setActionCommand("Last Product");//Sets command for the Last Item
            lastButton.addActionListener(new ButtonHandler(this));//Adds ButtonHandler to the button
            buttonPanel.add(lastButton);//Adds lastButton to the buttonPanel
            
            getContentPane().add(buttonPanel, BorderLayout.NORTH);
            getContentPane().add(centerPanel, BorderLayout.CENTER);//Panel is added to the Center of GUI
            
            int index = 0; //Integer index is set to 0
            
            //Add the logo to the GUI
            URL url = this.getClass().getResource("dvd_cd_logos.jpg");
            Image img = Toolkit.getDefaultToolkit().getImage(url);
            Image scaledImage = img.getScaledInstance(100, 50, Image.SCALE_AREA_AVERAGING); // scale logo to fit GUI
    
            // create JLabel with logo as label's icon
            Icon logoIcon = new ImageIcon(scaledImage);
            JLabel companyLogoLabel = new JLabel(logoIcon);
            getContentPane().add(companyLogoLabel, BorderLayout.WEST); // add logo to GUI
            
            
    
            
            
            
            
            paint(0);
        }
    
        public void paint(int index)//This repaints the GUI with new information about the product
        {
            ExtendedCDProduct temp = (ExtendedCDProduct) cdInventory.getItem(index);
    
            if (temp != null) {
                nameText.setText(temp.getName());
                numberText.setText("" + temp.getItemNum());
                genreText.setText(temp.getGenre());
                priceText.setText(String.format("$%.2f", temp.getPrice()));
                quantityText.setText("" + temp.getItems());
                valueText.setText(String.format("$%.2f", temp.getInventoryValue()));
            }
    
            totalValueText.setText(String.format("$%.2f", cdInventory.calculateTotalInventory()));
    
        }
    
        public Inventory getCdInventory() {
            return cdInventory;
        }
    
        public void setCdInventory(Inventory cdInventory) {
            this.cdInventory = cdInventory;
        }
    
    
    
    
        public static void main(String args[]) {
    
            InventoryProgramPart4 productGUI = new InventoryProgramPart4(); //This GUI object holds up to 4 CDs
            //Create the inventory
            Inventory cdInventory = new Inventory(4);
            try {
            //The following adds items to the inventory
            cdInventory.addCDProduct(new ExtendedCDProduct("Notting Hill", 1, 20, 9.95, "Soundtrack"));
            cdInventory.addCDProduct(new ExtendedCDProduct("In Your Dreams ", 2, 20, 9.95, "Classic Rock"));
            cdInventory.addCDProduct(new ExtendedCDProduct("Back in Black", 3, 20, 10.95, "Hard Rock"));
            cdInventory.addCDProduct(new ExtendedCDProduct("The Three Kinds", 4, 20, 19.95, "Blues"));
            }catch(Exception e) {
                System.out.println("No more space to add items to inventory");
            }
            //Sort the inventory
            cdInventory.sort();
    
            productGUI.setCdInventory(cdInventory);
    
            //The following makes the GUI appear on-screen
            productGUI.initialize();
            productGUI.setDefaultCloseOperation(EXIT_ON_CLOSE);
            productGUI.pack();
            productGUI.setSize(600, 275);
            productGUI.setResizable(false);
            productGUI.setLocationRelativeTo(null);
            productGUI.setVisible(true);
    
            return;
    
        }
    
    }
    And I have the image file in there too, but I'm not to worried about that right now. I have the image working just fine. Just all of the others I am worried about. I can't get it to work at all no matter what I do. Please help!!!

    Thank you!!

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

    Default Re: Button Help in Inventory Program

    Quote Originally Posted by huggy11911 View Post
    Hey everyone I am having some trouble with this program I am trying to make. It is just a simple inventory program and I want to make it so it has a add button, a delete button and a modify button that allows the user to perform the actions on the item name, the item number and the number of units that are in stock for each item.

    I also want to have a save button and I want it to save the inventory to a file: C:\data\inventory.dat. And the application must use exception handling to create a directory and file if necessary when the inventory is saved.

    I also want to include a search button that allows the user to search the inventory by the item name and once the search button is enabled, it either shows the product information or it tells the user that what was searched for wasn't found.
    OK, so we know your requirements.


    I have searched all over the place to figure this out. I have a Java book and it really doesn't help at all. Everything I have found just gives me errors and its a total mess. I have no clue where to start or even how to do any of this. Can anyone please help me out or at least guide me in the right direction? Like I said I completely suck at Java and I don't like Java at all and I have no idea what to do. There is some code I added in but none of it seems to work and I don't know where everything is suppose to go, I am having bad difficulties I need a lot of help!! Please help!!

    <deleted: one ton of code>
    Not much we can do with this unless you tell us specifically what errors you're seeing and what problems you're having. We're all volunteers who help on our free time and it is unlikely that anyone is going to go through a huge amount of code or be able to help if the question is, "here is my code, fix it for me". You've got to take the lead in helping us help you by pointing out the specific errors and problems else your forum experience will be very frustrating for you and us. By the way, is all the code above your code, or is any of it "found" code?

    Much luck.

  3. #3
    Tha Boss is offline Member
    Join Date
    Jun 2012
    Location
    ON, Canada.
    Posts
    25
    Rep Power
    0

    Default Re: Button Help in Inventory Program

    Wow, that's some messed up stuff bro, it seems you have problems reading and writing to external files and using button action listeners. All that massive amount of code you dumped there shows people nothing, no one has the amount of time to read through that thousands of lines of at least 4 or 5 different programs to figure out where your problem is... you may want to read the SSCCE link below Fubarable's post above. Post the small chunks that's giving you errors, or explain precisely what you can't figure out how to do.. you'll get much more luck that way..
    - Sincere Regards.

    E.Hughes™
    "I'm sometimes hard to please... because I'm only satisfied with the very best." - Fernand Point.

Similar Threads

  1. My Inventory Program
    By ladykrimson in forum New To Java
    Replies: 28
    Last Post: 11-01-2010, 09:06 PM
  2. Help with Inventory Program, Please
    By NewKidjava in forum New To Java
    Replies: 4
    Last Post: 08-21-2009, 11:09 AM
  3. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 01:30 AM
  4. Inventory Program
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-17-2009, 05:09 AM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 05:51 PM

Posting Permissions

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