Hello everyone. I am having a lot of trouble in a class, and I am wondering if I can get some input/advice with my part one inventory program. I have already submitted this assignment, but I am going to have to modify it on Wednesday. My instructor has not graded my assignment yet or answered any questions.

Attachments
/** Program: Inventory Program Part 1
 * File: InventoryProgram.java
 * Summary: An application that displays the product number, the name of the product, 
 * the number of units in stock, the price of each unit, and the value of the inventory.
 * Author: Monica Sanchez
 * Date: October 16, 2014
 **/

public class InventoryProgram { //Beginning of InventoryProgram class
    
    public static void main(String[] args) { //Beginning of main method
        
        OfficeSupplies officeSupplies = new OfficeSupplies ();
        
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplie information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    } //End of main method
    
} //End of InventoryProgram class

You haven't included the called class so we can't see what that does but is there a reason why you called the same method so many times?

sigh... Just ask one of the many others who posted the same assignment here waiting for someone to do the work for them...

I am not asking for anyone to do my assignment. I am just asking for advice.

public class InventoryProgram { //Beginning of InventoryProgram class

    public static void main(String[] args) { //Beginning of main method

        OfficeSupplies officeSupplies = new OfficeSupplies ();

    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplie information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    System.out.println(); // Print blank line for readability
    officeSupplies.displayOfficeSuppliesInfo(); //Begin display officeSupplies information
    } //End of main method

} //End of InventoryProgram class

import java.text.NumberFormat;

public class OfficeSupplies { //Beginning of OfficeSupplies class

    //Declare instance variables
    private int itemNumber; //Item number
    private String productName; //Name of product
    private int unitsInStock; // Number of units in stock
    private double priceOfEachUnit; // Price of each unit
    private double valueOfInventory; // Value of inventory, multiply units in stock and price of each unit
    private NumberFormat nf = NumberFormat.getCurrencyInstance(); // Used to format currency

    //Declare empty constructor
public OfficeSupplies(){} // Empty Constructor

    //Declare OfficeSupplies constructor that accepts parameters
public OfficeSupplies(int itemNumberIn, String productNameIn, int unitsInStockIn, double priceOfEachUnitIn,
        double valueOfInventoryIn, NumberFormat nfIn){ //Beginning of OfficeSupplies constructor that initializes class instance variables
    itemNumber = itemNumberIn; // Assign the value of itemNumberIn to itemNumber class instance variable
    productName = productNameIn; // Assign the value of productNameIn to productName class instance variable
    unitsInStock = unitsInStockIn; //Assign the value of unitsItStockIn to unitsInStock class instance variable
    priceOfEachUnit = priceOfEachUnitIn; //Assign the value of priceOfEachUnitIn to priceOfEachUnit class instance variable
    valueOfInventory = valueOfInventoryIn; //Assign the value of valueOfInventoryIn to valueOfInventory class instance variable
} //End of OfficeSupplies constructor

    //Create set and get methods for each class instance variable
public void setitemNumber (int itemNumberIn){ //Beginning of set method
    this.itemNumber = itemNumberIn; // set class instance variable
} //End set method

public int getitemNumber(){ //Beginning of get method
    return this.itemNumber; //Get the value of the class instance variable
} //End get method

public void setproductName (String productNameIn){ // Beginning of set method
    this.productName = productNameIn; //Get the value of the class instance variable
} //End of method

public String getproductName(){ //Beginning of get method
    return this.productName; //get the value of the class instance variable
} //End get method

public void setunitsInStock (int unitsInStockIn){ //Beginning of set method
    this.unitsInStock = unitsInStockIn; //Set class instance variable
} //End set method

public int getunitsInStock(){ //Beginning of get method
    return this.unitsInStock; //Get the value of the class instance variable
} //End get method

public void setpriceOfEachUnit (double priceOfEachUnitIn){ //Beginning of set method
    this.priceOfEachUnit = priceOfEachUnitIn; //Set class instance variable
} //End set method

public double getpriceOfEachUnit(){ //Beginning of get method
    return this.priceOfEachUnit; //Get the value of the class instance variable
} // End get method

//Calculate inventory value
public void setvalueOfInventory (double valueOfInventoryIn){ //Beginning of set method
    this.valueOfInventory = valueOfInventoryIn; //Set class instance variable
} //End set method

public double getvalueOfInventory(){ //Beginning of get method
    return priceOfEachUnit * unitsInStock; //Get the value of the class instance variable
} //End get method

public  NumberFormat getNf(){ //Beginning of getNF method
    return nf; //Value of instance variable
}
public void setNf (NumberFormat nf){ //Beginning of setNf method
    this.nf = nf; //Set instance variable name to name passed in method
}

public void displayOfficeSuppliesInfo (){ //Displays OfficeSupplies information to the console
    System.out.println("Product Number: " + getitemNumber());
    System.out.println("Product name: " + getproductName());
    System.out.println("Number of Units in Stock: " + getunitsInStock());
    System.out.println("Product Price: $ " + getpriceOfEachUnit());
    System.out.println("Product Inventory Value: $ " + getvalueOfInventory());
}

} //End of OfficeSupplies class

The reason I repeated the same code over and over again was because the instructions said not to include an array or loop statement. So I was confused on how I could repeat the code.

some recommendations:

throw away about all of the comments in that code.
just look at this part of your code:

 public class OfficeSupplies { //Beginning of OfficeSupplies class
    //Declare instance variables
    private int itemNumber; //Item number
    private String productName; //Name of product
    private int unitsInStock; // Number of units in stock
    private double priceOfEachUnit; // Price of each unit
    private double valueOfInventory; // Value of inventory, multiply units in stock and price of each unit
    private NumberFormat nf = NumberFormat.getCurrencyInstance(); // Used to format currency
    //Declare empty constructor
public OfficeSupplies(){} // Empty Constructor

it is riddled with comments, that don't add anything to your code, nor make it easier to read. the other way around, rather, there's so much useless comments there, that it's even harder to read it.
in the above snippet, there is not a single comment that should be there, since the names of the variables, and the concept of default constructor are all very well documented, and the names are well chosen.

for instance: private int itemNumber; //Item number
if you were to drop this comment ... do you think you'll no longer figure out what the variable is used for ?

as for your setters and getters .. again, drop the pointless comments, and respect the naming conventions, in the long run, it'll make your code a lot clearer:

public int getitemNumber(){ //Beginning of get method
    return this.itemNumber; //Get the value of the class instance variable
} //End get method

all three of the comments in this block, you repeat for each getter. For each setter, you do the same, save the second comment is replaced by a 'Set the value .. ' comment.
the code is self-explanatory, if you need comments, especially repeatedly, to see this, you'll soon end up with code that contains more lines of comments compared to the number of lines of actual code.

if then you go and ask someone for help, chances are they won't even be willing to read through all the comments in order to check where you have actual code.

also: if your variables name is itemNumber, your getter and setter should have the correct casing:

public int getItemNumber(){...
and public void setItemNumber(int itemNumber){..

at this point, printing several times makes no sense, since you only create one instance, and you never change the value of it's variables.

what exactly is it you are trying to do ?

I understand what you mean about all the commenting, but my instructor stresses about how we need to make sure we comment each line. I was just being paranoid about it so I just made sure I commented everything.
So for part one of this inventory program, my professor gave us this:
Choose a product that lends itself to an inventory (for example, products at your workplace, office supplies, music CDs, DVD movies, or software).

Create a product class that holds the item number, the name of the product, the number of units in stock, and the price of each unit.

Create a Java application that displays the product number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory (the number of units in stock multiplied by the price of each unit). Use a constructor to initialize the inventory information. Pay attention to the good programming practices in the text to ensure your source code is readable and well documented.

A few questions and suggestions:

  • Why did you include a null constructor? You generally only want a default c'tor if there is a reasonable default to provide, which in this case there really isn't. A null c'tor is generally a Bad Thing, as it looks like a proper default c'tor but doesn't actually initialize anything.

  • Do you really need to expose the number format? Generally speaking, this isn't something that is going to be changed over the course of the program lifetime. Furthermore, you don't actually use the number formatter to format the number values, so it's hard to see why this is included here at all. Since you are dealing with currency, and presumably the local settings are suitable, you should be applying the number formatter to the currency values when you display them (though see my next comment below).

  • In Java, all classes inherit the toString() method from the root Object class, and the usual solution to displaying an object is to use its toString() method. If you replace your display method with an override of toString(), and pass that string to the print method, you'll have a more flexible solution.

    @Override
    public String toString() {
        // Use the StringBuilder to create the string using the
        // fast append() method, then return the string 
        StringBuilder sb = new StringBuilder();
        String NEW_LINE = System.getProperty("line.separator");
    
        sb.append(Product Number: ");
        sb.append(getItemNumber());
        sb.append(NEW_LINE);
        sb.append("Product name: ");
        sb.append(getProductName();
        sb.append(NEW_LINE);
        sb.append("Number of Units in Stock: ";
        sb.append(getUnitsInStock());
        sb.append(NEW_LINE);
        sb.append("Product Price: $ ");
        sb.append(nf.format(getPriceOfEachUnit());
        sb.append(NEW_LINE);
        sb.append("Product Inventory Value: $ ");
        sb.append(nf.format(getValueOfInventory());
        sb.append(NEW_LINE);
        return sb.toString();
    }
    

Edited 2 Years Ago by Schol-R-LEA

This article has been dead for over six months. Start a new discussion instead.