Working with arrays question

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • hamiltongreg
    New Member
    • Aug 2007
    • 6

    #1

    Working with arrays question

    I am pretty new to Java and have a question about where to begin with my program.

    The assignment is to Modify the Inventory Program so the application can handle multiple items. Use an array to store the items. The output should display the information one product at a time, including the item number, the name of the product, the number of units in stock, the price of each unit, and the value of the inventory of that product. In addition, the output should display the value of the entire inventory.
    • Create a method to calculate the value of the entire inventory.
    • Create another method to sort the array items by the name of the product.

    Here is my code
    Code:
    /** 
      *@author Greg Hamilton
      *Program was changed 31 Aug 07
      *The purpose of this program is to create a product class that holds an item number, name of the DVD movie, number of units in stock
      *and the price of each unit
    */
    public class Dvd 
    
    {
    
    private String dvdTittle;
    private double dvdStock;
    private double dvdPrice;
    private double dvdItem;
    public Dvd( String tittle, double stock, double price, double item )
    
    {
    //implicit call to Object constructor occurs here
    dvdTittle = tittle;
    dvdStock = stock;
    dvdPrice = price;
    dvdItem = item;
    } //end four-argument constructor
    
    
    
    //set DVD name
    public void setdvdTittle( String tittle )
    {
    dvdTittle = tittle;
    } //end method setDvdTittle
    
    //return dvd Tittle
    public String getDvdTittle()
    {
    return dvdTittle;
    } //end method getDvdTittle
    
    //set Dvd stock
    public void setDvdStock( double stock)
    {
    dvdStock = ( stock < 0.0 ) ? 0.0 : stock;
    } //end method setDvdStock
    
    //return dvd stock
    public double getDvdStock()
    {
    return dvdStock;
    } //end method getDvdStock
    
    public void setDvdPrice( double price )
    {
    dvdPrice = ( price <0.0 ) ? 0.0 : price;
    } //end method SetDvdPrice
    
    //return dvd price
    public double getDvdPrice()
    {
    return dvdPrice;
    } //end method get Dvd Price
    
    public void setDvdItem( double item )
    {
    dvdItem = ( item <0.0) ? 0.0 : item;
    } //end method set dvd Item
    
    //return dvd item
    
    public double getDvdItem()
    {
    return dvdItem;
    } //end method getDvdItem
    
    
    // calculate inventory value
    public double value()
    {
    return dvdPrice * dvdStock;
    
    } //end method value
    
    
    
    
    
    public static void main( String args[] )
    
    {
    //instantiate Collection object
    Dvd aDvd= new Dvd ("Dejavu", 10, 12.50, 101); 
    
    
    System.out.println("Product Tittle is "+ aDvd.getDvdTittle() ); //display tittle
    System.out.println("The number of units in stock is "+ 
    aDvd.getDvdStock() ); //display units in stock
    System.out.println("The price of each DVD is $"+ aDvd.getDvdPrice() ); //display price
    System.out.println( "The item number is "+ aDvd.getDvdItem()); //display unit number
    System.out.println( "The value of the inventory is $"+ aDvd.value() ); //display total value
    
    } //end main
    
    } //end class Dvd
    I am really struggling where to start.

    On the array side, would I use the array only to store the item name, or would the array need to include the other features?

    I am confused since an array can only hold the same data type and don't think that the assignment is asking me to use multiple arrays.

    Thanks for any pointers.
  • Nepomuk
    Recognized Expert Specialist
    • Aug 2007
    • 3111

    #2
    Originally posted by hamiltongreg
    ...
    I am really struggling where to start.

    On the array side, would I use the array only to store the item name, or would the array need to include the other features?

    I am confused since an array can only hold the same data type and don't think that the assignment is asking me to use multiple arrays.

    Thanks for any pointers.
    You're right, an array can only hold data of the same type. However, who says that type has to be String?

    First of all, think about how you would do it in the real world. There are two possibilities:

    Either you have an Index of all Items (e.g. Name and position) and a Collection of the items
    or you just have a Collection of the items themselves, including everything at once.

    Each Item (it was a DVD, right?) has certain Values: a name, a language, a length... what ever is important for your tast. Then think about how you can put that information into one big lump and have an array of those "lumps".
    That should be enough to bring you on the right track, I guess.

    Greetings,
    Nepomuk

    Comment

    • hamiltongreg
      New Member
      • Aug 2007
      • 6

      #3
      Thanks for the pointer, you got me back on track!


      Originally posted by nepomuk
      You're right, an array can only hold data of the same type. However, who says that type has to be String?

      First of all, think about how you would do it in the real world. There are two possibilities:

      Either you have an Index of all Items (e.g. Name and position) and a Collection of the items
      or you just have a Collection of the items themselves, including everything at once.

      Each Item (it was a DVD, right?) has certain Values: a name, a language, a length... what ever is important for your tast. Then think about how you can put that information into one big lump and have an array of those "lumps".
      That should be enough to bring you on the right track, I guess.

      Greetings,
      Nepomuk

      Comment

      Working...