By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,798 Members | 1,336 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,798 IT Pros & Developers. It's quick & easy.

Can anyone else help with an Inventory program?

P: 59
r03581x has been helping me with a program, but he won't be online till Monday, and I have to have this finished by tomorrow... Is there anyone else out there that can help me?

Here is the first half of what I have so far... I will post the rest of the code in a second post cause it is too long...

Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2. import javax.swing.*;
  3. import java.awt.event.*;
  4. import java.awt.*;
  5.  
  6. class Product implements Comparable
  7. {
  8.     String name;         // class variable that stores the item name
  9.     double number;         // class variable that stores the item number
  10.     long stockQuantity; // class variable that stores the quantity in stock
  11.     double price;         // class variable that stores the item price
  12.     public Product()
  13.     {
  14.           name = "";
  15.         number = 0.0;
  16.         stockQuantity = 0L;
  17.         price = 0.0;
  18.      }
  19.  
  20.      public Product(String name, int number, long stockQuantity, double price)
  21.      {
  22.           this.name = name;
  23.         this.number = number;
  24.         this.stockQuantity = stockQuantity;
  25.         this.price = price;
  26.     }
  27.  
  28.     public void setItemName(String name)
  29.     {
  30.           this.name = name;
  31.      }
  32.  
  33.      public String getItemName()
  34.      {
  35.           return name;
  36.      }
  37.  
  38.      public void setItemNumber(double number)
  39.      {
  40.           this.number = number;
  41.      }
  42.  
  43.      public double getItemNumber()
  44.      {
  45.           return number;
  46.      }
  47.  
  48.      public void setStockQuantity(long quantity)
  49.      {
  50.           stockQuantity = quantity;
  51.      }
  52.  
  53.      public long getStockQuantity()
  54.      {
  55.           return stockQuantity;
  56.      }
  57.  
  58.      public void setItemPrice(double price)
  59.      {
  60.       this.price = price;
  61.      }
  62.  
  63.      public double getItemPrice()
  64.      {
  65.           return price;
  66.      }
  67.  
  68.      public double calculateInventoryValue()
  69.      {
  70.           return getItemPrice() * getStockQuantity();
  71.      }
  72.  
  73.      public int compareTo (Object o)
  74.      {
  75.           Product p = (Product)o;
  76.           return name.compareTo(p.getItemName());
  77.      }
  78.  
  79.      public String toString()
  80.      {
  81.           return "\nName: "+getItemName() + "\nNumber: "+number+"\nPrice: $"+price+"\nQuantity: "+stockQuantity + "\nValue: $"+calculateInventoryValue();
  82.      }
  83. }//end Product
  84.  
  85. class MobilePhone extends Product implements Comparable
  86. {
  87.     private String brand;
  88.     public MobilePhone()
  89.     {
  90.     super(); //call the constructor in Product
  91.     brand = ""; //add the additonal attribute
  92.     }
  93.  
  94.     public MobilePhone(String name, int number, long stockQuantity, double price, String brand)
  95.     {
  96.         super(name, number, stockQuantity, price); //call the constructor in Product
  97.         this.brand = brand; //add the additonal attribute
  98.     }
  99.  
  100.     public void setBrand(String brand)
  101.     {
  102.         this.brand = brand;
  103.     }
  104.  
  105.     public String getBrand()
  106.     {
  107.         return brand;
  108.     }
  109.  
  110.     public double calculateInventoryValue()
  111.     {
  112.         return getItemPrice() * getStockQuantity();
  113.     }
  114.  
  115.     public double calculateRestockFee()
  116.     {
  117.         return getItemPrice() * 0.05;
  118.     }
  119.  
  120.     public int compareTo (Object o)
  121.     {
  122.         Product p = (Product)o;
  123.         return getItemName().compareTo(p.getItemName());
  124.     }
  125.  
  126.     public String toString()
  127.     {
  128.         return "\nName: "+getItemName() + "\nNumber: "+getItemNumber()+"\nPrice: $"+getItemPrice()+"\nQuantity: "+getStockQuantity() +"\nBrand: "+getBrand()+"\nValue: $"+calculateInventoryValue();
  129.     }
  130. }//end MobilePhone
Dec 8 '06 #1
Share this Question
Share on Google+
2 Replies


P: 59
Here is the seconf half of the code...

Expand|Select|Wrap|Line Numbers
  1. public class Inventory10 extends JFrame implements ActionListener
  2. {
  3.     //utility class for displaying the picture
  4.     private class MyPanel extends JPanel
  5.     {
  6.         ImageIcon image = new ImageIcon("Sample.jpg");
  7.         int width = image.getIconWidth();
  8.         int height = image.getIconHeight();
  9.         long angle = 30;
  10.         public MyPanel()
  11.         {
  12.             super();
  13.         }
  14.  
  15.         public void paintComponent(Graphics g)
  16.         {
  17.              super.paintComponent(g);
  18.              Graphics2D g2d = (Graphics2D)g;
  19.              g2d.rotate (Math.toRadians(angle), 60+width/2, 60+height/2);
  20.              g2d.drawImage(image.getImage(), 60, 60, this);
  21.              g2d.dispose();
  22.         }
  23.  
  24.     }//end class MyPanel
  25.  
  26.     int currentIndex; //currently displayed Item
  27.     Product[] supplies = new Product[4];
  28.     JLabel name ;
  29.     JLabel number;
  30.     JLabel brand;
  31.     JLabel quantity;
  32.     JLabel price;
  33.     JLabel fee;
  34.     JLabel totalValue;
  35.     JTextField nameField = new JTextField(20);
  36.     JTextField numberField = new JTextField(20);
  37.     JTextField brandField = new JTextField(20);
  38.     JTextField quantityField = new JTextField(20);
  39.     JTextField priceField = new JTextField(20);
  40.  
  41.     JPanel display;
  42.     JPanel displayHolder;
  43.     JPanel panel;
  44.  
  45.     public Inventory10()
  46.     {
  47.         makeTheDataItems();
  48.         setSize(500, 500);
  49.         setTitle("Audrey's Inventory Program v1.0");
  50.  
  51.         //make the panels
  52.         display = new JPanel();
  53.         JPanel other = new JPanel();
  54.         JPanel picture = new MyPanel();
  55.         JPanel buttons = new JPanel();
  56.         JPanel centerPanel = new JPanel();
  57.         displayHolder = new JPanel();
  58.         display.setLayout(new GridLayout(3, 3));
  59.         other.setLayout(new GridLayout(2, 1));
  60.  
  61.         //make the labels
  62.         name = new     JLabel("Name        :");
  63.         number = new     JLabel("Number     :");
  64.         brand = new     JLabel("Brand     :");
  65.         quantity = new JLabel("Quantity    :");
  66.         price = new     JLabel("Price     :");
  67.         fee = new        JLabel("Fee         :");
  68.         totalValue = new JLabel("Total Value :");
  69.  
  70.         //make the buttons
  71.         JButton first = makeButton("First");
  72.         JButton next = makeButton("Next");
  73.         JButton previous = makeButton("Previous");
  74.         JButton last = makeButton("Last");
  75.         JButton exit = makeButton("Exit");
  76.  
  77.         //other buttons
  78.         JButton add = makeButton("Add");
  79.  
  80.         //add the labels to the display panel
  81.         display.add(name);
  82.         display.add(number);
  83.         display.add(brand);
  84.         display.add(quantity);
  85.         display.add(price);
  86.         display.add(fee);
  87.  
  88.         //add the buttons to the buttonPanel
  89.         buttons.add(first);
  90.         buttons.add(previous);
  91.         buttons.add(next);
  92.         buttons.add(last);
  93.         buttons.add(exit);
  94.  
  95.         //add the picture panel and display to the centerPanel
  96.         displayHolder.add(display);
  97.         centerPanel.setLayout(new GridLayout(2, 1));
  98.         centerPanel.add(picture);
  99.         centerPanel.add(displayHolder);
  100.         other.add(buttons);
  101.         JPanel forAdd = new JPanel(); // add the other buttons to this panel
  102.         forAdd.add(add);
  103.         other.add(forAdd);
  104.  
  105.         //add the panels to the frame
  106.         getContentPane().add(centerPanel, "Center");
  107.         getContentPane().add(other, "South");
  108.         this.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
  109.         setVisible(true);
  110.  
  111.     }//end Inventory10
  112.  
  113.     private void makeTheDataItems ()
  114.     {
  115.           supplies[0] = new MobilePhone("3310", 001, 200, 200, "Nokia");
  116.           supplies[1] = new MobilePhone("T720", 002, 500, 250, "Motorola");
  117.           supplies[2] = new MobilePhone("5110C", 003, 35, 149, "LG");
  118.           supplies[3] = new MobilePhone("9400D", 004, 90, 300, "Samsung");
  119.     }
  120.  
  121.     //method for creating and dressing buttons
  122.     private JButton makeButton(String label)
  123.     {
  124.         JButton button = new JButton(label);
  125.         button.setActionCommand(label);
  126.         button.addActionListener(this);
  127.         return button;
  128.     }
  129.  
  130.     private void addItem()
  131.     {
  132.         System.out.println("eeeeeeeeee");
  133.         panel = new JPanel();
  134.         JPanel add = new JPanel();
  135.         add.setLayout(new GridLayout(2, 1));
  136.         add.setLayout(new GridLayout(4, 4));
  137.         JButton addIt = makeButton("Add Item");
  138.         JLabel name = new JLabel("Name        :");
  139.         //JLabel number = new JLabel("Number     :");
  140.         JLabel brand = new JLabel("Brand     :");
  141.         JLabel quantity = new JLabel("Quantity    :");
  142.         JLabel price = new JLabel("Price     :");
  143.         add.add(name); add.add(nameField);
  144.         //add.add(number); add.add(numberField);
  145.         add.add(brand); add.add(brandField);
  146.         add.add(quantity); add.add(quantityField);
  147.         add.add(price); add.add(priceField);
  148.         panel.add(add);
  149.         JPanel forAddIt = new JPanel();
  150.         forAddIt.add(addIt);
  151.         panel.add(forAddIt);
  152.         displayHolder.remove(display);
  153.         displayHolder.add(panel);
  154.         //display = panel;
  155.         this.setVisible(true);
  156.     }//end addItem
  157.  
  158.      // main methods begins execution of java application
  159.     public static void main( String args[])
  160.     {
  161.         Inventory10 object = new Inventory10();
  162.     } // end main method
  163.  
  164.     public void actionPerformed(ActionEvent event)
  165.     {
  166.         String command = event.getActionCommand(); //retrieves command set for the button
  167.  
  168.         if(command.equals("First"))
  169.         {
  170.             displayFirst();
  171.         }
  172.         else if(command.equals("Next"))
  173.         {
  174.             displayNext();
  175.         }
  176.         else if(command.equals("Previous"))
  177.         {
  178.             displayPrevious();
  179.         }
  180.         else if(command.equals("Last"))
  181.         {
  182.             displayLast();
  183.         }
  184.         else if(command.equals("Exit"))
  185.         {
  186.             this.dispose();
  187.             System.exit(0);
  188.         }
  189.         else if(command.equals("Add"))
  190.         {
  191.             addItem();
  192.         }
  193.         else if(command.equals("Add Item"))
  194.         {
  195.             addItemToArray();
  196.         }
  197.  
  198.     }//end action method
  199.  
  200.     private void addItemToArray()
  201.     {
  202.         Product p = new MobilePhone(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
  203.         Double.parseDouble(priceField.getText()), brandField.getText());
  204.  
  205.         //extend size of array by one first
  206.         Product[] ps = new Product[supplies.length + 1];
  207.         for(int i = 0; i < ps.length-1; i++)
  208.         {
  209.             ps[i] = supplies[i];
  210.         }
  211.         ps[supplies.length] = p;
  212.         supplies = ps;
  213.         displayHolder.remove(panel);
  214.         displayHolder.add(display);
  215.         displayLast();
  216.         this.setVisible(false);
  217.         this.setVisible(true);
  218.     }//end addItem
  219.  
  220.     //utility method to reduce lines of code
  221.     private void displayItemAt(int index)
  222.     {
  223.         MobilePhone product = (MobilePhone)supplies[index];
  224.         name.setText("Item Name: "+ product.getItemName());
  225.         number.setText("Item Number: "+ product.getItemNumber());
  226.         brand.setText("Brand: "+ product.getBrand());
  227.         quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
  228.         price.setText("Item Price: "+ product.getItemPrice());
  229.         totalValue.setText("Total: " + product.calculateInventoryValue());
  230.         fee.setText("Fee :"+product.calculateRestockFee());
  231.         this.setVisible(true);
  232.     }
  233.  
  234.     public void displayFirst()
  235.     {
  236.         displayItemAt(0);
  237.         currentIndex = 0;
  238.     }
  239.  
  240.     public void displayNext()
  241.     {
  242.         if(currentIndex == supplies.length-1)
  243.         {
  244.             displayFirst();
  245.             currentIndex = 0;
  246.         }
  247.         else
  248.         {
  249.             displayItemAt(currentIndex + 1);
  250.             currentIndex++;
  251.         }
  252.     }
  253.  
  254.     public void displayPrevious()
  255.     {
  256.         if(currentIndex == 0)
  257.         {
  258.             displayLast();
  259.             currentIndex = supplies.length-1;
  260.         }
  261.         else
  262.         {
  263.             displayItemAt(currentIndex - 1);
  264.             currentIndex--;
  265.         }
  266.     }
  267.  
  268.     public void displayLast()
  269.     {
  270.         displayItemAt(supplies.length-1);
  271.         currentIndex = supplies.length-1;
  272.     }
  273. }//end class Inventory10
Dec 8 '06 #2

P: 59
Here is what I need it to do...

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 :\data\inventory.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.

If anyone can help me at all I would really apreciate it so much!!! Thanks
Dec 8 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.