Inventory program in Java

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hollywoood
    New Member
    • Dec 2006
    • 7

    Inventory program in Java

    I am trying to add the Delete button, save and search buttons. I have tried to call my teacher and he is absolutly no help and i have read and reread the text but still have no idea what is going on.........I have the added the add button but now i am stuck. Is there a simple way in completeing this??

    Please help

    • Due Date: Day 7 [Individual] forum
    • 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. • Post as an attachment
  • horace1
    Recognized Expert Top Contributor
    • Nov 2006
    • 1510

    #2
    you need to post the code you have so far - contributers can then comment on your problems

    Comment

    • hollywoood
      New Member
      • Dec 2006
      • 7

      #3
      I am totally lost in this class and i have no clue what is going on here. My teacher is no help and i cannot figure this out to save my life. I need to add a Delete button, a search button and be able to save it so that the added items will show up after i close it and reopen it.

      Any help would be greatly appriciated

      Code:
      import java.util.*;
      import javax.swing.*;
      import java.awt.event.*;
      import java.awt.*;
      
      class Product implements Comparable {
       String itemName;	 // class variable that stores the item name
       double itemNumber;	 // class variable that stores the item number
       long stockQuantity; // class variable that stores the quantity in stock
       double price;	 // class variable that stores the item price
      
       public Product() {
        	 itemName = "";
      	 itemNumber = 0.0;
      	 stockQuantity = 0L;
      	 price = 0.0;
      
       }
       public Product(String name, int number, long stockQuantity, double price) {
           this.itemName = name;
      	 this.itemNumber = number;
      	 this.stockQuantity = stockQuantity;
      	 this.price = price;
      	}
       public void setItemName(String name) {
        this.itemName = itemName;
       }
       public String getItemName() {
        return itemName;
       }
       public void setItemNumber(double number) {
        this.itemNumber = itemNumber;
       }
       public double getItemNumber() {
        return itemNumber;
       }
       public void setStockQuantity(long quantity) {
        stockQuantity = quantity;
       }
       public long getStockQuantity() {
        return stockQuantity;
       }
       public void setItemPrice(double price) {
        this.price = price;
       }
       public double getItemPrice() {
        return price;
       }
       public double calculateInventoryValue() {
        return getItemPrice() * getStockQuantity();
       }
       public int compareTo (Object o) {
        Product p = (Product)o;
        return itemName.compareTo(p.getItemName());
       }
       public String toString() {
        return "Title :"+getItemName() + "\nStock Number"+itemNumber+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
       }
      
      }
      
      class DVDTitle extends Product implements Comparable {
      private String rating;
      public DVDTitle() {
      super(); //Call the constructor in Product
      rating = ""; //Add the additonal attribute
      }
      public DVDTitle(String itemName, int itemNumber, long stockQuantity, double price, String rating) {
      super(itemName, itemNumber, stockQuantity, price); //Call the constructor in Product
      this.rating = rating; //Add the additonal attribute
      	}
      public void setRating(String rating) {
      this.rating = rating;
      }
      public String getRating() {
      return rating;
      }
      public double calculateInventoryValue() {
      return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05; //Had you forgotten to include the restocking fee?
      }
      //What happens when we want to change the restocking fee?
      public double calculateRestockFee() {
      return getItemPrice() * 0.05;
      }
      public int compareTo (Object o) {
      Product p = (Product)o;
      return getItemName().compareTo(p.getItemName());
      }
      public String toString() {
      return "Name :"+getItemName() + "\nNumber"+getItemNumber()+"\nPrice"+getItemPrice()+"\nQuantity"+getStockQuantity() +"\nRating :"+getRating()+"\nValue"+calculateInventoryValue();
      }
      }

      I will have to post the other part of the code to another post.

      Comment

      • hollywoood
        New Member
        • Dec 2006
        • 7

        #4
        Here is the other part of that code

        Code:
        public class Inventory1 extends JFrame implements ActionListener {
        //Utility class for displaying the picture
        //If we are going to use a class/method/variable inside that class only, we declare it private in that class
        private class MyPanel extends JPanel {
        	ImageIcon image = new ImageIcon("dvd.gif");
        	int width = image.getIconWidth();
        	int height = image.getIconHeight();
        	long angle = 30;
        	public MyPanel(){
        	 super();
        	}
        	public void paintComponent(Graphics g){
        	 super.paintComponent(g);
        	 Graphics2D g2d = (Graphics2D)g;
        	 //g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
        	 g2d.drawImage(image.getImage(), 60, 60, this);
        	 g2d.dispose();
        	}
        }//end class MyPanel
        
        int currentIndex; //Currently displayed Item
        Product[] supplies = new Product[4];
        JLabel itemName ;
        JLabel itemNumber;
        JLabel rating;
        JLabel quantity;
        JLabel price;
        JLabel fee;
        JLabel totalValue;
        JTextField itemNameField = new JTextField(20);
        JTextField itemNumberField = new JTextField(20);
        JTextField ratingField = new JTextField(20);
        JTextField quantityField = new JTextField(20);
        JTextField priceField = new JTextField(20);
        
        JPanel display;
        JPanel displayHolder;
        JPanel panel;
        public Inventory1() {
        makeTheDataItems();
        setSize(600, 500);
        setTitle("Jeremy's DVD Inventory Program");
        //make the panels
        display = new JPanel();
        JPanel other = new JPanel();
        JPanel picture = new MyPanel();
        JPanel buttons = new JPanel();
        JPanel centerPanel = new JPanel();
        displayHolder = new JPanel();
        display.setLayout(new GridLayout(5, 3));
        other.setLayout(new GridLayout(2, 1));
        //make the labels
        itemName = new	 JLabel("Name       :");
        itemNumber = new	 JLabel("Number       :");
        rating = new	 JLabel("Rating      :");
        quantity = new JLabel("Quantity       :");
        price = new	 JLabel("Price       :");
        fee = new		JLabel("Fee       :");
        totalValue = new JLabel("Total Value       :");
        //Use the utility method to make the buttons
        JButton first = makeButton("First");
        JButton next = makeButton("Next");
        JButton previous = makeButton("Previous");
        JButton last = makeButton("Last");
        JButton exit = makeButton("Exit");
        //Other buttons
        JButton add = makeButton("Add");
        
        //Add the labels to the display panel
        display.add(itemName);
        display.add(itemNumber);
        display.add(rating);
        display.add(quantity);
        display.add(price);
        display.add(fee);
        //add the buttons to the buttonPanel
        buttons.add(first);
        buttons.add(previous);
        buttons.add(next);
        buttons.add(last);
        buttons.add(exit);
        //Add the picture panel and display to the centerPanel
        displayHolder.add(display);
        centerPanel.setLayout(new GridLayout(2, 1));
        centerPanel.add(picture);
        centerPanel.add(displayHolder);
        other.add(buttons);
        JPanel forAdd = new JPanel(); // add the other buttons to this panel
        forAdd.add(add);
        other.add(forAdd);
        
        //Add the panels to the frame
        getContentPane().add(centerPanel, "Center");
        getContentPane().add(other, "South");
        this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
        setVisible(true);
        }
        private void makeTheDataItems () {
          Product p1 = new DVDTitle("XMEN-The Last Stand", 001, 200, 12.99, "PG-13");
          Product p2 = new DVDTitle("Harry Potter-The Goblet of Fire", 002, 50, 9.95, "PG-13");
          Product p3 = new DVDTitle("Jackass-Number Two", 003, 100, 19.95, "R");
          Product p4 = new DVDTitle("Pirates of the Caribbean", 004, 75, 9.99, "PG-13");
        
          supplies[0] = p1;
          supplies[1] = p2;
          supplies[2] = p3;
          supplies[3] = p4;
        }
        //Utility method for creating and dressing buttons
        private JButton makeButton(String label) {
        JButton button = new JButton(label);
        button.setActionCommand(label);
        button.addActionListener(this);
        return button;
        }
        private void addItem() {
        System.out.println("eeeeeeeeee");
        panel = new JPanel();
        JPanel add = new JPanel();
        add.setLayout(new GridLayout(2, 1));
        add.setLayout(new GridLayout(4, 4));
        JButton addIt = makeButton("Add Item");
        JLabel itemName = new JLabel("Name       :");
        //JLabel itemNumber = new JLabel("Number       :");
        JLabel rating = new JLabel("Rating       :");
        JLabel quantity = new JLabel("Quantity	:");
        JLabel price = new JLabel("Price	 :");
        add.add(itemName); add.add(itemNameField);
        //add.add(itemNumber); add.add(itemNumberField);
        add.add(rating); add.add(ratingField);
        add.add(quantity); add.add(quantityField);
        add.add(price); add.add(priceField);
        panel.add(add);
        JPanel forAddIt = new JPanel();
        forAddIt.add(addIt);
        panel.add(forAddIt);
        displayHolder.remove(display);
        displayHolder.add(panel);
        //display = panel;
        this.setVisible(true);
        }
        
        public static void main( String args[]) {
        Inventory1 object = new Inventory1(); //The main method should not have too much code
        } // end main method
        public void actionPerformed(ActionEvent event) {
        String command = event.getActionCommand(); //This retrieves the command that we set for the button
        //Always compare strings using the .equals method and not using ==
        if(command.equals("First")) {
        displayFirst();
        }
        else if(command.equals("Next")) {
        displayNext();
        }
        else if(command.equals("Previous")) {
        displayPrevious();
        }
        else if(command.equals("Last")) {
        displayLast();
        }
        else if(command.equals("Exit")) {
        this.dispose();
        System.exit(0);
        }
        else if(command.equals("Add")) {
        addItem();
        }
        else if(command.equals("Add Item")) {
        addItemToArray();
        }
        
        }
        private void addItemToArray() {
        Product p = new DVDTitle(itemNameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
        Double.parseDouble(priceField.getText()), ratingField.getText());
        //Extend size of array by one first
        Product[] ps = new Product[supplies.length + 1];
        for(int i = 0; i < ps.length-1; i++) {
        ps[i] = supplies[i];
        }
        ps[supplies.length] = p;
        supplies = ps;
        displayHolder.remove(panel);
        displayHolder.add(display);
        displayLast();
        this.setVisible(false);
        this.setVisible(true);
        }
        //Utility method to ease the typing and reuse code
        //This method reduces the number of lines of our code
        private void displayItemAt(int index) {
        DVDTitle product = (DVDTitle)supplies[index];
        itemName.setText("Item Name: "+ product.getItemName());
        itemNumber.setText("Item Number: "+ product.getItemNumber());
        rating.setText("Rating: "+ product.getRating());
        quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
        price.setText("Item Price: "+ product.getItemPrice());
        totalValue.setText("Total: " + product.calculateInventoryValue());
        fee.setText("Fee :"+product.calculateRestockFee());
        this.setVisible(true);
        }
        public void displayFirst() {
        displayItemAt(0);
        currentIndex = 0;
        }
        public void displayNext() {
        if(currentIndex == supplies.length-1) {
        displayFirst();
        currentIndex = 0;
        }
        else {
        displayItemAt(currentIndex + 1);
        currentIndex++;
        }
        }
        public void displayPrevious() {
        if(currentIndex == 0) {
        displayLast();
        currentIndex = supplies.length-1;
        }
        else {
        displayItemAt(currentIndex - 1);
        currentIndex--;
        }
        }
        public void displayLast() {
        displayItemAt(supplies.length-1);
        currentIndex = supplies.length-1;
        }
        }//end class Inventory2.1

        Comment

        • hollywoood
          New Member
          • Dec 2006
          • 7

          #5
          I have the add button working......I just need help with the other.

          Thanks

          Comment

          • stevedub
            New Member
            • Jan 2007
            • 40

            #6
            When I try to compile the Inventory1 class I get 87 errors. Why is this? I am also working on this assignment.

            Comment

            • r035198x
              MVP
              • Sep 2006
              • 13225

              #7
              Originally posted by stevedub
              When I try to compile the Inventory1 class I get 87 errors. Why is this? I am also working on this assignment.
              Did you import the required packages for GUI? Did you compile the Produvt class first?

              Comment

              • stevedub
                New Member
                • Jan 2007
                • 40

                #8
                I compiled the product and DVD class first, but I am not sure how to import the GUI components. This class is really getting difficult now, only two weeks left.

                Comment

                • r035198x
                  MVP
                  • Sep 2006
                  • 13225

                  #9
                  Originally posted by stevedub
                  I compiled the product and DVD class first, but I am not sure how to import the GUI components. This class is really getting difficult now, only two weeks left.
                  To import classes in the awt package simply put



                  import java.awt.*; at the top of the java file.



                  If you are still getting errors post the code you are using.

                  Comment

                  • stevedub
                    New Member
                    • Jan 2007
                    • 40

                    #10
                    Ok, I am still getting errors. The code I am trying is the above code, I am basically just trying to get an idea of how the program should look. I also tried to put the import at the top of the Inventory1 program, but I am getting the same errors.

                    Comment

                    • stevedub
                      New Member
                      • Jan 2007
                      • 40

                      #11
                      Ok, I got it to compile, I just needed to add a couple more imports at the top of the inventory program.

                      Comment

                      • r035198x
                        MVP
                        • Sep 2006
                        • 13225

                        #12
                        Originally posted by stevedub
                        Ok, I got it to compile, I just needed to add a couple more imports at the top of the inventory program.


                        I hope you understand all of it.

                        Comment

                        • stevedub
                          New Member
                          • Jan 2007
                          • 40

                          #13
                          Ok, after running the program I noticed that this is a step ahead of what I need. What I need to do is have that code display just a list. here is a link to what it is suppose to look like, we can pick whichever way to display, the top or bottom version . http://i88.photobucket.com/albums/k1...oryexample.jpg

                          If you can help at all with this I will forever be in debt.

                          Comment

                          • stevedub
                            New Member
                            • Jan 2007
                            • 40

                            #14
                            Here is my code from last week's assignment. We are to modify this to look like the link above:

                            Code:
                            class Product implements Comparable
                            {
                               private String title;   // class variable that stores the item name
                               private int item;     // class variable that stores the item number
                               private double stockQuantity;   // class variable that stores the quantity in stock
                               private double price;     // class variable that stores the item price
                            
                               public Product()
                               {
                                  title = "";
                                  item = 0;
                                  stockQuantity = 0;
                                  price = 0.0;
                               }
                            
                               public Product(String title, int item, double stockQuantity, double price)
                               {
                                  this.title = title;
                                  this.item = item;
                                  this.stockQuantity = stockQuantity;
                                  this.price = price;
                               }
                            
                               public void setTitle(String title)
                               {
                                  this.title = title;
                               }
                            
                               public String getTitle()
                               {
                                  return title;
                               }
                            
                               public void setItem(int item)
                               {
                                  this.item = item;
                               }
                            
                               public int getItem()
                               {
                                  return item;
                               }
                            
                               public void setStockQuantity(double quantity)
                               {
                                  stockQuantity = quantity;
                               }
                            
                               public double getStockQuantity()
                               {
                                  return stockQuantity;
                               }
                            
                               public void setItemPrice(double price)
                               {
                                  this.price = price;
                               }
                            
                               public double getItemPrice()
                               {
                                  return price;
                               }
                            
                               public double calculateInventoryValue()
                               {
                                  return (price * stockQuantity)*.05+(price*stockQuantity);
                               }
                            
                               public int compareTo(Object o)
                               {
                                  Product p = null;
                                  try
                                  {
                                     p = (Product) o;
                                  }
                                  catch (ClassCastException cE)
                                  {
                                     cE.printStackTrace();
                                  }
                                  return title.compareTo(p.getTitle());
                               }
                            
                               public String toString()
                               {
                                  return "DVD Title: " + title + "\nItem #: " + item + "\nPrice: $" + price + "\nQuantity: "
                                  + stockQuantity + "\nValue: $" + calculateInventoryValue();
                               }
                            }
                            
                            
                            class DVD extends Product
                            {
                            
                               String genre;
                               double restockingFee;
                            
                            
                               public DVD(String title, int item, double stockQuantity,double price ,double restockingFee, String genre)
                               {
                                  super(title,item,  stockQuantity, price);
                                  this.genre = genre;
                                  this.restockingFee = 0.05;
                            
                                  // TODO Auto-generated constructor stub
                               }
                            
                               //returns the value of the inventory, plus the restocking fee
                               public double getInventoryValue()
                               {
                                  // TODO Auto-generated method stub
                                  return super.getItemPrice() + restockingFee;
                               }
                            
                               public String toString()
                               {
                                  //this is not efficient
                                  //StringBuffer sb = new StringBuffer("Genre      \t").append(genre).append("\n");
                                  //sb.append(super.toString());
                            
                                  return super.toString() + "\nGenre      \t" + genre + "\nRestockingFee  \t" +restockingFee;
                               }
                            
                            }
                            
                            
                            
                            
                            public class Inventory2
                            {
                               Product[] supplies;
                            
                               public static void main(String[] args)
                               {
                                  Inventory2 inventory = new Inventory2();
                                  inventory.addProduct(new DVD("Beerfest", 1, 15, 22.95,.05,"Action"));
                                  inventory.addProduct(new DVD("Illustionist", 2, 25, 25.95,.05,"Comedy"));
                                  inventory.addProduct(new DVD("Employee of the Month", 3, 10,23.95,.05,"Comedy"));
                            
                                  System.out.println("Inventory of DVD Movies:\n\n");
                            
                            
                                  System.out.println();
                                  inventory.showInventory();
                                  inventory.sortByName();
                                  System.out.println();
                                  inventory.showInventory();
                            
                                  double total = inventory.calculateTotalInventory();
                                  System.out.println("Total Value is: $" + total);
                            
                            
                               }
                            
                               public void sortByName()
                               {
                                  for (int i = 1; i < supplies.length; i++)
                                  {
                                     int j;
                                     Product val = supplies[i];
                                     for (j = i - 1; j > -1; j--)
                                     {
                                        Product temp = supplies[j];
                                        if (temp.compareTo(val) <= 0)
                                        {
                                           break;
                                        }
                                        supplies[j + 1] = temp;
                                     }
                                     supplies[j + 1] = val;
                                  }
                               }
                            
                               //creates a String representation of the array of products
                               public String toString()
                               {
                                  String s = "";
                                  for (Product p : supplies)
                                  {
                                     s = s + p.toString();
                                     s = s + "\n\n";
                                  }
                                  return s;
                               }
                            
                               //Using an array so adding an item requires us to increase the size of the array first
                               public void addProduct(Product p1)
                               {
                                  if (supplies == null)
                                  {
                                     supplies = new Product[0];
                                  }
                                  Product[] p = supplies; //Copy all products into p first
                                  Product[] temp = new Product[p.length + 1]; //create bigger array
                                  for (int i = 0; i < p.length; i++)
                                  {
                                     temp[i] = p[i];
                                  }
                                  temp[(temp.length - 1)] = p1; //add the new product at the last position
                                  supplies = temp;
                               }
                            
                               //sorting the array using Bubble Sort
                               public double calculateTotalInventory()
                               {
                                  double total = 0.0;
                                  for (int i = 0; i < supplies.length; i++)
                                  {
                                     total = total + supplies[i].calculateInventoryValue();
                                  }
                                  return total;
                               }
                            
                               public void showInventory()
                               {
                                  System.out.println(toString()); //call our toString method
                               }
                            }

                            Comment

                            • stevedub
                              New Member
                              • Jan 2007
                              • 40

                              #15
                              Also, with the OP's code, how do you get your image to show? I tried to make a logo, but I don't see anything. What I did was I made an image and named it "dvd.gif" and put it in the same folder as the code. Is this the correct way to do that?

                              Comment

                              Working...