Results 1 to 5 of 5
  1. #1
    NewKidjava is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default Help with Inventory Program, Please

    I am new to java and I am having some trouble trying to add an array in my program. I need to use an array to store items. I have been told by others that may program has an array in it already, but I don't think I have done it properly. Can someone please help? Thank you so much.

    Here's what I have so far:

    public class Inventory2
    {
    public static void main ( String args[] )
    {
    cd CD;

    cd = new CD(1, "The Block", 3, 12.99);
    System.out.println(cd);

    cd = new CD(2, "Step by Step", 5, 9.99);
    System.out.println(cd);

    cd = new CD(3, "Face the Music", 4, 7.99);
    System.out.println(cd);

    cd = new CD(4, "Summertime", 2, 2.99);

    } // end main
    } // end class

    class CD
    {
    private String cdName;
    private int cdCount;
    private double cdPrice;
    private int cdItem;
    }

    public class CD (int itemNumber, String productName, int unitCount, double unitPrice )
    {
    cdName = name;
    cdCount = count;
    cdPrice = price;
    cdItem = item;
    } // end constructor


    public void Inventory2( String productName )
    {
    p_ProductName = productName; // initializes p_ProductName
    } // end constructor


    // constructor initializes p_ProductName, p_UnitCount;
    // double instance variables are initialized to 0 by default

    public void Inventory2( String productName, int unitCount, double unitPrice )
    {
    ProductName = productName; // initializes ProductName
    UnitCount = unitCount; // initialized UnitCount
    UnitPrice = unitPrice; // initialized UnitPrice
    } // end constructor


    // begin method
    // method to retrieve the ProductName
    public String getProductName()
    {
    return ProductName;
    } // end method getProductName


    // method to retrieve the UnitCount
    public double getUnitCount()
    {
    return UnitCount;
    } // end method getUnitCount

    // method to retrieve the UnitPrice
    public double getUnitPrice()
    {
    return UnitPrice;
    } // end method getUnitPrice


    // method to retrieve product's value.
    public double getUnitValue()
    {
    return UnitCount*UnitPrice;
    } // end method getUnitValue

    public String toString()
    {
    return String.format( "name=%s count=%d price=%.2f item=%d value=$%.2f",
    name, count, price, item,value ());
    }
    } // end class CD

    class Inventory2
    {
    CD audio = new CD[200];
    public void addToInventory2(CD audio)
    {

    } //end class

    public double getProductValue()
    {
    Inventory2 myInventory = new Inventory();
    myInventory.addToInventory2(new CD (1, "Hangin' Tough", 2, 4.99));

    System.out.println( "The total value of inventory is:" + myInventory.getInventoryValue());
    return myinventory.getInventoryValue();
    } // end method

    } //end class Inventory2

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    NewKidjava is offline Member
    Join Date
    Aug 2009
    Posts
    2
    Rep Power
    0

    Default Tried that site

    Thanks for the link but i've tried that site before. I guess my main question would be is where to place the array? Would I be correct to place it at the beginning of the program?

    thank again

  4. #4
    Mr.Beans's Avatar
    Mr.Beans is offline Senior Member
    Join Date
    Apr 2009
    Location
    Virginia Beach, Virginia
    Posts
    149
    Rep Power
    0

    Default

    Here is a basic bare bones inventory class.

    Java Code:
    public class Inventory
    {
    	private ArrayList<CD> cdList = new ArrayList<CD>();
    	// or you could also use CD[]
    
    
    	public int getAmountOfCD() {
    		return cdList.size();
    	}
    	
    	public void addCD(CD item) {
    		cdList.add(item);
    	}
    
    	public void removeItem(CD item) {
    		cdList.remove(item);
    	}
    
    }
    However, maybe you need to freshen up on the very basics of creating variables and classes since you seem to get that very wrong in your post.

    You should check out the Java Trail
    Last edited by Mr.Beans; 08-21-2009 at 04:14 AM.

  5. #5
    dlorde is offline Senior Member
    Join Date
    Jun 2008
    Posts
    339
    Rep Power
    7

    Default

    Quote Originally Posted by NewKidjava View Post
    I guess my main question would be is where to place the array? Would I be correct to place it at the beginning of the program?
    It depends what you are going to do with it. If it is a way for one of your classes to store data, then make it a member variable of that class.

Similar Threads

  1. Inventory Program Part 2 of 6
    By tlouvierre in forum New To Java
    Replies: 2
    Last Post: 05-28-2009, 01:30 AM
  2. Inventory Program
    By tlouvierre in forum New To Java
    Replies: 5
    Last Post: 05-17-2009, 05:09 AM
  3. [SOLVED] GUI Inventory Program
    By marMcD in forum New To Java
    Replies: 5
    Last Post: 03-03-2009, 06:12 AM
  4. Inventory Program Part 3 ~ please help!
    By marMcD in forum New To Java
    Replies: 13
    Last Post: 02-25-2009, 05:57 AM
  5. Inventory program
    By Nexcompac in forum New To Java
    Replies: 3
    Last Post: 07-27-2007, 05:51 PM

Posting Permissions

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