Results 1 to 4 of 4
  1. #1
    crystylmoon is offline Member
    Join Date
    Apr 2011
    Rep Power

    Question Code written but cannot get it to add the total inventory amount and needs sorted

    Hi, I have written my code for my assignment and it compiles and runs, however, it doesn't display JUST the total of the inventory it displays all 5 totals again. I cannot figure out what I have done wrong, or could be I'm tired :eek:. Can someone please tell me what I am doing wrong? I also need to sort by product name but finally gave up on it and removed the code so I could at least get the total done.

    public class Laptop { //class name and attributes

    private String ItemNumber; //item # of product
    private String ProductName; //product name
    private double UnitsStock; //# of units in stock
    private double UnitPrice; //Price per unit
    private double InventoryValue; //The dollar value of the inventory in stock
    private double CalculateInventory; //The total value of all of the inventory in stock
    private double value;


    public Laptop(String item, String product, double units, double price) {
    ItemNumber = item;
    ProductName = product;
    UnitsStock = units;
    UnitPrice = price;

    } //end constructor

    //getter and setter methods for Laptop

    public void setItemNumber (String item) { //setter for item number
    this.ItemNumber = item;
    } //end setter item number

    public String getItemNumber() { //getter for item number
    return ItemNumber;
    } //end getter item number

    public void setProductName (String product) { //setter for product name
    this.ProductName = product;
    } //end setter product name

    public String getProductName() { //getter for product name
    return ProductName;
    } //end getter product name

    public void setUnitsStock (double units) { //setter for units in stock
    this.UnitsStock = units;
    } //end setter units in stock

    public double getUnitsStock() { //getter for units in stock
    return UnitsStock;
    } //end getter units in stock

    public void setUnitPrice (double price) { //setter for unit price
    this.UnitPrice = price;
    } //end setter unit price

    public double getUnitPrice() { //getter for unit price
    return UnitPrice;
    } //end getter for unit price

    //calculate inventory value

    public double getInventoryValue(){
    return UnitsStock * UnitPrice;

    }//end calculate inventory value

    public void setCalculateInventory (double value){
    this.CalculateInventory = value;

    public double getCalculateInventory(){
    double value = 0;
    for(int i = 0; i < 5; i++){
    value = getInventoryValue();
    return value;

    //end getter and setter methods for Laptop

    } //end class Laptop

    //begin main program

    import java.util.*;

    public class InventoryPart2 {

    public static void main (String args []){

    //instantiate Laptop array

    Laptop myLaptops[] = new Laptop[5];

    myLaptops[0] = new Laptop("0001", "Dell Inspiron 1545", 4, 549.99);
    myLaptops[1] = new Laptop("0002", "IBM Thinkpad R400", 6, 580.00);
    myLaptops[2] = new Laptop("0003", "HP Pavilion", 2, 679.99);
    myLaptops[3] = new Laptop("0004", "Sony Vaio C Series", 8, 899.99);
    myLaptops[4] = new Laptop("0005", "Apple MacBook Pro", 2, 1799.00);


    for (int i = 0; i < myLaptops.length; i++){

    System.out.println("Product Number: \t\t" + myLaptops[i].getItemNumber());
    System.out.println("Product Name: \t\t\t" + myLaptops[i].getProductName());
    System.out.println("Number of Units in Stock: \t" + myLaptops[i].getUnitsStock());
    System.out.printf("Price per Unit: \t\t$%.2f\n", + myLaptops[i].getUnitPrice());
    System.out.printf("Value of Inventory: \t\t$%.2f\n", + myLaptops[i].getInventoryValue());

    } // end output

    for (int i = 0; i < 5; i++){ //output total inventory value
    System.out.printf("Total Value of Inventory is: \t$%.2f\n", + myLaptops[i].getCalculateInventory());

    }//end output total inventory value

    } //end method main

    } //end class InventoryPart1

  2. #2
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Atlanta, Georgia, US
    Rep Power


    Please use code tags, indent, and delete your comments so we can easily read your code.

  3. #3
    crystylmoon is offline Member
    Join Date
    Apr 2011
    Rep Power

    Default Sorry about that

    It was indented when I hit submit, oh well, I guess me posting is what it took for me to figure it out! I did it.

  4. #4
    ra4king's Avatar
    ra4king is offline Senior Member
    Join Date
    Apr 2011
    Atlanta, Georgia, US
    Rep Power


    Yes indentation is deleted when you type plain words. If you use code tags like so: [ code ] code goes here [ /code ] (without the spaces between the brackets), all indentation will be restored.

Similar Threads

  1. Replies: 1
    Last Post: 03-29-2011, 08:59 AM
  2. Replies: 0
    Last Post: 03-18-2011, 09:48 PM
  3. I need some java / html code written
    By DeDanan in forum New To Java
    Replies: 2
    Last Post: 03-01-2011, 10:36 AM
  4. Replies: 6
    Last Post: 04-13-2009, 04:19 AM
  5. Replies: 7
    Last Post: 04-28-2008, 04:28 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts