New to java looking for a little help adding a feature

Hello,

I am trying to take an Inventory2 and products file and add the ability to sort the list by the title of the movie. Is there a way to add a feature to my existing code or will I have to redesign it using a compare class?

Here is the code:

import java.io.InputStreamReader;

import java.io.BufferedReader;

import java.io.IOException;

// begin Class Inventory2

publicclass Inventory2

{

publicstaticvoid main( String[] args)

{

// create inventory of items

Products title1 =new Products(1,"Movie 1", 2, 39.99);

Products title2 =new Products(2,"Movie 2", 6, 29.99);

Products title3 =new Products(3,"Movie 3", 7, 29.99);

Products title4 =new Products(4,"Movie 4", 8, 59.99);

//Display the inventories one at a time.

title1.showProducts();

title2.showProducts();

title3.showProducts();

title4.showProducts();

}// End main method

}// End Inventory2

// Begin class products

publicclass Products

{

privateint productNum;

private String name =new String();

privateint units;

privatedouble price;

publicint getproductNum()// Get productNum

{

return productNum;

}

publicvoid setproductNum(int productNum)// productNum

{

this.productNum = productNum;

}

public String getName()// Get productNane

{

return name;

}

publicvoid setName(String name)// Set productNane

{

this.name = name;

}

publicint getUnits()// Get units

{

return units;

}

publicvoid setUnits(int units)// Set units

{

this.units = units;

}

publicdouble getPrice()// Get price

{

return price;

}

publicvoid setPrice(double price)// Set price

{

this.price = price;

}

// Constructors

Products()

{// Empty Constructor

}//End constructor

// Constructor that takes arguments

Products(int _productNum, String _name,int _units,double _price)

{

// Constructor

productNum = _productNum;

name = _name;

units = _units;

price = _price;

}// End constructor

// Computes value of Products

publicdouble valueOfProducts()

{

return price * units;

}

// Displays the details of the Products

publicvoid showProducts()

{

System.out.println();// prompt

// Output messages

System.out.println("Name: "+name);

System.out.println("Number "+productNum);

System.out.println("Items Available = "+units);

System.out.println("Price $"+price);

System.out.println("Value of Inventory"+name+" is = $"+valueOfProducts());

}// End display

}// End class

[6432 byte] By [Techman0405a] at [2007-11-27 9:55:43]
# 1

In your main method, add each Product to a List of Products as you create them. Either implement Comparable in your Product class, or create a Comparator. That's where you implement the comparison between the names. Then you can use one of the Collections.sort() methods to sort the List, and then print each Product in order.

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Comparator.html

http://java.sun.com/j2se/1.5.0/docs/api/java/lang/Comparable.html

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List)

http://java.sun.com/j2se/1.5.0/docs/api/java/util/Collections.html#sort(java.util.List,%20java.util.Comparator)

hunter9000a at 2007-7-13 0:25:44 > top of Java-index,Java Essentials,New To Java...
# 2
it looks to me you are always printing title1 then then title2.. etc.you need to change your logic before the display the inventories on at a time. the products are not in arrays. you might consider revising the way you creat the products. study collections.thanks
khalidkattana at 2007-7-13 0:25:45 > top of Java-index,Java Essentials,New To Java...