Inventory Program Part2

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • JosAH
    Recognized Expert MVP
    • Mar 2007
    • 11453

    #16
    You've still missed the point: a Product should be a single class, not just a couple
    of separate arrays that take the individual attributes of a Product. First define that
    little class.

    kind regards,

    Jos

    Comment

    • chaarmann
      Recognized Expert Contributor
      • Nov 2007
      • 785

      #17
      first make a class called Product. It stores the name price, stock, name, and number (which are called attributes) of a single object. Then this class should have setter and getter methods for each attribute. Like
      Code:
      public String getName()
      {
         return this.name;
      }
      
      public void setName(String newName)
      {
         this.name = newName;
      }
      You don't need the setter methods if you make a constructor that sets them all, and if you are sure you don't need to change them anymore. But you need the getter methods to make up your Comparator later on.

      Then you make an array of these products.
      Code:
      Product[10] productArray =
      {
       new Product("apple", "10.20 Dollar", 3),
       new Product("banana", "6.40 Dollar", 4),
       ...
      }
      That means, as what JosAH was saying, you don't have separate arrays of names, prices etc. Instead you have single products as objects which have all its data inside.

      Then read about about Collections and Comparator to sort your array the way you want.
      Sorting should be in a single line like:
      Code:
      Collections.sort(productArray, sortByPriceComparator);

      Comment

      Working...