I am having an issue where with my program and reading all the posts on your site has been VERY useful in understanding this project thank you! Now for my issue i am having trouble getting the buttons to recognize the class provided for them. For instance (i use Netbeans) an i am getting an error for the Last, add, modify and so on. the only button that does not have an error is the previous button, I can not find what i screwed up! here is what i have been working with

package javaapplication14;

import java.awt.*;  // import the java awt package
import java.awt.event.*;  // Import the java event package
import javax.swing.*; // Import the java swing package

public class VhsInventory {

    // main method
    public static void main(String[] args) {

        System.out.println("My Crappy VHS collection\n");

        Movie vhs = null;
        Inventory inventory = new Inventory();

        vhs = new Movie(1, "The Grinch", 2, 3f, "Comedy");
        System.out.println(vhs);
        inventory.addMovie(vhs);

        vhs = new Movie(2, "Free Willy", 2, 1.50f, "Drama");
        System.out.println(vhs);
        inventory.addMovie(vhs);

        vhs = new Movie(3, "Die Hard", 3, 5f, "Action");
        System.out.println(vhs);
        inventory.addMovie(vhs);

        vhs = new Movie(4, "A Christmas Carol", 4, 4f, "Drama");
        inventory.addMovie(vhs);
        System.out.println(vhs);

        vhs = new Movie(5, "The Mask", 3, 2.95f, "Comedy");
        System.out.println(vhs);
        inventory.addMovie(vhs);

        inventory.printInventory();
        new InventoryGUI(inventory);
    } // end main
} // end

class VHS {

    private int itemNo;
    private String title;
    private int inStock;
    private double unitPrice;

    VHS(int itemNo, String title, int inStock, double 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 double getUnitPrice() {
        return unitPrice;
    }

    public double value() {
        return inStock * unitPrice;
    }

    @Override
    public String toString() {
        return String.format("itemNo=%2d   title=%-22s   inStock=%3d   price=$%7.2f   value=$%8.2f",
                itemNo, title, inStock, unitPrice, value());
    }
} // end

class Inventory {
    // Setup of array to hold 30 movies

    private final int INVENTORY_SIZE = 30;
    private VHS[] items;
    private int numItems;

    Inventory() {
        items = new Movie[INVENTORY_SIZE];
        numItems = 0;
    }

    public int getNumItems() {
        return numItems;
    }

    public VHS getVHS(int n) {
        return items[n];
    }

    // Adds a Movie
    public void addMovie(VHS item) {
        items[numItems] = item;
        ++numItems;
    }

    // Loop through
    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 My Crappy VHS Collection\n");

        // If no items were found
        if (numItems <= 0) {
            System.out.println("No Movies avalible.\n");
        } else {
            for (int i = 0; i < numItems; i++) {
                System.out.printf("%3d   %s\n", i, items[i]);
            }
            System.out.printf("\n Inventory value is $%,.2f\n\n", value());
        }
    }
} // end

class Movie extends VHS {
    // Holds movie Genre and restocking fee

    public String genre;

    // Constructor
    public Movie(int MovieID, String itemName, int quantityOnHand, double itemPrice, String genre) {
        super(MovieID, itemName, quantityOnHand, itemPrice);
        this.genre = genre;
    }

    // To set the genre manually
    public void setgenre(String genre) {
    }

    // Get the genre of this Movie
    public String getMoviegenre() {
        return genre;
    }

    @Override
    public double value() {
        return super.value() + restockingFee();
    }

    // 5% restocking fee
    public double restockingFee() {
        return super.value() * 0.05f;
    }
} // end


// GUI for the Inventory
class Vhslogo extends JPanel {

    @Override
    public void paintComponent(Graphics g) {
        super.paintComponent(g); // call paintComponent

        // draws larger 3d rectangle
        g.setColor(Color.red);
        g.draw3DRect(45, 15, 180, 40, true);

        // draws small black square
        g.setColor(Color.BLACK);
        g.draw3DRect(5, 15, 40, 40, true);
        g.fill3DRect(5, 15, 40, 40, false);

        // draws company name
        g.setColor(Color.BLACK);
        g.setFont(new Font("Arial", Font.BOLD, 25));
        g.drawString("Crappy VHS Inventory", 10, 45);

    } // end paintComponent
} // end class

class InventoryGUI extends JFrame {

    private Inventory theInventory;
    private int index = 1;

    // GUI elements to display currently selected VHS information
    private final JLabel itemNumberLabel = new JLabel("Number:");
    private JTextField itemNumberText;
    private final JLabel prodnameLabel = new JLabel("Title:");
    private JTextField prodnameText;
    private final JLabel prodpriceLabel = new JLabel("Price:  $");
    private JTextField prodpriceText;
    private final JLabel numinstockLabel = new JLabel("  Avalibable:");
    private JTextField numinstockText;
    private final JLabel valueLabel = new JLabel("  Total Price:");
    private JTextField valueText;
    private final JLabel restockingFeeLabel = new JLabel("  Restocking Fee:");
    private JTextField restockingFeeText;
    private final JLabel totalValueLabel = new JLabel(" Total Value:");
    private JTextField totalValueText;
    private JPanel centerPanel;
    private JPanel buttonPanel;

    // constructor for the GUI
    InventoryGUI(Inventory inventory) {
        super ("My Crappy VHS Collection");
        Vhslogo jpanel = new Vhslogo();
        final Dimension dim = new Dimension(140, 60);
        final FlowLayout flo = new FlowLayout(FlowLayout.LEFT);
        JPanel jp;

        theInventory = inventory;

        // setup the GUI

        // product information
        // setup a panel to collect all the components.
        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);

        JButton previousButton = new JButton("Back");
        previousButton.addActionListener(new PreviousButtonHandler());
        buttonPanel.add(previousButton);


        JButton lastButton = new JButton("Last");
        lastButton.addActionListener(new LastButtonHandler());
        buttonPanel.add(lastButton);

        JButton addButton = new JButton("Add");
        addButton.addActionListener(new AddButtonHandler());
        buttonPanel.add(addButton);

        JButton deleteButton = new JButton("Delete");
        deleteButton.addActionListener(new DeleteButtonHandler());
        buttonPanel.add(deleteButton);

        JButton modifyButton = new JButton("Modify");
        modifyButton.addActionListener(new ModifyButtonHandler());
        buttonPanel.add(modifyButton);

        JButton saveButton = new JButton("Save");
        saveButton.addActionListener(new SaveButtonHandler());
        buttonPanel.add(saveButton);

        JButton findButton = new JButton("Find");
        findButton.addActionListener(new findButtonHandler());
        buttonPanel.add(findButton);


        centerPanel.add(buttonPanel);
        jp = new JPanel(flo);
        itemNumberLabel.setPreferredSize(dim);
        jp.add(itemNumberLabel);
        itemNumberText = new JTextField(3);
        itemNumberText.setEditable(false);
        jp.add(itemNumberText);
        centerPanel.add(jp);

        jp = new JPanel(flo);
        prodnameLabel.setPreferredSize(dim);
        jp.add(prodnameLabel);
        prodnameText = new JTextField(12);
        prodnameText.setEditable(false);
        jp.add(prodnameText);
        centerPanel.add(jp);

        jp = new JPanel(flo);
        prodpriceLabel.setPreferredSize(dim);
        jp.add(prodpriceLabel);
        prodpriceText = new JTextField(12);
        prodpriceText.setEditable(false);
        jp.add(prodpriceText);
        centerPanel.add(jp);

        jp = new JPanel(flo);
        numinstockLabel.setPreferredSize(dim);
        jp.add(numinstockLabel);
        numinstockText = new JTextField(5);
        numinstockText.setEditable(false);
        jp.add(numinstockText);
        centerPanel.add(jp);

        jp = new JPanel(flo);
        restockingFeeLabel.setPreferredSize(dim);
        jp.add(restockingFeeLabel);
        restockingFeeText = new JTextField(5);
        restockingFeeText.setEditable(false);
        jp.add(restockingFeeText);
        centerPanel.add(jp);

        jp = new JPanel(flo);
        valueLabel.setPreferredSize(dim);
        jp.add(valueLabel);
        valueText = new JTextField(17);
        valueText.setEditable(false);
        jp.add(valueText);
        centerPanel.add(jp);


        jp = new JPanel(flo);
        totalValueLabel.setPreferredSize(dim);
        jp.add(totalValueLabel);
        totalValueText = new JTextField(17);
        totalValueText.setEditable(false);
        jp.add(totalValueText);
        centerPanel.add(jp);

        // add the panel to the GUI display
        setContentPane(centerPanel);

        repaintGUI();

        setDefaultCloseOperation(EXIT_ON_CLOSE);
        setSize(560, 530);
        setResizable(false);
        setLocationRelativeTo(null);
        setVisible(true);
    }

    public void repaintGUI() {
        Movie temp = (Movie) theInventory.getVHS(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 FirstButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            index = 1;
            repaintGUI();
        }
    }

    class PreviousButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int numItems = theInventory.getNumItems();
            if (numItems != 0) {
                index = (--index) % numItems;
            }

            if (index < 0) {
                index = numItems - 1;
            }

            if (numItems == 0) // catches for last entry not needed because we add a blank entry at zero but just in case
            {
                JOptionPane.showMessageDialog(null, "That's the last entry.", "Please try again", JOptionPane.ERROR_MESSAGE);
            }

            repaintGUI();
        }
    

        class NextButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (++index) % numItems;
                repaintGUI();
            }
            }

        class LastButtonHandler implements ActionListener{

            public void actionPerformed(ActionEvent e) {
                int numItems = theInventory.getNumItems();
                index = (numItems - 1) % numItems;
                repaintGUI();
            }
            }


        class AddButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                repaintGUI();
            }
        }

        class SaveButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                repaintGUI();
            }
        }

        class ModifyButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                repaintGUI();
            }

        }
        class DeleteButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                repaintGUI();
            }
        }

        class findButtonHandler implements ActionListener {

            public void actionPerformed(ActionEvent e) {
                repaintGUI();
            }
        }
    }
}

Recommended Answers

All 2 Replies

You missing closing bracket on PreviousButtonHandler class

class PreviousButtonHandler implements ActionListener {

        public void actionPerformed(ActionEvent e) {
            int numItems = theInventory.getNumItems();
            if (numItems != 0) {
                index = (--index) % numItems;
            }

            if (index < 0) {
                index = numItems - 1;
            }

            if (numItems == 0) // catches for last entry not needed because we add a blank entry at zero but just in case
            {
                JOptionPane.showMessageDialog(null, "That's the last entry.", "Please try again", JOptionPane.ERROR_MESSAGE);
            }

            repaintGUI();
        }
    }

and after adding this one you will need to delete one at the end of the code (end of VhsInventory class). Look like while you been adding new handlers you placed them directly in PreviousButtonHandler class.

PS: You should also start putting classes in they own files. Code is growing and difficult to maintain

I think you should look at all of your brackets, most of your classes are contained within the PreviousButton class or whatever it is called, so get counting :)

Chris

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.