2 Replies - 8448 Views - Last Post: 29 August 2007 - 03:10 PM Rate Topic: -----

#1 mudpuppy  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 17
  • Joined: 13-August 07

Inventory Program GUI/Buttons

Post icon  Posted 29 August 2007 - 11:53 AM

My code compiles, but I can't run the program to see if it does what it is supposed to. This is what the assignment intails for this week: Modify the Inventory Program by adding a button to the GUI that allows the user to move to the first item, the previous item, the next item, and the last item in the inventory. If the first item is displayed and the user clicks on the Previous button, the last item should display. If the last item is displayed and the user clicks on the Next button, the first item
should display. the GUI using Java graphics classes.
Add a company logo to the GUI using Java graphics classes.

As far as I know, I have done this. If anyone could look at my code and point me in the right direction, it would be greatly appreciated. Also, I have to do this to the assignment by Sunday: 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\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.

I have yet to start on this part because I have yet to get the other part corrected. Any advice on the part that is due Sunday is also greatly appreciated. I have no idea even where to begin.

Here is all of my codes:
public class Fish extends Product {
	
		public Fish(int number, String fishname, int fishquantity, double fishprice) {
				super(number, fishname, fishquantity, fishprice);
		}

		public double getValue() {
		double inventoryvalue = super.getValue();
		return (inventoryvalue * 1.05);
	}	
}




import javax.swing.SwingUtilities;

public class Fish2 {	
	
	public static final int MAXIMUM_ITEMS = 4;
	
	public Fish2() {}

	public static void main(String[] args) {
	
		// Create Java JFrame / GUI
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				InventoryGUI inventoryGUI = new InventoryGUI(new Inventory());
				inventoryGUI.createDisplayGUI();
			}
		});
	}
}



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


public class Inventory 
{
  
		
		public static final int MAXIMUM_ITEMS = 10;
		private static Product product[] = new Fish[MAXIMUM_ITEMS];

		public static void main(String args[]) {
				buildInventory();
				getInventory();		
		}

		
		public static void buildInventory() {

			   
				product[0] = new Fish(0, "Fancy Tailed Guppy",25, 4.99);
				product[1] = new Fish(1, "Huma Huma Picasso Trigger",2, 35.99);
				product[2] = new Fish(2, "Black Durgeon Trigger",1, 99.99);
				product[3] = new Fish(3, "Niger Trigger",2, 45.99);
				product[4] = new Fish(4, "Jack Dempsey Cichlid",4, 19.99);
				product[5] = new Fish(5, "Midas Cichlid",1, 69.99);
				product[6] = new Fish(6, "Neon Tetra",25, 2.99);
				product[7] = new Fish(7, "Convict Cichlid",2, 15.99);
				product[8] = new Fish(8, "Oscar", 4, 37.99);
				product[9] = new Fish(9, "Albino Cory Cat",1, 29.99);
		}


		
		public static void getInventory() {
				for(int i = 0; i < product.length; i++) {
					System.out.println("Number:	 " + product[i].getNumber());
					System.out.println("Name:	   " + product[i].getName());
					System.out.println("Quantity:   " + product[i].getQuantity());
					System.out.println("Price:	  " + product[i].getPrice());
					System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
			

						

					System.out.printf("Restock Fee: " + (product[i].getQuantity() * product[i].getPrice()) * 0.05);
//					System.out.printf("\n%s's Inventory Total including Restocking Fee is: $%5.2f.\n");
					
			  }  
	   }

	Product Product() {
		return null;
	}

	Product[] getProduct() {
		return null;
	}
}




This line has been commented out because I could never get the restocking fee to display right: // System.out.printf("\n%s's Inventory Total including Restocking Fee is: $%5.2f.\n");

import java.awt.Container;
import java.awt.GridLayout;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JButton;

public class InventoryGUI {

	private GridLayout gridLayout = new GridLayout(Inventory.MAXIMUM_ITEMS,2);
	private JFrame frame = null;
	private JLabel label = null;
	private JTextField textField = null;
	
	private Inventory inventory = null;
	
	public static final int MAXIMUM_ITEMS = 4;
	Product[] product = null;
	
	private String price = "";
	
	public InventoryGUI(Inventory inventory) {
		this.inventory = inventory;
	}
	
	public void createDisplayGUI() {

		// Create a JFrame container for all GUI widgets
							frame = new JFrame("Inventory");
							frame = new JFrame("Number");		
							frame = new JFrame("Name");
							frame = new JFrame("Quantity");			  
							frame = new JFrame("Price");				   
							frame = new JFrame("Item Value");
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		
		// Set up default look and feel for particular Operating System
		JFrame.setDefaultLookAndFeelDecorated(true);
		
		// Set up the content pane and components in GridLayout
		Container container = frame.getContentPane();
		container.setLayout(gridLayout);
		
		// get the all the products in the inventory
		 product = inventory.getProduct();
		
		// loop through all the products in the inventory and display them.
		   for (int i=0; i < Inventory.MAXIMUM_ITEMS; i++) {

		
			// Define label widget
			  label = new JLabel(product[i].getName());
			  
		  
		 Icon logo = new ImageIcon("C:/COMPANYLOGO.jpg"); 
		 label = new JLabel(logo);
		 label.setToolTipText("Company Logo"); 
 

			// Define textField widget
			price = Double.toString (product[i].getPrice());
			textField = new JTextField(price);
		
			// add widgets to JFrame
			container.add(label);
			container.add(textField);

			 //add buttons to JFrame
			  JButton add = new JButton("Add"); 
			  JButton delete = new JButton("Delete"); 
			  JButton modify = new JButton("Modify"); 
			  JButton first = new JButton("First"); 
			  JButton next = new JButton("Next"); 
			  JButton previous = new JButton("Previous"); 
			  JButton last = new JButton("Last"); 
		}
		
		// display the frame and all widgets within
		frame.pack();
		frame.setVisible(true);
	}
}

public class Product {

	private String name = "";
	private int number = 0;
	private double price = 0;
	private int quantity = 0;

	
	public Product(int number, String name, int quantity, double price) {
		this.number = number;
		this.name = name;
		this.quantity = quantity;
		this.price = price;
	}

	
	public String getName() { return name; }
	public int getNumber() { return number; }
	public double getPrice() { return price; }
	public int getQuantity() { return quantity; }

	
	public double getValue() {
		 return (double) quantity * price;
	}
}




Thanks to anyone who helps! I just want to pass this class with a little understanding of Java.

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program GUI/Buttons

#2 PennyBoki  Icon User is offline

  • system("revolution");
  • member icon

Reputation: 53
  • View blog
  • Posts: 2,334
  • Joined: 11-December 06

Re: Inventory Program GUI/Buttons

Posted 29 August 2007 - 01:58 PM

It's true that your code compiles but when ran it gets NullPointerException, you need to correct something in the product objects you use, sorry to post such modest suggestion, but this problem is way too much time consuming, so I'd suggest in order to solve this faster, or to get more help, break this into peaces and try to do it bit by bit, and the be very careful when you are putting things together.
Was This Post Helpful? 0
  • +
  • -

#3 Martyr2  Icon User is offline

  • Programming Theoretician
  • member icon

Reputation: 3873
  • View blog
  • Posts: 11,407
  • Joined: 18-April 07

Re: Inventory Program GUI/Buttons

Posted 29 August 2007 - 03:10 PM

Well remember mudpuppy about printf, it is used to display a "formatted" string. So first of all your first printf is not really needed. Your second one is missing the value for the total inventory value...

public static void getInventory() {
	   // You will need an accumulator to add up total value when you loop through your products
	   double totalStockValue = 0.0;

	   for(int i = 0; i < product.length; i++) {
			  System.out.println("Number:	 " + product[i].getNumber());
			  System.out.println("Name:	   " + product[i].getName());
			  System.out.println("Quantity:   " + product[i].getQuantity());
			  System.out.println("Price:	  " + product[i].getPrice());
			  System.out.println("Item Value: " + product[i].getQuantity() * product[i].getPrice());
					 
			  // No need to use printf here, you are printing a normal line. 
			  // Remember you have getValue() in a product so you can use that to simplify this equation.

			  System.out.println("Restock Fee: " + product[i].getValue() * 0.05;);

			  // Now we add the products total value (value plus restock fee of 5%) to total stock value
			  totalStockValue += (product[i].getValue() * 1.05);
					
	   }  
	   
	   // We have looped through all products showing their fee and adding their total value. 
	   // Now we can use that printf to show the total inventory value.
	   // Notice how I am using the format specifier and matching it to the totalStockValue variable below?
	   System.out.printf("Inventory Total including Restocking Fee is: $%5.2f.\n", totalStockValue);
}



You know you might want to change the name of this function from getInventory to something like "printInventory" because you are really printing what is in the inventory is all. You might also want to get rid of that Product Product() method in your inventory class because it is not needed.

As for your GUI part, you got some hustling to do because you have a lot to add here. We are not going to do it all for you. So here is an itemized list of what you will need to do and research to get you through this next phase...

1) You will need to look up the idea of "ActionListeners" which you will allow you to attach events to your buttons. These listeners listen for a click of the button and run the "actionPerformed()" method to handle the event. In this function you will be able to create a product and add it to your inventory list. The one for your delete of course will read which item is selected and delete it from the array. Edit will listen for a click and handle the find and replace of your item in the array. (This will be tied to your search functionality mentioned later for finding by name. You have to find it first to edit it right?)

These events will rely heavily on your product class definition and use its methods so know them well.

2) You are going to have to know your loops so you can move through the array. When you go to add an item, you can go through the array to the last element, read its the products method "getNumber()" and increment it by one, then add it to the array.

3) You are going to have to build in a write to file feature into your Inventory class. Something like "writeInventoryFile()" which will open a file, loop through your products array and output each one as a record. When you are done, it will close the file. This all can be done in one function and should be somewhere in the neighborhood of about 30-40 lines of code roughly. Look at java.io package for file handling classes (hint: you might want to use a FileOutputWriter object.

FileOutputWriter class

4) Lastly, for the search all you will need to do is loop through the array looking at each products name (getName() of the product, remember?) and compare it to the search criteria. (hint: don't forget to use .equals() method of your string)

Happy programming and make sure not to procrastinate on this! :)

This post has been edited by Martyr2: 29 August 2007 - 03:12 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1