如果有库存,则按价格升序对产品进行排序
Sort product if in stock then in ascending order in price
我正在做一个 Java 比较练习,要求我按产品是否有货对产品进行排序,然后按价格升序排列。有关更多详细说明,请阅读 PriceStockComparator class.
中的评论
import java.util.Comparator;
import java.util.Collections;
public class Product {
private String name;
private int priceCents;
private int stockLevel;
public Product(String name, int priceCents, int stockLevel) {
this.name = name;
this.priceCents = priceCents;
this.stockLevel = stockLevel;
}
@Override
public String toString() {
return this.name + ": " + this.stockLevel + " @ $" + this.priceCents / 100.0;
}
/**
* This comparator should sort products by whether or not they are in stock,
* then in ascending order by their price.
*
* After sorting, products that have a positive stock level should appear
* before products that have a stock level of 0. If two products being
* sorted are either 1) both in stock; or 2) both out of stock, they
* should then be sorted based on their price in ascending order.
*/
static class PriceStockComparator implements Comparator<Product> {
@Override
public int compare(Product p1, Product p2) {
// write your code here
return Integer.compare(p1.stockLevel, p2.stockLevel);
}
}
}
我的程序有 4 个错误
=> java.lang.AssertionError: expected:<[Apple: 5 @ .0, Bottled Water: 2 @ .0, Donut: 7 @ .0]> but was:<[Bottled Water: 2 @ .0, Apple: 5 @ .0, Donut: 7 @ .0]>
=> java.lang.AssertionError: expected:<[Apple: 5 @ .0, Bottled Water: 2 @ .0, Donut: 7 @ .0, Fish: 3 @ .0, Crackers: 0 @ .0, Eggs: 0 @ .0]> but was:<[Crackers: 0 @ .0, Eggs: 0 @ .0, Bottled Water: 2 @ .0, Fish: 3 @ .0, Apple: 5 @ .0, Donut: 7 @ .0]>
=> java.lang.AssertionError: expected:<[Crackers: 0 @ .0, Eggs: 0 @ .0]> but was:<[Eggs: 0 @ .0, Crackers: 0 @ .0]>
=> java.lang.AssertionError: expected:<[Donut: 7 @ .0, Fish: 3 @ .0, Eggs: 0 @ .0]> but was:<[Eggs: 0 @ .0, Fish: 3 @ .0, Donut: 7 @ .0]>
那是因为我排序了库存水平,但我不能先把它设为正数,而且我没有按价格升序排序。我不认为我可以在这里使用两个 compareTo() 并且我打算使用 Collection.sort
来对价格的升序进行排序。但我不知道该怎么做。如果有人可以提供帮助,我会很好。谢谢!
您可以按如下方式编写您的 compareTo 方法:
public int compare(Product p1, Product p2) {
//If both the products are in stock or are not in stock
// we can compare them on price
if(p1.getStockLevel()>0 && p2.getStockLevel()>0 ||
p1.getStockLevel()<=0 && p2.getStockLevel()<=0)
return Integer.compare(p1.getPriceCents(), p2.getPriceCents());
// If the first product is not in stock then we move it down the list
// by returning a -1
else if(p1.getStockLevel()>0 && p2.getStockLevel()<=0)
return -1;
// If second product is not in stock but first one
else
return 1;
}
您可以使用 Java 8 个 lambda 表达式。编译器能够推断类型定义。如果您的对象产品在列表中,您可以执行如下操作...
List<Product> products = new ArrayList<>();
Collections.sort(products, (a,b) -> a.priceCents - b.priceCents);
如果您需要参考,Baeldung 有一个很好的页面,其中包含一些您可以借鉴的示例。
我正在做一个 Java 比较练习,要求我按产品是否有货对产品进行排序,然后按价格升序排列。有关更多详细说明,请阅读 PriceStockComparator class.
中的评论import java.util.Comparator;
import java.util.Collections;
public class Product {
private String name;
private int priceCents;
private int stockLevel;
public Product(String name, int priceCents, int stockLevel) {
this.name = name;
this.priceCents = priceCents;
this.stockLevel = stockLevel;
}
@Override
public String toString() {
return this.name + ": " + this.stockLevel + " @ $" + this.priceCents / 100.0;
}
/**
* This comparator should sort products by whether or not they are in stock,
* then in ascending order by their price.
*
* After sorting, products that have a positive stock level should appear
* before products that have a stock level of 0. If two products being
* sorted are either 1) both in stock; or 2) both out of stock, they
* should then be sorted based on their price in ascending order.
*/
static class PriceStockComparator implements Comparator<Product> {
@Override
public int compare(Product p1, Product p2) {
// write your code here
return Integer.compare(p1.stockLevel, p2.stockLevel);
}
}
}
我的程序有 4 个错误
=> java.lang.AssertionError: expected:<[Apple: 5 @ .0, Bottled Water: 2 @ .0, Donut: 7 @ .0]> but was:<[Bottled Water: 2 @ .0, Apple: 5 @ .0, Donut: 7 @ .0]>
=> java.lang.AssertionError: expected:<[Apple: 5 @ .0, Bottled Water: 2 @ .0, Donut: 7 @ .0, Fish: 3 @ .0, Crackers: 0 @ .0, Eggs: 0 @ .0]> but was:<[Crackers: 0 @ .0, Eggs: 0 @ .0, Bottled Water: 2 @ .0, Fish: 3 @ .0, Apple: 5 @ .0, Donut: 7 @ .0]>
=> java.lang.AssertionError: expected:<[Crackers: 0 @ .0, Eggs: 0 @ .0]> but was:<[Eggs: 0 @ .0, Crackers: 0 @ .0]>
=> java.lang.AssertionError: expected:<[Donut: 7 @ .0, Fish: 3 @ .0, Eggs: 0 @ .0]> but was:<[Eggs: 0 @ .0, Fish: 3 @ .0, Donut: 7 @ .0]>
那是因为我排序了库存水平,但我不能先把它设为正数,而且我没有按价格升序排序。我不认为我可以在这里使用两个 compareTo() 并且我打算使用 Collection.sort
来对价格的升序进行排序。但我不知道该怎么做。如果有人可以提供帮助,我会很好。谢谢!
您可以按如下方式编写您的 compareTo 方法:
public int compare(Product p1, Product p2) {
//If both the products are in stock or are not in stock
// we can compare them on price
if(p1.getStockLevel()>0 && p2.getStockLevel()>0 ||
p1.getStockLevel()<=0 && p2.getStockLevel()<=0)
return Integer.compare(p1.getPriceCents(), p2.getPriceCents());
// If the first product is not in stock then we move it down the list
// by returning a -1
else if(p1.getStockLevel()>0 && p2.getStockLevel()<=0)
return -1;
// If second product is not in stock but first one
else
return 1;
}
您可以使用 Java 8 个 lambda 表达式。编译器能够推断类型定义。如果您的对象产品在列表中,您可以执行如下操作...
List<Product> products = new ArrayList<>();
Collections.sort(products, (a,b) -> a.priceCents - b.priceCents);
如果您需要参考,Baeldung 有一个很好的页面,其中包含一些您可以借鉴的示例。