Get Methods and GUI

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

31 Replies - 2088 Views - Last Post: 23 June 2009 - 05:12 PM Rate Topic: -----

#1 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Get Methods and GUI

Posted 19 June 2009 - 04:31 PM

Well, I know everyone just loves that Inventory Program, and I guess it's my turn. I've done all the assignments but now I'm having some serious trouble. I have my Product.java class, DVD.java class (extends class Product), and an Inventory.java file that creates and holds an array of products. Now I have to put all this information into a GUI. I've designed the GUI and can have it show the Labels and TextFields, my problem is how to "get" the information from my other classes into the TextFields for display in the GUI. I've tried to use the get methods that I created, like getItemName, getItemValue.. stuff like that, but they don't work as arguments for the GUI. Here is my code for the GUI:

// InventoryGUI.java
//Using the JLabel class to display Inventory program

import java.awt.*; // specifies how components are arranged
import javax.swing.*; //see if the asterisk loads all that stuff.
import java.text.DecimalFormat; //decimalFormat loaded just in case it needs it to display properly

public class InventoryGUI extends JFrame //get parent class JFrame and use for new class InventoryGUI
{	
	private static final int WIDTH = 600;
	private static final int HEIGHT = 150;
	
	
	private JLabel itemNameL, quantityOnHandL, itemPriceL, productIdL, yearL, welcome1L, welcome2L, itemValueL, totalInvValueL,
					restockingFeeL;// Declare Labels to be used in GUI
	
	private JTextField itemNameTF, quantityOnHandTF, itemPriceTF, productIdTF, itemValueTF, totalInvValueTF,
					yearTF, restockingFeeTF;	// Declare TextFields to be used in GUI	
		
		
	public InventoryGUI() //InventoryGUI constructor adds JLabels and JTextFields to JFrame
	{
		itemNameL = new JLabel( "Name" );
		quantityOnHandL = new JLabel( "Quantity on Hand" );
		itemPriceL = new JLabel( "Price per item" );
		productIdL = new JLabel( "Product ID" );
		itemValueL = new JLabel( "Value of all Same Items" );
		totalInvValueL = new JLabel( "Value of All Items" );
		yearL = new JLabel( "Year" );
		restockingFeeL = new JLabel( "Restocking Fee" );
		
		itemNameTF = new JTextField( 30 );
		quantityOnHandTF = new JTextField( 3 );
		itemPriceTF = new JTextField( 10 );
		productIdTF = new JTextField( 5 );
		itemValueTF = new JTextField( 10 );
		totalInvValueTF = new JTextField( 12 );
		yearTF = new JTextField( 10 );
		restockingFeeTF = new JTextField( 8 );
		
		setTitle( "Sample Title: Mike's Inventory" );
		Container pane = getContentPane();
		pane.setLayout( new FlowLayout() );
		
		pane.add( itemNameL );
		pane.add( itemNameTF );
		pane.add( quantityOnHandL );
		pane.add( quantityOnHandTF );
		pane.add( itemPriceL );
		pane.add( itemPriceTF );
		pane.add( productIdL );
		pane.add( productIdTF );
		pane.add( itemValueL );
		pane.add( itemValueTF );
		pane.add( totalInvValueL );
		pane.add( totalInvValueTF );
		pane.add( yearL );
		pane.add( yearTF );
		pane.add( restockingFeeL );
		pane.add( restockingFeeTF );
					   
		setSize(WIDTH, HEIGHT);
		setVisible(true);
		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public static void main(String[] args)
	{
		InventoryGUI InventoryObj = new InventoryGUI();
	}
}   




Maybe I can use the get methods, but I just don't know how the syntax works. Or Maybe I can't use the get methods, if not, could someone give me an idea of how the data gets into the GUI? Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Get Methods and GUI

#2 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 04:42 PM

Where is the data created? DOes the user enter the data into the text fields or do you have default data created in another class? If so can you show that class.
Was This Post Helpful? 0
  • +
  • -

#3 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 04:59 PM

Well, the data is created from my previous Java classes that are in the same directory as this one I'm working on. Here are the codes.

This is Product.java
//This is the Product.java class.  It is the methods for the Product object used in Inventory.java

public class Product 
{
	// Private variables
	private String name;
	private int quantity;
	private double price;
	private int productid = 0;

	// Default constructor uses other constructor to set the default state of the object (instantiate).

	public Product() 
	{
		this(0,"Unknown",0,0.00);
	}

	// Constructor that user can specify a name, quantity, and price for items.
	public Product(int productId, String itemname, int quantityOnHand, double itemprice) 
	{
		productid = productId;
		setName(itemname);
		setQuantityOnHand(quantityOnHand);
		setPrice(itemprice);
	}


	// Public mutators (changes state of the object)
	public void setName(String itemname) 
	{
		name = itemname;
	}


	// Sets quantity on hand and if negative, defaults to zero.
	public void setQuantityOnHand(int quantityOnHand) 
	{
		if (quantityOnHand > 0) 
		{
			quantity = quantityOnHand;
		}
		else { quantity = 0; }
	}


	// Set price of a product and defaults it to zero if negative.
	public void setPrice(double itemPrice) 
	{
		if (itemPrice > 0.00) 
		{
			price = itemPrice;
		}
		else { price = 0.00; }
	}

	// Get the product's name
	public String getName() 
	{
		return name;
	}

	public int getQuantityOnHand() 
	{
		return quantity;
	}

	public double getPrice() 
	{
		return price;
	}

	// Calculate the value of stock on this particular item.
	public double getItemValue() 
	{
		return (price * (double)quantity);
	}

	// String representation of the product
	public String toString() 
	{
		return name + " - " + price;
	}
}


Then comes DVD which extends Product
// Inherited class DVD from the base class Product
// It is a product, but has the extra feature of having a year the movie was made.

public class DVD extends Product //creates subclass from parent class Product
{
	// Holds the year the move was made
	private int movieyear;

	// Constructor, it calls the constructor of Product first (parent class)
	// Passing the parent class its values to initialize the class BEFORE
	// setting this instance year variable.

	public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) 
	{
		// Pass on the values needed for creating the Product class first thing
		super(productId, itemname, quantityOnHand, itemprice);//super because it references the parent class
		movieyear = year;
	}

	// In case user wants to set the year manually
	public void setYear(int year) 
	{
		movieyear = year;
	}

	// Get the year of this DVD product
	public int getYear() 
	{
		return movieyear;
	}

	// Overrides getItemValue() in Product class by calling the parent class version and
	// adding a 5% restocking fee on top
	public double getItemValue() 
	{
		return super.getItemValue() * 1.05;//super because getItemValue is part of parent class
	}

	// Simply gets the base class's value, and figures out the 5% restocking fee only
	public double getRestockingFee() 
	{
		return super.getItemValue() * .05;
	}
}


Then is my Inventory wrapper class..
//Inventory program that uses Product.java to create an array 
//of Product objects that can be used to modify inventory and get total $$$
// Import the format class to format values into currency format.
import java.text.DecimalFormat;

// Inventory class, used to manage a bunch of Product classes.

public class Inventory 
{
	// Setup an array of Products (set it to hold 20 items)
	int inventorySize = 20;
	private Product items[] = new Product[inventorySize];//set constant size of 20 to new product as argument.

	// Set the formatter to format values into currency form.
	DecimalFormat formatter = new DecimalFormat("$##,###.00");

	// Adds a product to the array of products. Adds to first empty slot found.
	public void addProduct(Product item) 
	{
		for (int i = 0; i < inventorySize; i++) //increment element number after fills with data
		{
			if (items[i] == null) 
			{
				items[i] = item;
				return;
			}
		}
	}

	// Loop through array of products and add up the total value.
	// Go item by item adding the quantity on hand * its price.
	// Add that value to a running total accumulator variable.

	public double getTotalInvValue() 
	{
		double sumOfInventory = 0.0;//declare and initialize variable to 0.0
	   
		// Uses a condensed for loop which iterates the array of items.
		for (Product item : items) 
		{
			// Make sure we have an item for the given position.
			if (item != null) 
			{
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}

	// Prints the inventory list including name, quantity, price, and total stock value for each item.
	public void printInventory() 
	{
		System.out.println("Printing items in inventory...\n");

		boolean hasItems = false;//declare and initalize variable to false

		for (Product item : items) 
		{
			if (item != null) 
			{
				hasItems = true;
				System.out.println(item.toString() + " Quantity: " + item.getQuantityOnHand() + " Value of Stock: " + formatter.format(item.getItemValue()));
			}
		}  

		// If no items were found, print a message saying the inventory is empty.
		if (!hasItems) { System.out.println("Inventory is empty at the moment.\n"); }
	}
}


and then we come to my first post, the InventoryGUI. All the methods I use are created in these other java files, and they all work! I just can't seem to get the data from them into my InventoryGUI.. or at least display in there.. Thanks for the help, in advance.

This post has been edited by Galnoth: 19 June 2009 - 05:00 PM

Was This Post Helpful? 0
  • +
  • -

#4 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 05:45 PM

Just a few things. Firstly, all you are doing is providing constructors and methods as to how you want your Objects created, you havnt actually created any Objects yet, just provided their 'template'.
Secondly, in your Inventory class, your making Arrays and setting all your methods to the super class's item e.g.
public void addProduct(Product item)
	{



There, you want to pass the addProduct method a Product Object. Are you sure this shouldnt be a DVD Object, which is a Product with your extra variabels. If its just a Product you want created for this part, your items will not have a MovieYear.

Now this is what you have to decide. Do you want the users of your system to create these Products or are you going to create them yourself? Looking at your gui i presume you need to create them yourself. I dont know if you want to do this in a new class or just do it through a method? Ok, maybe in your inventory class, you should add a method called defaultData
public void defaultData(){
}


Then inside this method, you need to create your default Objects. I will give you an example
Product productOne = new Product(001, "Basketball Diaries", 4, 3.45);

Now that has created me a Product Object called productOne which holds the data i have just provided. Now, you want to add this Product to your array. So underneath it, you can do somthing like
this.addProduct(productOne);


Now that will add the product to your array. You continue making as many default products as you need and add them to your Array. We will discuss getting them in your gui after.
If I was you however, I would scrap your addProduct method just incase it mistakenly skips an index. To make sure you do everything correct, you can place it in at the correct indexes yourself e,g,
Product productOne = new Product(001, "Basketball Diaries", 4, 3.45);
items[0]=productOne;


Just remember though, you need to decide whether you intend to add a Product Object or a DVD Object. Hope this helps for now.
Was This Post Helpful? 1
  • +
  • -

#5 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 07:04 PM

Ok. Thanks for the advice, which I followed :D I would like to create DVD objects so it will display the added feature of the year. I also took out the for loop to add objects to the array like you suggested. This is what I did so far. I wanted to load the array with 5 DVD's. Can they go in the product array? Should right, since DVD extends product?
public void defaultData()
	{
		Product DVD1 = new Product( 001, "Saw", 4, 9.95 );
		items[0] = DVD1;
		
		Product DVD2 = new Product( 002, "Saw 2", 1, 5.99 );
		items[1] = DVD2;
		
		Product DVD3 = new Product( 003, "Saw 3", 3, 7.39 );
		items[2] = DVD3;
		
		Product DVD4 = new Product( 004, "Saw 4", 7, 15.99 );
		items[3] = DVD4;
		
		Product DVD5 = new Product( 005, "Saw 5", 7, 15.99 );
		items[4] = DVD5;
	}


Saw is awesome, by the way. If the DVD objects can go into the Product array, should I add the year to the argument somewhere?
Was This Post Helpful? 0
  • +
  • -

#6 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 07:20 PM

Basically, Product acts as your template, although if your only dealing with a DVD, you might as well add the year variable into the product class. But keep it like it is as it shows you know inheritence. When creating a DVD, you want to create a DVD then like so
public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);


You can then add this to a Product Array
public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);
		items[0] = dvd1;



What you might want to do is make your Product class abstract, but we leave it for now. Also, remember you set your Array size to 20. This means that if you test it with 5 items you will get a nullPointerException. So either change the size to 5 for now or create 20 items.
Was This Post Helpful? 1
  • +
  • -

#7 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 07:36 PM

This is cool. I'm just a beginner. I've not learned about "this.<Object>" or what you said, abstract. I don't know about these yet. But I did change the Products to DVDs and added the year in the argument. Also, I changed the array size to 5. I knew you couldn't change the size once declared, but I didn't know you had to fill it. Is it the same for others, like JTextField? So now this creates an array of DVD's called items[], and this is an array of products. So that will allow defaultData to use Product and DVD's methods to "get" their data?
int inventorySize = 5;
	private Product items[] = new Product[inventorySize];//set constant size of 5 to new product as argument.

	public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);
		items[0] = dvd1;
		
		DVD dvd2 = new DVD( 002, "Saw 2", 2, 5.99, 2003 );
		items[1] = dvd2;
		
		DVD dvd3 = new DVD( 003, "Saw 3", 3, 7.39, 2004 );
		items[2] = dvd3;
		
		DVD dvd4 = new DVD( 004, "Saw 4", 8, 15.99, 2006 );
		items[3] = dvd4;
		
		DVD dvd5 = new DVD( 005, "Saw 5", 7, 15.99, 2008 );
		items[4] = dvd5;
	}

Was This Post Helpful? 0
  • +
  • -

#8 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 07:50 PM

View PostGalnoth, on 19 Jun, 2009 - 06:36 PM, said:

So that will allow defaultData to use Product and DVD's methods to "get" their data?

Yes.
What I would do now is add a toString method at the bottom of your Product class
public String toString(){
String details = "Item Num "+ productId+ "\n" +
					   "Name " + itemname + "\n" +
					   "Quantity " + quantityOnHand + "\n" +
						"price " + itemprice + "\n";
return details;
}

You will need to allign it properly for nice output, its not alligning correctly when i post it.
Then in your DVD class, add another toString
public String toString(){
String details = super.toString(); 
	  details += "Movie Date " + movieyear +"\n";
return details;
}


Then in your inventory class, near the bottom just add a main method to make sure everything got added to your Array properly
public static void main(String[] args){
Inventory myInventory = new Inventory();
myInventory.printInventory();
}


In your printInventory method, remove this
 + " Quantity: " + item.getQuantityOnHand() + " Value of Stock: " + formatter.format(item.getItemValue())

for now so your just printing out the items.

This post has been edited by nick2price: 19 June 2009 - 07:51 PM

Was This Post Helpful? 0
  • +
  • -

#9 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 08:13 PM

Alright, I did what you said but Inventory won't compile. I get errors:
javac Product.java
Process started >>>
Product.java:81: cannot find symbol
symbol  : variable productId
location: class Product
		String details = "Item Num "+ productId+ "\n" +
									  ^
Product.java:82: cannot find symbol
symbol  : variable itemname
location: class Product
			"Name " + itemname + "\n" + "Quantity " + quantityOnHand + "\n" + "price " + itemprice + "\n";
					  ^
Product.java:82: cannot find symbol
symbol  : variable quantityOnHand
location: class Product
			"Name " + itemname + "\n" + "Quantity " + quantityOnHand + "\n" + "price " + itemprice + "\n";
													  ^
Product.java:82: cannot find symbol
symbol  : variable itemprice
location: class Product
			"Name " + itemname + "\n" + "Quantity " + quantityOnHand + "\n" + "price " + itemprice + "\n";
																						 ^
4 errors
<<< Process finished.


That means that it can't find those variable inside Inventory. Does that mean the "Inventory myinventory" needs to be changed and called something different? All the files won't compile with the same errors. Product, DVD, and Inventory.
Was This Post Helpful? 0
  • +
  • -

#10 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 08:26 PM

Them errors are coming from your toString methods, You did place them in your prodcut and dvd class didnt you? And not the Inventory class
Was This Post Helpful? 0
  • +
  • -

#11 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 08:34 PM

View Postnick2price, on 19 Jun, 2009 - 07:26 PM, said:

Them errors are coming from your toString methods, You did place them in your prodcut and dvd class didnt you? And not the Inventory class


Right. Got a toString in Product that concentates(?) itemName, itemPrice, etc. variables. Then one in DVD to return the year, and NOT one in Inventory. Inventory has the defaultData that is supposed to fill in the array. Is that not working somehow?
Was This Post Helpful? 0
  • +
  • -

#12 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 08:38 PM

So you added one toString method i gave u in earlier post to the product class, and the other to dvd class. There should be no toString method in the inventory class. All the toString method is doing is making it neat when you print it out. So go through that earlier post i done, and make sure you follow it, by removing what i said from your printInventory method. Then add the main method to the bottom of your inventory class and see if it works
Was This Post Helpful? 0
  • +
  • -

#13 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 08:52 PM

OK. I went back through your previous posts. Here is my updated code:
Product.java
//This is the Product.java class.  It is the methods for the Product object used in Inventory.java

public class Product 
{
	// Private variables
	private String name;
	private int quantity;
	private double price;
	private int productid = 0;

	// Default constructor uses other constructor to set the default state of the object (instantiate).

	public Product() 
	{
		this(0,"Unknown",0,0.00);
	}

	// Constructor that user can specify a name, quantity, and price for items.
	public Product(int productId, String itemname, int quantityOnHand, double itemprice) 
	{
		productid = productId;
		setName(itemname);
		setQuantityOnHand(quantityOnHand);
		setPrice(itemprice);
	}


	// Public mutators (changes state of the object)
	public void setName(String itemname) 
	{
		name = itemname;
	}


	// Sets quantity on hand and if negative, defaults to zero.
	public void setQuantityOnHand(int quantityOnHand) 
	{
		if (quantityOnHand > 0) 
		{
			quantity = quantityOnHand;
		}
		else { quantity = 0; }
	}


	// Set price of a product and defaults it to zero if negative.
	public void setPrice(double itemPrice) 
	{
		if (itemPrice > 0.00) 
		{
			price = itemPrice;
		}
		else { price = 0.00; }
	}

	// Get the product's name
	public String getName() 
	{
		return name;
	}

	public int getQuantityOnHand() 
	{
		return quantity;
	}

	public double getPrice() 
	{
		return price;
	}

	// Calculate the value of stock on this particular item.
	public double getItemValue() 
	{
		return (price * (double)quantity);
	}

	// String representation of the product
	public String toString()
	{
		String details = "Item Num "+ productId+ "\n" +
			"Name " + itemName + "\n" + "Quantity " + quantityOnHand + "\n" + "price " + itemPrice + "\n";
		return details;
	}
}

toString that makes a string of all the variables.
DVD.java
// Inherited class DVD from the base class Product
// It is a product, but has the extra feature of having a year the movie was made.

public class DVD extends Product //creates subclass from parent class Product
{
	// Holds the year the move was made
	private int movieyear;

	// Constructor, it calls the constructor of Product first (parent class)
	// Passing the parent class its values to initialize the class BEFORE
	// setting this instance year variable.

	public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year) 
	{
		// Pass on the values needed for creating the Product class first thing
		super(productId, itemname, quantityOnHand, itemprice);//super because it references the parent class
		movieyear = year;
	}

	// In case user wants to set the year manually
	public void setYear(int year) 
	{
		movieyear = year;
	}

	// Get the year of this DVD product
	public int getYear() 
	{
		return movieyear;
	}

	// Overrides getItemValue() in Product class by calling the parent class version and
	// adding a 5% restocking fee on top
	public double getItemValue() 
	{
		return super.getItemValue() * 1.05;//super because getItemValue is part of parent class
	}

	// Simply gets the base class's value, and figures out the 5% restocking fee only
	public double getRestockingFee() 
	{
		return super.getItemValue() * .05;
	}
	
	public String toString()
	{
		String details = super.toString();
		details += "Movie Date " + movieyear +"\n";
		return details;
	}
}

toString that super to Product movieyear (I think I only called it year?)
Inventory.java
//This is Mike Fasulo's WK6 CP Inventory program that uses Product.java to create an array 
//of Product objects that can be used to modify inventory and get total $$$
// Import the format class to format values into currency format.
import java.text.DecimalFormat;

// Inventory class, used to manage a bunch of Product classes.

public class Inventory 
{
	// Setup an array of Products (set it to hold 5 items)
	int inventorySize = 5;
	private Product items[] = new Product[inventorySize];//set constant size of 5 to new product as argument.

	public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);
		items[0] = dvd1;
		
		DVD dvd2 = new DVD( 002, "Saw 2", 2, 5.99, 2003 );
		items[1] = dvd2;
		
		DVD dvd3 = new DVD( 003, "Saw 3", 3, 7.39, 2004 );
		items[2] = dvd3;
		
		DVD dvd4 = new DVD( 004, "Saw 4", 8, 15.99, 2006 );
		items[3] = dvd4;
		
		DVD dvd5 = new DVD( 005, "Saw 5", 7, 15.99, 2008 );
		items[4] = dvd5;
	}
	
	// Set the formatter to format values into currency form.
	DecimalFormat formatter = new DecimalFormat("$##,###.00");
	
	

	// Loop through array of products and add up the total value.
	// Go item by item adding the quantity on hand * its price.
	// Add that value to a running total accumulator variable.

	public double getTotalInvValue() 
	{
		double sumOfInventory = 0.0;//declare and initialize variable to 0.0
	   
		// Uses a condensed for loop which iterates the array of items.
		for (Product item : items) 
		{
			// Make sure we have an item for the given position.
			if (item != null) 
			{
				sumOfInventory += item.getItemValue();
			}
		}
		return sumOfInventory;
	}

	// Prints the inventory list including name, quantity, price, and total stock value for each item.
	public void printInventory() 
	{
		System.out.println("Printing items in inventory...\n");

		boolean hasItems = false;//declare and initalize variable to false

		for ( Product item : items ) 
		{
			if ( item != null ) 
			{
				hasItems = true;
				System.out.println( item.toString() );// + " Quantity: " + item.getQuantityOnHand() + " Value of Stock: " + formatter.format(item.getItemValue()));
			}
		}  

		// If no items were found, print a message saying the inventory is empty.
		if ( !hasItems ) { System.out.println( "Inventory is empty at the moment.\n" ); }
	}
	
	public static void main(String[] args)
	{
		Inventory myInventory = new Inventory();
		myInventory.printInventory();
	}
}

I commented out the stuff you said remove and finished the println to print item.toString's arguments... should that be items because that was the name of the array? Is that the problem, because these won't compile?
Was This Post Helpful? 0
  • +
  • -

#14 nick2price  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 561
  • View blog
  • Posts: 2,826
  • Joined: 23-November 07

Re: Get Methods and GUI

Posted 19 June 2009 - 09:10 PM

Ok, I chopped out a couple of methods in your inventory class for now and changed your print method to make it easier. In your product and dvd class's, there was just a few incorrect variable names. These are correct
public class Product
{
	// Private variables
	private String name;
	private int quantity;
	private double price;
	private int productid = 0;

	// Default constructor uses other constructor to set the default state of the object (instantiate).

	public Product()
	{
		this(0,"Unknown",0,0.00);
	}

	// Constructor that user can specify a name, quantity, and price for items.
	public Product(int productId, String itemname, int quantityOnHand, double itemprice)
	{
		productid = productId;
		setName(itemname);
		setQuantityOnHand(quantityOnHand);
		setPrice(itemprice);
	}


	// Public mutators (changes state of the object)
	public void setName(String itemname)
	{
		name = itemname;
	}


	// Sets quantity on hand and if negative, defaults to zero.
	public void setQuantityOnHand(int quantityOnHand)
	{
		if (quantityOnHand > 0)
		{
			quantity = quantityOnHand;
		}
		else { quantity = 0; }
	}


	// Set price of a product and defaults it to zero if negative.
	public void setPrice(double itemPrice)
	{
		if (itemPrice > 0.00)
		{
			price = itemPrice;
		}
		else { price = 0.00; }
	}

	// Get the product's name
	public String getName()
	{
		return name;
	}

	public int getQuantityOnHand()
	{
		return quantity;
	}

	public double getPrice()
	{
		return price;
	}

	// Calculate the value of stock on this particular item.
	public double getItemValue()
	{
		return (price * (double)quantity);
	}

	// String representation of the product
	public String toString()
	{
		String details = "Item Num "+ productid+ "\n" +
			"Name " + name + "\n" + "Quantity " + quantity + "\n" + "price " + price + "\n";
		return details;
	}
}



public class DVD extends Product //creates subclass from parent class Product
{
	// Holds the year the move was made
	private int movieyear;

	// Constructor, it calls the constructor of Product first (parent class)
	// Passing the parent class its values to initialize the class BEFORE
	// setting this instance year variable.

	public DVD(int productId, String itemname, int quantityOnHand, double itemprice, int year)
	{
		// Pass on the values needed for creating the Product class first thing
		super(productId, itemname, quantityOnHand, itemprice);//super because it references the parent class
		movieyear = year;
	}

	// In case user wants to set the year manually
	public void setYear(int year)
	{
		movieyear = year;
	}

	// Get the year of this DVD product
	public int getYear()
	{
		return movieyear;
	}

	// Overrides getItemValue() in Product class by calling the parent class version and
	// adding a 5% restocking fee on top
	public double getItemValue()
	{
		return super.getItemValue() * 1.05;//super because getItemValue is part of parent class
	}

	// Simply gets the base class's value, and figures out the 5% restocking fee only
	public double getRestockingFee()
	{
		return super.getItemValue() * .05;
	}
   
	public String toString()
	{
		String details = super.toString();
		details += "Movie Date " + movieyear +"\n";
		return details;
	}
}



And in your main method in your inventory class, i forgot to tell you to call your defaultData method. Because remember, its a method, so if its never called the data is never put in. This works now
import java.text.DecimalFormat;

// Inventory class, used to manage a bunch of Product classes.

public class Inventory
{
	// Setup an array of Products (set it to hold 5 items)
	int inventorySize = 5;
	private Product[] items = new Product[inventorySize];//set constant size of 5 to new product as argument.

	public void defaultData()
	{
		DVD dvd1 = new DVD( 001, "Saw", 4, 9.95, 2002);
		items[0] = dvd1;
	   
		DVD dvd2 = new DVD( 002, "Saw 2", 2, 5.99, 2003 );
		items[1] = dvd2;
	   
		DVD dvd3 = new DVD( 003, "Saw 3", 3, 7.39, 2004 );
		items[2] = dvd3;
	   
		DVD dvd4 = new DVD( 004, "Saw 4", 8, 15.99, 2006 );
		items[3] = dvd4;
	   
		DVD dvd5 = new DVD( 005, "Saw 5", 7, 15.99, 2008 );
		items[4] = dvd5;
	}
  
	// Prints the inventory list including name, quantity, price, and total stock value for each item.
	public void printInventory()
	{
		System.out.println("Printing items in inventory...\n");

		for (int x=0; x<items.length; x++)
		{
			System.out.println(items[x].toString());
		}

	}
   
	public static void main(String[] args)
	{
		Inventory myWork = new Inventory();
		myWork.defaultData();
		myWork.printInventory();
	}
}



Just notice the change i made to the print method for now and what i done in the main method
Was This Post Helpful? 0
  • +
  • -

#15 Galnoth  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 18
  • Joined: 14-June 09

Re: Get Methods and GUI

Posted 19 June 2009 - 09:28 PM

That's great! It works and prints the items array that has the DVD's in it. Unfortunately, we took out the code that does the calculations for TotalInvValue and RestockingFee. With this array of products/DVD's called items[]; I can call the "get" methods in it to display the data into a JTextField?
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3