7 Replies - 2871 Views - Last Post: 02 May 2010 - 12:34 PM Rate Topic: -----

#1 BlondieSweetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-May 10

Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 06:48 AM

Hello, I am new at this and I am having a problem with an Inventory Program Part 2 for my Java Class. I have almost everything for the code working the way it should be however, I am supposed to code a class instance variable to recieve a value from a method. The problem I am having is the method is supposed to calculate the value of a cd (price * in stock units) and then pass it to the variable. The instructor wants me to code a method that accepts parameters, but everytime I try I get "red flagged" by NetBeans. I do not understand why this is not working. The Instructor would also like the program to display the value of the entire Inventory after I display the value of the Product. Here are the three files that I have:

/*Program: Inventory Program
 * File: Product.java
 * Summary: Sets the product information, Also retrieves and displays the objects information
 * Author: Joelene Wells
 * Date: April 30, 2010
 */
import java.text.NumberFormat;
public class Product {

//begin product class
NumberFormat cf = NumberFormat.getCurrencyInstance();
//declare instance variables
     String productName; //item's name
     private int itemNumber;//item's number
     private double itemPrice; //item's price
     private int stockUnits; //number of units in stock of the item
     private double productValue;//individual cd's Value
     private double totalValue;//total value of all inventory

     //creates product constructor
     public Product(String setProductName, int setItemNumber, int setItemPrice, int setStockUnits){
       productName = setProductName; //product name from the inventory class
       itemNumber = setItemNumber; //sets product nunber from inventory class
       itemPrice = setItemPrice; //sets product price from inventory class
       stockUnits = setStockUnits; //sets stock units from inventory class

     }

    public void setProductName(String nameIn){
         this.productName = nameIn;//set's item name and passes it to the method
    }//end setProductName method

    public String getProductName (){
        return this.productName;//returns product name
    }//end getProductName method

    public void setItemNumber (int productNumber){
        this.itemNumber = productNumber;//sets product number from method
    }//end setItemNumber method

    public int getItemNumber () {
         return this.itemNumber; //returns product number
    }//end getItemNumber method

    public void setItemPrice (double productPrice){
        this.itemPrice = productPrice;//sets product price from method
    }//end setItemPrice method

    public double getItemPrice (){
        return this.itemPrice;//returns product price
    }//end getItemPrice method

    public void setStockUnits (int inStockUnits){
        this.stockUnits = inStockUnits;// sets in stock units from method
    }//end setStockUnits method

    public int getStockUnits (){
        return this.stockUnits;//returns in stock units
    }//end getStockUnits method

    public double getValue (){
       return getStockUnits() * getItemPrice();

    }
   

   public void displayInventoryInfo(){
        System.out.println();//blank line easier to read
        System.out.println("Name: " + productName);//prints products name
        System.out.println("Product Number: " + itemNumber);//prints item's number
        System.out.printf("Item Price: " + cf.format(itemPrice));//prints the item price
        System.out.println();//print blank line
        System.out.println("Units In Stock: " + stockUnits);//prints the number of instock units
        System.out.printf("Total Value: "+ cf.format(productValue));//prints the total value of stock
        System.out.println();//print blank line
        System.out.printf("Value of all Inventory: "+ cf.format(totalValue));//prints the value of all the inventory
        System.out.println();//blank line for readability
     }//end displayInventory Method

 }//end product class



/* Program: Inventory Program
 * File:Inventory.java
 * Summary: This program is creating an array of objects and
 * Author: Joelene Wells
 * Date: April 30, 2010
 */

import java.util.Arrays; //imports the array package for usage
public class Inventory {
//start inventory class

    public static void main(String[] args) {
    Product cd[] = new Product[5];//creates the array
    //instantiates the Product class
    Product cd1 = new Product("Mariah Carey", 1205, 13, 130);//creats an instance of the product class and instantiates the class instance variables
    Product cd2 = new Product("Ludacris", 1230, 10, 50);//creates an instance of the product class and instantiats the calss instance variables
    Product cd3 = new Product("Justin Timberlake", 1250, 18, 100);//creates an instnce of the product class and instantiate the class instnace variables
    Product cd4 = new Product("Jennifer Lopez", 1270, 14, 185); //creates an instance of the product class and instatiate the class instance variable
    Product cd5 = new Product("DMX", 2000, 20, 150);//creates an instance of the product class and instantiates the class instance variables

    // add the cd objects to the array
    cd[0] = cd1; //adds Mariah Carey's cd to the array
    cd[1] = cd2; //adds Ludacris's cd to the array
    cd[2] = cd3; //adds Justin Timberlake's cd to the array
    cd[3] = cd4; //adds Jennifer Lopez's cd to the array
    cd[4] = cd5; //adds DMX's cd to the array

    //sort the array and display the information on the console
    Arrays.sort(cd, new CompareCDNames()); //sorts the cd Array

    System.out.println("Displaying CD's In Order by Name: ");//tell the user that the items are in order
    
    for (int i = 0; i < cd.length; i++){
        cd[i].displayInventoryInfo();//displays the information in the Product class
    }//end displaying of the inventory information

      
    }//end main method

}//end inventory class


/*Program: Inventory Program
 *File: CompareCDName.java
 *Summary: Compare objects that sorts the CD's by the name
 *Author: Joelene Wells
 * Date: April 30, 2010
 */

import java.util.Comparator;//imports the java comparator to compare names
public class CompareCDNames implements Comparator {
    //being CompareCDNames Class
    public int compare (Object cd1, Object cd2)// creates two CD objects to compare the CD names
    { String name1 = ((Product)cd1).getProductName().toUpperCase();//gets the first name from the Product Class
      String name2 = ((Product)cd2).getProductName().toUpperCase();//gets the second name from the Product Class
      // compare name1 with name 2
      if (!(name1.equals(name2)))
          return name1.compareTo(name2);
      else
          return name2.compareTo(name1);
    }//end compare method

}//end CompareCDNames class



Without trying to calculate the total inventory the files work. If there is a way someone can point me in the right direction I would greatly appreciate it.

Is This A Good Question/Topic? 0
  • +

Replies To: Inventory Program Part 2 Using a Variable

#2 japanir  Icon User is offline

  • jaVanir
  • member icon

Reputation: 1010
  • View blog
  • Posts: 3,025
  • Joined: 20-August 09

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 07:42 AM

Where is the method in charge of calculating the total?
Post the current version of that method, along with any error messages you may get.
it is hard to say what is the problem without seeing that method.
Was This Post Helpful? 0
  • +
  • -

#3 BlondieSweetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-May 10

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 08:46 AM

I am sorry for the confusion, here is the method that I thought would take care of the problem, but to no avail. I do not get any error messages however on output it posts the total value of the individual cd as 0 and the total value of the inventory as 0.

 public  void setValue(double inStockUnits, double stockPrice){
        inStockUnits = stockUnits;
        stockPrice = itemPrice;
        productValue = inStockUnits * stockPrice;
    }

    public double getValue (){
        return productValue;
    }

    public double getTotalValue(){
        return totalValue = totalValue + productValue;
    }


Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 08:55 AM

Your problem is that you do not initialize these two variables before they are used in the toString() method, so they take on the value of 0. Do you want totalValue to represent the total value for all CDs or just the current product? Should productValue represent the unitPrice * quantity for a given CD? I think you need to rework your design some. A more OO design would be to have an Store class which manages your Products; including adding, removing, updating attributes, and storing total inventory price. You can also make totalValue a static variable and update it in the contsructor with the totalValue for an item, which is bad design. If productValue represents the total value for an Item, you should probably initialize it using params from the constructor.
private double productValue;//individual cd's Value 
private double totalValue;//total value of all inventory 


Was This Post Helpful? 0
  • +
  • -

#5 BlondieSweetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-May 10

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 09:30 AM

Hello, Okay here is what I understand, I need to initialize the variable somehow, then I have to get that variable to store the value that is calculated in this method
 public double getValue (){
        return getStockUnits()* getItemPrice();
    }



What I do not understand is how to initialize that variable. All the information for each of the cd's is stored within an array in the Inventory class
/* Program: Inventory Program
 * File:Inventory.java
 * Summary: This program is creating an array of objects and
 * Author: Joelene Wells
 * Date: April 30, 2010
 */

import java.util.Arrays; //imports the array package for usage
public class Inventory {
//start inventory class

    public static void main(String[] args) {
    Product cd[] = new Product[5];//creates the array
    //instantiates the Product class
    Product cd1 = new Product("Mariah Carey", 1205, 13, 130);//creats an instance of the product class and instantiates the class instance variables
    Product cd2 = new Product("Ludacris", 1230, 10, 50);//creates an instance of the product class and instantiats the calss instance variables
    Product cd3 = new Product("Justin Timberlake", 1250, 18, 100);//creates an instnce of the product class and instantiate the class instnace variables
    Product cd4 = new Product("Jennifer Lopez", 1270, 14, 185); //creates an instance of the product class and instatiate the class instance variable
    Product cd5 = new Product("DMX", 2000, 20, 150);//creates an instance of the product class and instantiates the class instance variables

    // add the cd objects to the array
    cd[0] = cd1; //adds Mariah Carey's cd to the array
    cd[1] = cd2; //adds Ludacris's cd to the array
    cd[2] = cd3; //adds Justin Timberlake's cd to the array
    cd[3] = cd4; //adds Jennifer Lopez's cd to the array
    cd[4] = cd5; //adds DMX's cd to the array

    //sort the array and display the information on the console
    Arrays.sort(cd, new CompareCDNames()); //sorts the cd Array

    System.out.println("Displaying CD's In Order by Name: ");//tell the user that the items are in order
    
    for (int i = 0; i < cd.length; i++){
        cd[i].displayInventoryInfo();//displays the information in the Product class
    }//end displaying of the inventory information

      
    }//end main method

}//end inventory class


The productValue variable is the value of the each collection of cd's
The totalValue variable is the value of all the cd's in stock.
Was This Post Helpful? 0
  • +
  • -

#6 BlondieSweetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-May 10

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 11:47 AM

Hello,
Okay I have gotten the variable productValue to hold the value I need it to. Now how do I get it to add that value to the next value for the next cd. For example, I have the productValue = 1690, for Mariah Carey cd, and I want to add the productValue for Justin Timberlake's cd (value = 1800). Each time I compile the program I get the same output for the value of each CD collection and the Value of the entire Inventory collects (ie Total Value: 1800
Total Inventory Value: 1800).
/*Program: Inventory Program
 * File: Product.java
 * Summary: Sets the product information, Also retrieves and displays the objects information
 * Author: Joelene Wells
 * Date: April 30, 2010
 */
import java.text.NumberFormat;
public class Product {

//begin product class
NumberFormat cf = NumberFormat.getCurrencyInstance();
//declare instance variables
     String productName; //item's name
     private int itemNumber;//item's number
     private double itemPrice; //item's price
     private int stockUnits; //number of units in stock of the item
     private double productValue;//individual cd collections value
     private double totalValue;//total value of all inventory

     //creates product constructor
     public Product(String setProductName, int setItemNumber, int setItemPrice, int setStockUnits){
       productName = setProductName; //product name from the inventory class
       itemNumber = setItemNumber; //sets product nunber from inventory class
       itemPrice = setItemPrice; //sets product price from inventory class
       stockUnits = setStockUnits; //sets stock units from inventory class
       }// end product constructor

    public void setProductName(String nameIn){
         this.productName = nameIn;//set's item name and passes it to the method
    }//end setProductName method

    public String getProductName (){
        return this.productName;//returns product name
    }//end getProductName method

    public void setItemNumber (int productNumber){
        this.itemNumber = productNumber;//sets product number from method
    }//end setItemNumber method

    public int getItemNumber () {
         return this.itemNumber; //returns product number
    }//end getItemNumber method

    public void setItemPrice (double productPrice){
        this.itemPrice = productPrice;//sets product price from method
    }//end setItemPrice method

    public double getItemPrice (){
        return this.itemPrice;//returns product price
    }//end getItemPrice method

    public void setStockUnits (int inStockUnits){
        this.stockUnits = inStockUnits;// sets in stock units from method
    }//end setStockUnits method

    public  int getStockUnits (){
        return this.stockUnits;//returns in stock units
    }//end getStockUnits method

    public double getItemValue(){
        return productValue = stockUnits * itemPrice;
    }

    public void getTotalValue(double valueTotal){
        valueTotal = totalValue + productValue;
        totalValue = valueTotal;
    }//end getTotalValue method

    public double sumOfTotalValue(){
        return totalValue = totalValue + productValue;
    }

   public void displayInventoryInfo(){
        System.out.println();//blank line easier to read
        System.out.println("Name: " + productName);//prints products name
        System.out.println("Product Number: " + itemNumber);//prints item's number
        System.out.printf("Item Price: " + cf.format(itemPrice));//prints the item price
        System.out.println();//print blank line
        System.out.println("Units In Stock: " + stockUnits);//prints the number of instock units
        System.out.printf("Total Value: "+ cf.format(getItemValue()));//prints the total value of stock
        System.out.println();//print blank line
        System.out.printf("Value of all Inventory: "+ cf.format(sumOfTotalValue()));//prints the value of all the inventory
        System.out.println();//blank line for readability
     }//end displayInventory Method

 }//end product class



Could you please show me where I am going wrong with this?
Was This Post Helpful? 0
  • +
  • -

#7 macosxnerd101  Icon User is offline

  • Self-Trained Economist
  • member icon




Reputation: 10464
  • View blog
  • Posts: 38,783
  • Joined: 27-December 08

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 12:12 PM

View Postmacosxnerd101, on 02 May 2010 - 11:55 AM, said:

A more OO design would be to have an Store class which manages your Products; including adding, removing, updating attributes, and storing total inventory price. You can also make totalValue a static variable and update it in the contsructor with the totalValue for an item, which is bad design.

By making totalValue static, it will be accessible and the same for all instances of the Product class. It's like the laws in a country- they apply to all citizens, not just certain ones. So for example:
class Count{
   static int x = 0;
   Count(){
 
        //each time a new Count object
        //is instantiated, the static
        //variable will be incremented by 1
        //to count the total number of 
        //Count objects that have been instantiated
        x++;
        System.out.println(x);
   }

   public static void main(String[] args){
         new Count(); //1 will be printed
         new Count(); //2 will be printed
   }
} 


Was This Post Helpful? 2
  • +
  • -

#8 BlondieSweetz  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 6
  • Joined: 02-May 10

Re: Inventory Program Part 2 Using a Variable

Posted 02 May 2010 - 12:34 PM

Thank You so much for your help. I feel like a complete bonehead because I made it more difficult then it was. Thank you again.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1