create an entity class "Product" with following properties product_id, product_name, product_details, price. Add some sample data in this product class
using collections. and sort and display :
-->on default by product_id
-->based on product_name or price
-->sort on product_name, if two product_name is same sort on product_details basis.
-->sort on price, if two prices are same consider sorting on product_id basis.
Product.java
using collections. and sort and display :
-->on default by product_id
-->based on product_name or price
-->sort on product_name, if two product_name is same sort on product_details basis.
-->sort on price, if two prices are same consider sorting on product_id basis.
Product.java
package com.test;
import
java.util.Comparator;
public class Product implements
Comparable<Product>{
private int productId;
private String productName;
private String productDescription;
private double price;
public Product()
{
}
public Product(int productId,
String productName, String
productDescription, double price)
{
this.productId = productId;
this.productName = productName;
this.productDescription =
productDescription;
this.price = price;
}
public int getProductId()
{
return productId;
}
public void setProductId(int productId)
{
this.productId = productId;
}
public String
getProductName()
{
return productName;
}
public void
setProductName(String productName)
{
this.productName = productName;
}
public String
getProductDescription()
{
return productDescription;
}
public void
setProductDescription(String productDescription)
{
this.productDescription =
productDescription;
}
public double getPrice()
{
return price;
}
public void setPrice(double price)
{
this.price = price;
}
@Override
public int
compareTo(Product p)
{
return this.productId-p.productId;
}
@Override
public String
toString()
{
return "price= " + price + ",
productDescription= "
+ productDescription + ",
productId= " + productId
+ ",
productName= " + productName;
}
public static class
ProductInnerClass implements Comparator<Product>
{
@Override
public int compare(Product
p1, Product p2)
{
int i=Double.compare(p1.getPrice(),
p2.getPrice());
if(i==0)
{
return
p1.getProductId()-p2.getProductId();
}
return i;
}
}
}
ProductMain.java
package
com.alok.entity;
import java.io.BufferedReader;
import
java.io.IOException;
import
java.io.InputStreamReader;
import
java.util.ArrayList;
import
java.util.Collections;
import
java.util.Comparator;
import
com.alok.client.Product;
public class ProductMain {
public static void main(String[]
args) throws NumberFormatException, IOException {
ArrayList<Product> productList=new
ArrayList<Product>();
productList.add(new Product(555, "Monitor", "15
inch", 4000.00));
productList.add(new Product(2222, "Monitor", "17
inch", 5000.00));
productList.add(new Product(3333, "Del", "Laptop", 4000.00));
productList.add(new Product(6666, "Mouse", "Optical
Mouse", 200.00));
BufferedReader br=new BufferedReader(new
InputStreamReader(System.in));
int i=1;
while(i<3)
{
System.out.println("1 : Sort
by id");
System.out.println("2 : Sort
by name");
System.out.println("3 : Sort
by price");
int ch=Integer.parseInt(br.readLine());
switch(ch)
{
case 1 :
Collections.sort(productList);
System.out.println("Sorted
product : ");
for(Product p :
productList)
{
System.out.println(p);
}
break;
case 2 :
Collections.sort(productList, new Comparator<Product>()
{
@Override
public int compare(Product
p1, Product p2)
{
int
i=p1.getProductName().compareToIgnoreCase
(p2.getProductName());
if(i==0)
{
return
p1.getProductDescription().
compareToIgnoreCase(p2.getProductDescription());
}
return i;
}
});
System.out.println("Sorted
product : ");
for(Product p :
productList)
{
System.out.println(p);
}
break;
case 3 :
Collections.sort(productList,new Product.ProductInnerClass());
System.out.println("Sorted
product : ");
for(Product p :
productList)
{
System.out.println(p);
}
break;
default : System.out.println("Invalid
Option");
System.exit(0);
}
}
}
}
OUTPUT:
1 : Sort by id
2 : Sort by name
3 : Sort by price
1
Sorted product :
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
2
Sorted product :
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
3
Sorted product :
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
1 : Sort by id
2 : Sort by name
3 : Sort by price
4
Invalid Option
2 : Sort by name
3 : Sort by price
1
Sorted product :
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
2
Sorted product :
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
1 : Sort by id
2 : Sort by name
3 : Sort by price
3
Sorted product :
price= 200.0, productDescription= Optical Mouse, productId= 6666, productName= Mouse
price= 4000.0, productDescription= 15 inch, productId= 555, productName= Monitor
price= 4000.0, productDescription= Laptop, productId= 3333, productName= Del
price= 5000.0, productDescription= 17 inch, productId= 2222, productName= Monitor
1 : Sort by id
2 : Sort by name
3 : Sort by price
4
Invalid Option
No comments:
Post a Comment