【定制排序、使用比较器】 商品属性Production // 价格、商品名、销量、评论数、客户收藏数量 现有商品的仓库,提供了三个方法 --商品集合【属性】 --实现商品的价格降序排序() --实现商品的价格升序序排序() --实现商品的排序(入参比较器【匿名内部类】)
时间: 2023-12-16 16:03:23 浏览: 169
下面是一个示例代码,可以对商品集合根据不同的属性进行排序:
```
import java.util.*;
// 商品类
class Product {
private String name;
private int price;
private int sales;
private int comments;
private int favorites;
public Product(String name, int price, int sales, int comments, int favorites) {
this.name = name;
this.price = price;
this.sales = sales;
this.comments = comments;
this.favorites = favorites;
}
public String getName() {
return name;
}
public int getPrice() {
return price;
}
public int getSales() {
return sales;
}
public int getComments() {
return comments;
}
public int getFavorites() {
return favorites;
}
}
// 商品比较器类
class ProductComparator {
public static Comparator<Product> byPriceDescending() {
return new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p2.getPrice() - p1.getPrice();
}
};
}
public static Comparator<Product> byPriceAscending() {
return new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p1.getPrice() - p2.getPrice();
}
};
}
public static Comparator<Product> bySalesDescending() {
return new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p2.getSales() - p1.getSales();
}
};
}
public static Comparator<Product> byCommentsDescending() {
return new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p2.getComments() - p1.getComments();
}
};
}
public static Comparator<Product> byFavoritesDescending() {
return new Comparator<Product>() {
@Override
public int compare(Product p1, Product p2) {
return p2.getFavorites() - p1.getFavorites();
}
};
}
}
// 仓库类
class Warehouse {
private List<Product> productList;
public Warehouse(List<Product> productList) {
this.productList = productList;
}
public void sortProducts(Comparator<Product> comparator) {
Collections.sort(productList, comparator);
}
public void sortProductsByPriceDescending() {
sortProducts(ProductComparator.byPriceDescending());
}
public void sortProductsByPriceAscending() {
sortProducts(ProductComparator.byPriceAscending());
}
public List<Product> getProductList() {
return productList;
}
}
public class Test {
public static void main(String[] args) {
// 构造商品集合
List<Product> productList = new ArrayList<>();
productList.add(new Product("商品1", 100, 10, 20, 30));
productList.add(new Product("商品2", 200, 20, 30, 40));
productList.add(new Product("商品3", 300, 30, 40, 50));
// 构造仓库对象
Warehouse warehouse = new Warehouse(productList);
// 按价格降序排序
warehouse.sortProductsByPriceDescending();
List<Product> sortedProducts = warehouse.getProductList();
System.out.println("按价格降序排序:");
for (Product p : sortedProducts) {
System.out.println(p.getName() + ",价格:" + p.getPrice());
}
// 按价格升序排序
warehouse.sortProductsByPriceAscending();
sortedProducts = warehouse.getProductList();
System.out.println("\n按价格升序排序:");
for (Product p : sortedProducts) {
System.out.println(p.getName() + ",价格:" + p.getPrice());
}
// 按销量降序排序
warehouse.sortProducts(ProductComparator.bySalesDescending());
sortedProducts = warehouse.getProductList();
System.out.println("\n按销量降序排序:");
for (Product p : sortedProducts) {
System.out.println(p.getName() + ",销量:" + p.getSales());
}
// 按评论数降序排序
warehouse.sortProducts(ProductComparator.byCommentsDescending());
sortedProducts = warehouse.getProductList();
System.out.println("\n按评论数降序排序:");
for (Product p : sortedProducts) {
System.out.println(p.getName() + ",评论数:" + p.getComments());
}
// 按客户收藏数量降序排序
warehouse.sortProducts(ProductComparator.byFavoritesDescending());
sortedProducts = warehouse.getProductList();
System.out.println("\n按客户收藏数量降序排序:");
for (Product p : sortedProducts) {
System.out.println(p.getName() + ",客户收藏数量:" + p.getFavorites());
}
}
}
```
阅读全文