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:

// 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.

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,

Re: Get Methods and GUI

Posted 19 June 2009 - 07:04 PM

Ok. Thanks for the advice, which I followed 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?

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

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.

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?

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.

Re: Get Methods and GUI

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?

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

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?

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

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?