Java Inventory Program Problem!!!Please HELP!

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • pinkf24
    New Member
    • Jan 2008
    • 2

    #1

    Java Inventory Program Problem!!!Please HELP!

    I cannot figure out how to add the following:
    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\invento ry.dat file.
    Use exception handling to create the directory and file if necessary.
    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.

    Any help would be greatly appriciated I have been working on this for 8 hours!
    Here is my code:
    import java.util.Array s;
    import java.awt.*;
    import java.awt.event. *;
    import javax.swing.*;
    import javax.swing.JLa bel;

    class logo extends JPanel
    {
    public logo()
    {
    super(new GridLayout(1,1) );
    JLabel label1;
    ImageIcon icon = new ImageIcon("C:/cdlogo.jpg");
    label1 = new JLabel(icon);
    label1.setVerti calTextPosition (JLabel.BOTTOM) ;
    label1.setHoriz ontalTextPositi on(JLabel.CENTE R);
    add(label1);//add label
    }
    }//end class logo

    public class Inventory5 {


    public static void main(String[] args) {

    Album cd = null;
    Inventory inventory = new Inventory();

    cd = new Album(1, "Songs About Jane", 3, 14.99f, 2003);
    System.out.prin tln(cd);
    inventory.addAl bum(cd);

    cd = new Album(2, "30 Seconds to Mars", 5, 10.99f, 2005);
    System.out.prin tln(cd);
    inventory.addAl bum(cd);

    cd = new Album(3, "Mars Volta", 2, 13.95f, 1995);
    System.out.prin tln(cd);
    inventory.addAl bum(cd);

    cd = new Album(4, "The Mamas and The Papas", 9, 18.99f, 1970);
    inventory.addAl bum(cd);
    System.out.prin tln(cd);

    cd = new Album(5, "Idol Greatest Hits", 7, 15.98f, 2006);
    System.out.prin tln(cd);
    inventory.addAl bum(cd);

    inventory.print Inventory();
    new InventoryGUI(in ventory);
    } // end main

    } // end Inventory5


    class CD {
    private int itemNo;
    private String title;
    private int inStock;
    private float unitPrice;

    CD(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());
    }

    } // end CD


    class Inventory {
    // Array of CDs (set it to hold 25 items)
    private final int INVENTORY_SIZE = 25;
    private CD[] items;
    private int numItems;

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

    public int getNumItems() {
    return numItems;
    }

    public CD getCD(int n) {
    return items[n];
    }


    public void addAlbum(CD 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.prin tln("\nAmanda’s DVD Inventory\n");

    if (numItems <= 0) {
    System.out.prin tln("Inventory is empty at the moment.\n");
    } else {
    for (int i = 0; i < numItems; i++)
    System.out.prin tf("%3d %s\n", i, items[i]);
    System.out.prin tf("\nTotal value in inventory is $%,.2f\n\n", value());
    }
    }

    } // end Inventory



    class Album extends CD {

    private int albumYear;

    public Album(int AlbumID, String itemName, int quantityOnHand, float itemPrice, int year) {
    super(AlbumID, itemName, quantityOnHand, itemPrice);

    this.albumYear = albumYear;
    }


    public void setYear(int year) {
    albumYear = year;
    }

    public int getAlbumYear() {
    return albumYear;
    }

    public float value() {
    return super.value() + restockingFee() ;
    }

    public float restockingFee() {
    return super.value() * 0.05f;
    }

    } // end Album


    // GUI for inventory
    class InventoryGUI extends JFrame
    {
    // access CD Collection inventory
    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 restockingFeeLa bel = new JLabel(" Restocking Fee:");
    private JTextField restockingFeeTe xt;

    private final JLabel totalValueLabel = new JLabel(" Inventory Total Value:");
    private JTextField totalValueText;

    private JPanel centerPanel;
    private JPanel buttonPanel;


    InventoryGUI(In ventory inventory) {
    super("Amanda’s Movie Inventory");
    final Dimension dim = new Dimension(140, 20);
    final FlowLayout flo = new FlowLayout(Flow Layout.LEFT);
    JPanel jp;

    theInventory = inventory;



    centerPanel = new JPanel();
    centerPanel.set Layout(new BoxLayout(cente rPanel, BoxLayout.Y_AXI S));

    buttonPanel = new JPanel();

    JButton firstButton = new JButton("First" );
    firstButton.add ActionListener( new FirstButtonHand ler());
    buttonPanel.add (firstButton);

    JButton previousButton = new JButton("Previo us");
    previousButton. addActionListen er(new PreviousButtonH andler());
    buttonPanel.add (previousButton );

    JButton nextButton = new JButton("Next") ;
    nextButton.addA ctionListener(n ew NextButtonHandl er());
    buttonPanel.add (nextButton);

    JButton lastButton = new JButton("Last") ;
    lastButton.addA ctionListener(n ew LastButtonHandl er());
    buttonPanel.add (lastButton);

    JButton addButton = new JButton("Add");
    addButton.addAc tionListener(ne w AddButtonHandle r());
    buttonPanel.add (addButton);

    JButton deleteButton = new JButton("Delete ");
    deleteButton.ad dActionListener (new DeleteButtonHan dler());
    buttonPanel.add (deleteButton);

    JButton modifyButton = new JButton("Modify ");
    modifyButton.ad dActionListener (new ModifyButtonHan dler());
    buttonPanel.add (modifyButton);

    JButton saveButton = new JButton("Save") ;
    saveButton.addA ctionListener(n ew SaveButtonHandl er());
    buttonPanel.add (saveButton);

    JButton searchButton = new JButton("Search ");
    searchButton.ad dActionListener (new SearchButtonHan dler());
    buttonPanel.add (searchButton);


    centerPanel.add (buttonPanel);

    jp = new JPanel(flo);
    itemNumberLabel .setPreferredSi ze(dim);
    jp.add(itemNumb erLabel);
    itemNumberText = new JTextField(3);
    itemNumberText. setEditable(fal se);
    jp.add(itemNumb erText);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    prodnameLabel.s etPreferredSize (dim);
    jp.add(prodname Label);
    prodnameText = new JTextField(17);
    prodnameText.se tEditable(false );
    jp.add(prodname Text);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    prodpriceLabel. setPreferredSiz e(dim);
    jp.add(prodpric eLabel);
    prodpriceText = new JTextField(17);
    prodpriceText.s etEditable(fals e);
    jp.add(prodpric eText);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    numinstockLabel .setPreferredSi ze(dim);
    jp.add(numinsto ckLabel);
    numinstockText = new JTextField(5);
    numinstockText. setEditable(fal se);
    jp.add(numinsto ckText);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    restockingFeeLa bel.setPreferre dSize(dim);
    jp.add(restocki ngFeeLabel);
    restockingFeeTe xt = new JTextField(17);
    restockingFeeTe xt.setEditable( false);
    jp.add(restocki ngFeeText);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    valueLabel.setP referredSize(di m);
    jp.add(valueLab el);
    valueText = new JTextField(17);
    valueText.setEd itable(false);
    jp.add(valueTex t);
    centerPanel.add (jp);

    jp = new JPanel(flo);
    totalValueLabel .setPreferredSi ze(dim);
    jp.add(totalVal ueLabel);
    totalValueText = new JTextField(17);
    totalValueText. setEditable(fal se);
    jp.add(totalVal ueText);
    centerPanel.add (jp);

    // adds panel to display
    setContentPane( centerPanel);

    repaintGUI();

    setDefaultClose Operation(EXIT_ ON_CLOSE);
    setSize(420, 480);
    setResizable(fa lse);
    setLocationRela tiveTo(null);
    setVisible(true );
    }

    public void repaintGUI() {
    Album temp = (Album) theInventory.ge tCD(index);

    if (temp != null) {
    itemNumberText. setText("" + temp.getItemNo( ));
    prodnameText.se tText(temp.getT itle());
    prodpriceText.s etText(String.f ormat("$%.2f", temp.getUnitPri ce()));
    restockingFeeTe xt.setText(Stri ng.format("$%.2 f", temp.restocking Fee()));
    numinstockText. setText("" + temp.getInStock ());
    valueText.setTe xt(String.forma t("$%.2f", temp.value()));
    }
    totalValueText. setText(String. format("$%.2f", theInventory.va lue()));
    }

    class FirstButtonHand ler implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    index = 0;
    repaintGUI();
    }
    }

    class PreviousButtonH andler implements ActionListener {
    public void actionPerformed (ActionEvent e){
    int numItems = theInventory.ge tNumItems();
    index--;
    if (index < 0)
    index = numItems - 1;
    repaintGUI();
    }
    }

    class NextButtonHandl er implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    int numItems = theInventory.ge tNumItems();
    index = (++index) % numItems;
    repaintGUI();
    }
    }
    class LastButtonHandl er implements ActionListener {
    public void actionPerformed (ActionEvent e) {
    int numItems = theInventory.ge tNumItems();
    index = (numItems -1) % numItems;
    repaintGUI();

    }
    }

    class AddButtonHandle r implements ActionListener {
    public void actionPerformed (ActionEvent e){
    int numItems = theInventory.ge tNumItems();
    index--;
    if (index > 5)
    index = numItems + 6;
    repaintGUI();
    }
    }


    class SaveButtonHandl er implements ActionListener {
    public void actionPerformed (ActionEvent e){
    repaintGUI();
    }
    }

    class ModifyButtonHan dler implements ActionListener {
    public void actionPerformed (ActionEvent e){
    repaintGUI();
    }
    }

    class DeleteButtonHan dler implements ActionListener {
    public void actionPerformed (ActionEvent e){
    repaintGUI();
    }
    }

    class SearchButtonHan dler implements ActionListener {
    public void actionPerformed (ActionEvent e){
    repaintGUI();
    }
    }

    } // End InventoryGUI class

    I am sorry this is my first time posting a question so if i did it wrong please let me know. I just need some examples to help me start!
  • pinkf24
    New Member
    • Jan 2008
    • 2

    #2
    Nevermind I finally got it!!

    Comment

    • dlb53
      New Member
      • Feb 2008
      • 3

      #3
      I dont get it - can you explain?

      Comment

      Working...