3 Replies - 2630 Views - Last Post: 19 January 2008 - 11:39 AM Rate Topic: -----

#1 badness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 07

Inventory Program Final Changes

Posted 16 January 2008 - 09:29 PM

Hello all. I have an inventory program that I need to do some updating to in order to complete the project. I could use some help with this if you wouldn't mind. Java is really cool and it is interesting, but the project I have is above my capabilities at this time. Thanks in advance.

I need to add a company logo. Let's go with Doug's DVDs and a graphic of a disc if possible. I also need to add a delete button and a modify button. I have an add button and I need those 3 to allow the user to perform the actions related to it (add, delete, or modify), the name, the number in stock, and the price of each. It could use a display that shows the total inventory value as well if possible.

I also need a Save button to the GUI that saves the inventory to a C:\data\inventory.dat file.

Finally I need to 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.

I would appreciate any help I can get. I will post my code in the next reply. Thanks.

Here is my code:
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
 
class Product implements Comparable {
 String name;
 double number;
 long stockQuantity;
 double price;
 
 public Product() {
  name = "";
	 number = 0.0;
	 stockQuantity = 0L;
	 price = 0.0;
 
 }
 public Product(String name, int number, long stockQuantity, double price) {
  this.name = name;
	 this.number = number;
	 this.stockQuantity = stockQuantity;
	 this.price = price;
	}
	public void setItemName(String name) {
  this.name = name;
 }
 public String getItemName() {
  return name;
 }
 public void setItemNumber(double number) {
  this.number = number;
 }
 public double getItemNumber() {
  return number;
 }
 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 name.compareTo(p.getItemName());
 }
 public String toString() {
  return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
 }
 
} 
 
 
 
class DVD extends Product implements Comparable {
 private String title;
 public DVD() {
  super(); //Call the constructor in Product
  title = ""; //Add the additonal attribute
 }
 public DVD(String name, int number, long stockQuantity, double price, String title) {
  super(name, number, stockQuantity, price); //Call the constructor in Product
  this.title = title; //Add the additonal attribute
	}
	public void setTitle(String title) {
  this.title = title;
 }
 public String getTitle() {
  return title;
 }
 public double calculateInventoryValue() {
  return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity()*0.05;
 }
 
 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() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
 }
}
 
public class Inventory5_5 extends JFrame implements ActionListener {
 
 
 private class MyPanel extends JPanel {
	ImageIcon image = new ImageIcon("Sample.jpg");
	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 name;
 JLabel number;
 JLabel title;
 JLabel quantity;
 JLabel price;
 JLabel fee;
 JLabel totalValue;
 
 JTextField nameField = new JTextField(20);
 JTextField numberField = new JTextField(20);
 JTextField titleField = new JTextField(20);
 JTextField quantityField = new JTextField(20);
 JTextField priceField = new JTextField(20);
 
 
 JPanel display;
 JPanel displayHolder;
 JPanel panel;
 public Inventory5_5() {
  makeTheDataItems();
 
  setSize(500, 500);
  setTitle("Doug'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(7, 1));
  //other.setLayout(new GridLayout(1, 1));
 
  //make the labels
  name = new	   JLabel("Name  :");
  number = new   JLabel("Number	 :");
  title = new	 JLabel("Title	   :");
  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(name);
  display.add(number);
  display.add(title);
  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 DVD("DVD", 001, 20, 10.99, "The Ring");
  Product p2 = new DVD("DVD", 002, 50, 13.99, "The Lord of the Rings: The Two Towers");
  Product p3 = new DVD("DVD", 003, 10, 10.99, "Perfect Storm");
  Product p4 = new DVD("DVD", 004, 30, 9.99, "Happy Gilmore");
  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() {
  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 name = new JLabel("Name		:");
  //JLabel number = new JLabel("Number	:");
  JLabel title = new JLabel("Title	 :");
  JLabel quantity = new JLabel("Quantity	:");
  JLabel price = new JLabel("Price	 :");
 
  add.add(name); add.add(nameField);
  //add.add(number); add.add(numberField);
  add.add(title); add.add(titleField);
  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[]) {
  Inventory5_5 object = new Inventory5_5(); 
 } // end main method
 
 public void actionPerformed(ActionEvent event) {
  String command = event.getActionCommand(); 
  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 DVD(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
  Double.parseDouble(priceField.getText()), titleField.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);
 
 }
 
 
 private void displayItemAt(int index) {
   DVD product = (DVD)supplies[index];
   name.setText("Item Name: "+ product.getItemName());
   number.setText("Item Number: "+ product.getItemNumber());
   title.setText("Title: "+ product.getTitle());
   quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
   price.setText("Item Price: "+ product.getItemPrice());
   totalValue.setText("Total: " + product.calculateInventoryValue());
   fee.setText("Fee :"+product.calculateRestockFee());
   this.repaint();
   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 Inventory5_5



Thanks again.

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program Final Changes

#2 capty99   User is offline

  • i am colt mccoy
  • member icon

Reputation: 99
  • View blog
  • Posts: 10,081
  • Joined: 26-April 01

Re: Inventory Program Final Changes

Posted 16 January 2008 - 10:43 PM

the java guys here are real good, and do this stuff in their sleep, but also do it as a profession , and know that you won't learn it if you don't do it.

so, be a little bit more specific about your problems... show us an example of you attempting it , or if you know nothing about it at all ask for a direction to look in, and someone will surely help you out asap.
Was This Post Helpful? 0
  • +
  • -

#3 Jayman   User is offline

  • Student of Life
  • member icon

Reputation: 421
  • View blog
  • Posts: 9,532
  • Joined: 26-December 05

Re: Inventory Program Final Changes

Posted 17 January 2008 - 01:32 AM

Please use :code: tags when posting code.
Was This Post Helpful? 0
  • +
  • -

#4 badness   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 4
  • Joined: 07-December 07

Re: Inventory Program Final Changes

Posted 19 January 2008 - 11:39 AM

 
import java.util.*;
import javax.swing.*;
import java.awt.event.*;
import java.awt.*;
import java.io.*;

 
class Product implements Comparable {
 String name;
 double number;
 long stockQuantity;
 double price;
 
 public Product() {
  name = "";
	 number = 0.0;
	 stockQuantity = 0L;
	 price = 0.0;
 
 }
 public Product(String name, int number, long stockQuantity, double price) {
  this.name = name;
	 this.number = number;
	 this.stockQuantity = stockQuantity;
	 this.price = price;
	}
	public void setItemName(String name) {
  this.name = name;
 }
 public String getItemName() {
  return name;
 }
 public void setItemNumber(double number) {
  this.number = number;
 }
 public double getItemNumber() {
  return number;
 }
 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 name.compareTo(p.getItemName());
 }
 public String toString() {
  return "Name :"+getItemName() + "\nNumber"+number+"\nPrice"+price+"\nQuantity"+stockQuantity + "\nValue :"+calculateInventoryValue();
 }
 
} 
 
 
 
class DVD extends Product implements Comparable {
 private String title;
 public DVD() {
  super(); //Call the constructor in Product
  title = ""; //Add the additonal attribute
 }
 public DVD(String name, int number, long stockQuantity, double price, String title) {
  super(name, number, stockQuantity, price); //Call the constructor in Product
  this.title = title; //Add the additonal attribute
	}
	public void setTitle(String title) {
  this.title = title;
 }
 public String getTitle() {
  return title;
 }
 public double calculateInventoryValue() {
  return getItemPrice() * getStockQuantity() + getItemPrice()*getStockQuantity();
 }
 
 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() +"\nTitle :"+getTitle()+"\nValue"+calculateInventoryValue();
 }
}
 
public class Inventory5_5 extends JFrame implements ActionListener {
 
 
 private class MyPanel extends JPanel {
	ImageIcon image = new ImageIcon("DVDgraphic.jpg");
	int  width  = image.getIconWidth();
	int  height  = image.getIconHeight();
	long angle = 0;
 
	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 name;
 JLabel number;
 JLabel title;
 JLabel quantity;
 JLabel price;
 JLabel fee;
 JLabel totalValue;
 
 JTextField nameField = new JTextField(20);
 JTextField numberField = new JTextField(20);
 JTextField titleField = new JTextField(20);
 JTextField quantityField = new JTextField(20);
 JTextField priceField = new JTextField(20);
 
 
 JPanel display;
 JPanel displayHolder;
 JPanel panel;
 public Inventory5_5() {
  makeTheDataItems();
 
  setSize(850, 700);
  setTitle("Doug's DVD Emporium Inventory Package");
  //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(7, 1));
  //other.setLayout(new GridLayout(1, 1));
 
  //make the labels
  name = new	   JLabel("Item  :");
  number = new   JLabel("Number	 :");
  title = new	 JLabel("Title	   :");
  quantity = new   JLabel("Quantity :");
  price = new	 JLabel("Price	   :");
  fee = new  JLabel("Restocking 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");
  JButton delete = makeButton("Delete");
  JButton save = makeButton("Save");
  JButton modify = makeButton("Modify");
  JButton search = makeButton("Search");
  
  //Add the labels to the display panel
  display.add(name);
  display.add(number);
  display.add(title);
  display.add(quantity);
  display.add(price);
  display.add(fee);
  display.add(totalValue); 
  
  //add the buttons to the buttonPanel
  buttons.add(first);
  buttons.add(previous);
  buttons.add(next);
  buttons.add(last);
  buttons.add(search);
  buttons.add(exit);
  buttons.add(delete);
  buttons.add(save);
  buttons.add(modify);
  
  
  //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 DVD("DVD", 001, 20, 10.99, "The Ring");
  Product p2 = new DVD("DVD", 002, 50, 13.99, "The Lord of the Rings: The Two Towers");
  Product p3 = new DVD("DVD", 003, 10, 10.99, "Perfect Storm");
  Product p4 = new DVD("DVD", 004, 30, 9.99, "Happy Gilmore");
  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() {
  panel = new JPanel();
  JPanel add = new JPanel();
  add.setLayout(new GridLayout(2, 1));
  add.setLayout(new GridLayout(5, 5));
  JButton addIt = makeButton("Add Item");
 
  JLabel name = new JLabel("Item		:");
  JLabel number = new JLabel("Number	:");
  JLabel title = new JLabel("Title	 :");
  JLabel quantity = new JLabel("Quantity	:");
  JLabel price = new JLabel("Price	 :");
 
  add.add(name); add.add(nameField);
  add.add(number); add.add(numberField);
  add.add(title); add.add(titleField);
  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[]) {
  Inventory5_5 object = new Inventory5_5(); 
 } // end main method
 
 public void actionPerformed(ActionEvent event) {
  String command = event.getActionCommand(); 
  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 DVD(nameField.getText(), supplies.length -2, Long.parseLong(quantityField.getText()),
  Double.parseDouble(priceField.getText()), titleField.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);
 
 }
 
 
 private void displayItemAt(int index) {
   DVD product = (DVD)supplies[index];
   name.setText("Item: "+ product.getItemName());
   number.setText("Item Number: "+ product.getItemNumber());
   title.setText("Title: "+ product.getTitle());
   quantity.setText("Quantity In Stock: "+ product.getStockQuantity());
   price.setText("Item Price: "+ product.getItemPrice());
   totalValue.setText("Total: " + product.calculateInventoryValue());
   fee.setText("Restocking Fee :"+product.calculateRestockFee());
   this.repaint();
   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 Inventory5_5

 


I have added the buttons that I need, the graphic (cheesy, I know) and put the import java.io.*; utitilit in the code. The delete, search, modify and save buttons need to funcition as described before thus the import java.io.*; at the top of the code. I reviewed the writing to text forum post that has this code:

 public class InputFile1
{
	 public static void main (String args[]) throws IOException
	 {
		  final FileWriter inputthingy = new FileWriter("inputfile1.dat");
		  final BufferedWriter bufferthingy = new BufferedWriter(inputthingy);
		  final PrintWriter printythingy = new PrintWriter(bufferthingy);
		  try {
		  
		  printythingy.println ("Hello inputthingy file");
		  System.out.println("File write successful");
		  } catch (Exception e) {
			   System.out.println("Error writing to file");
		  }
		  bufferthingy.close();
		  
			   System.exit(0);
	 }
 
} 
 


How would I modify to fit and function in my program and where exactly would I put it. Remember my data needs to be saved in a C:\data\inventory.dat file.

I could still use some help with the arrays for delete and modify and the search function. I am trying to do as much of this as I can, but I am running out of time very rapidly. I have spent hours working at this. Thanks for any and all help.

Badness
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1